Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

WIndows.Forms.Timer or System.Timer?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    WIndows.Forms.Timer or System.Timer?

    Under Best Practises, an example is given using Windows.Forms.Timer... which is good for single threads, right?

    What I need is a timer mechanism that I can have running on an indicator on multiple charts concurrently... and am wondering if I should instead use the multi-threaded System.Timer.

    This arises when I want to send two emails on a closing condition (in realtime) on the chart... One email to my email address... and a second to an SMS provider.
    What I need to avoid is multiple instances of this thing trying to send emails concurrently, (or even 1 immediately after the other as in the code below), as I get an error logged saying there are too many connections from my host (that would be SMTP connections I presume)
    I have this code:
    Code:
    SendMail([COLOR=#d69d85]my_email_address[/COLOR], [COLOR=#569cd6]string[/COLOR][COLOR=#b4b4b4].[/COLOR]Format([COLOR=#d69d85]"PD Alert: {0} BTC {1} @ {2}"[/COLOR], exit_bar_str, MI[COLOR=#b4b4b4].[/COLOR]Name, MI[COLOR=#b4b4b4].[/COLOR]FormatPrice(trade_price)), [COLOR=#569cd6]string[/COLOR][COLOR=#b4b4b4].[/COLOR]Format([COLOR=#d69d85]"{0} BTC {1} @ {2}"[/COLOR], exit_bar_str, MI[COLOR=#b4b4b4].[/COLOR]Name, MI[COLOR=#b4b4b4].[/COLOR]FormatPrice(trade_price)));
    SendMail(sms_address, [COLOR=#569cd6]string[/COLOR][COLOR=#b4b4b4].[/COLOR]Format([COLOR=#d69d85]"PD Alert: {0} BTC {1} @ {2}"[/COLOR], exit_bar_str, MI[COLOR=#b4b4b4].[/COLOR]Name, MI[COLOR=#b4b4b4].[/COLOR]FormatPrice(trade_price)), [COLOR=#569cd6]string[/COLOR][COLOR=#b4b4b4].[/COLOR]Format([COLOR=#d69d85]"{0} BTC {1} @ {2}"[/COLOR], exit_bar_str, MI[COLOR=#b4b4b4].[/COLOR]Name, MI[COLOR=#b4b4b4].[/COLOR]FormatPrice(trade_price)));
    So... I figure if I deploy a Timer, I can delay sending the second email by say a random number of seconds... which is fine for my purposes, I am only using this on a daily chart, seconds don't matter a hoot. Why random? Well, if I only had this indicator on one chart, I could use a fixed delay like 5 seconds between the two calls to SendMail... but with the indicator deployed on maybe 10 charts (all with different symbols)... and the condition may trigger on more than one chart at the same instant... there is a potential problem.
    So... maybe code like this might work?
    Code:
    Wait_Random_Seconds();
    SendMail([COLOR=#d69d85]my_email_address[/COLOR], [COLOR=#569cd6]string[/COLOR][COLOR=#b4b4b4].[/COLOR]Format([COLOR=#d69d85]"PD Alert: {0} BTC {1} @ {2}"[/COLOR], exit_bar_str, MI[COLOR=#b4b4b4].[/COLOR]Name, MI[COLOR=#b4b4b4].[/COLOR]FormatPrice(trade_price)), [COLOR=#569cd6]string[/COLOR][COLOR=#b4b4b4].[/COLOR]Format([COLOR=#d69d85]"{0} BTC {1} @ {2}"[/COLOR], exit_bar_str, MI[COLOR=#b4b4b4].[/COLOR]Name, MI[COLOR=#b4b4b4].[/COLOR]FormatPrice(trade_price)));
    Wait_Random_Seconds();
    SendMail(sms_address, [COLOR=#569cd6]string[/COLOR][COLOR=#b4b4b4].[/COLOR]Format([COLOR=#d69d85]"PD Alert: {0} BTC {1} @ {2}"[/COLOR], exit_bar_str, MI[COLOR=#b4b4b4].[/COLOR]Name, MI[COLOR=#b4b4b4].[/COLOR]FormatPrice(trade_price)), [COLOR=#569cd6]string[/COLOR][COLOR=#b4b4b4].[/COLOR]Format([COLOR=#d69d85]"{0} BTC {1} @ {2}"[/COLOR], exit_bar_str, MI[COLOR=#b4b4b4].[/COLOR]Name, MI[COLOR=#b4b4b4].[/COLOR]FormatPrice(trade_price)));
    Yes a random timer will not guarantee no conflicts... but it's the best idea I had so far ;-) If I wait say between 30 and 240 seconds or whatever... chances are indeed slim, with the caveat of ensuring calls to Random use different seeds on each chart etc...

    So... two questions:
    1. Should I use System.Timer that is good for multiple threads... I think the answer is Yes, given different symbols' charts run in different threads, right?
    2. Is there a better way to achieve this?
    Thanks,
    T.

    #2
    Hi tgn, thanks for writing in.

    If there is a conflict with the frequency of messages with the SMTP server then the best way of doing this would be to add all of the data series under one script using AddDataSeries() then put these messages into a queue and send one email from the queue at a time. Using a random timer or making the thread sleep is not recommended and would not give consistent results.

    Kind regards,
    -ChrisL

    Comment


      #3
      Thanks, that's not going to work for me.

      This fails on just one chart, with the two sequential mails as per my first code above. Evidently, two emails sent one after the other breaks things.
      A related question that might partially solve this... can you specify more than one recipient in the first arg of SendMail? Same way you can put two recipients separated by a semicolon in most mail agents? There is nothing in the SendMail documentation about this... which is why I just invoked it twice.

      Comment


        #4
        OK... thought I'd test this, calling from the Addon Shell. Can't use SendMail... need to use the Addon variant Globals.SendMail... which (woohoo !) includes a "Cc:" argument...

        Guess what... Globals.SendMail's 2nd arg, ie Cc ... does not work. It sends email to the first recipient, ie the "To", but I've verified that a valid 2nd arg "Cc" is silently ignored. That;looks like a bug... (Cancel that Woohoo!)

        T.

        Comment


          #5
          Hi tgn55, thanks for your reply.

          Can you please post a sample that can reproduce this?

          Best regards,
          -ChrisL

          Comment


            #6
            Sure...

            Please note: the attached addon has some completely unrelated elements, I was testing for another project... just ignore everything except the email address boxes and the Click Me button ;-)
            Also, if you look in the code you will see I call Globals.SendMail twice... with the TO and CC args reversed.

            What I see is it is always and only the first arg that receives the email... never the second, ie the Cc address. QED.

            Regards,
            T.
            Attached Files

            Comment


              #7
              Hi tgn,

              I got the same issue so I reported it to our development team. I'll let you know as soon as I get an update on the issue.

              Best regards.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              598 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              343 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              103 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              556 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              555 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X