+= System.Timers.ElapsedEventHandler -> is running in the same thread or seperate?

已答复 += System.Timers.ElapsedEventHandler -> is running in the same thread or seperate?

  • 2012年8月1日 16:34
     
     

    hi

    I have a program that runs mostly using timers.

    each timers trigger an event. -> i also have multiple events trigger at the same time?

    I am wondering, does these events run on separate thread or do they run on the same thread as the program?

    Thanks

全部回复

  • 2012年8月1日 16:38
    版主
     
     已答复

    The System.Timers.Timer (and System.Threading.Timer) call their event on a different thread each time.  They don't have a "same thread" as the program, but rather will raise their elapsed events on a ThreadPool thread.

    If you need a timer on the UI thread, you can use System.Windows.Forms.Timer (Windows Forms) or DispatcherTimer (WPF) to get a "UI thread owned" timer.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

  • 2012年8月1日 17:08
     
     

    thank you very much. It is very clear now.

    btw

    if i trigger multiple events at the same times, is there a more elegant way of doing instead of timers?

    • 已编辑 TuBong 2012年8月1日 17:09
    •  
  • 2012年8月1日 17:16
    版主
     
     

    It depends on what you're doing - timers are fairly clean and simple, though, so they're often the "nicest" or most "elegant" way to get there.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

  • 2012年8月1日 17:46
     
     
    those events are made to do tons of math calculations from array (400 elements), not sure if that helps.
    • 已编辑 TuBong 2012年8月1日 17:47
    •  
  • 2012年8月1日 18:08
     
     

    Hello,

    Why can't you use one thread that calls these calculations through a queue instead of executing each one of them on different threads.

    Why do you need to execute them simultaneously? 


    Regards,

    Eyal Shilony


  • 2012年8月1日 18:21
    版主
     
     
    If you're trying to do parallel processing of the array, you should look at the TPL instead of timers.  Parallel.For and Parallel.ForEach can be used to process collections in parallel, and do a much better job.  See "Data Parallelism" for more details: http://msdn.microsoft.com/en-us/library/dd537608

    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

  • 2012年8月1日 18:46
     
      包含代码
    If you're trying to do parallel processing of the array, you should look at the TPL instead of timers.  Parallel.For and Parallel.ForEach can be used to process collections in parallel, and do a much better job.  See "Data Parallelism" for more details: http://msdn.microsoft.com/en-us/library/dd537608

    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    thanks, i have never done this stuff before. but, looking at the link, can you explain me what is sourceCollection is in this code? i.e. how do i defind sourceCollection?

    // Sequential version            
    foreach (var item in sourceCollection)
    {
        Process(item);
    }
    
    // Parallel equivalent
    Parallel.ForEach(sourceCollection, item => Process(item));


    FYI - this is what i am doing in my program

    i have a timer that triggers these events every 500 ms

    eg.

            static void Event1(object sender, System.Timers.ElapsedEventArgs e)
            {
                double[] array1 = arrayTmp;
                List<double> list1 = new List<double>(); // list to store objects
                for (int i = 0; i < 200; i++)
                {
                    if (array1[i] > 500)
                        list1.Add(array1[i]);
                }
    
                for (int i = 0; i < list1.Count; i++)
                {
                    //do math here
                }
            }
    
            static void Event2(object sender, System.Timers.ElapsedEventArgs e)
            {
                // similar style with different array elements
            }
    
            static void Event3(object sender, System.Timers.ElapsedEventArgs e)
            {
                //...
            }

    • 已编辑 TuBong 2012年8月1日 18:56
    •  
  • 2012年8月1日 21:21
     
     

    hi

    I have a program that runs mostly using timers.

    each timers trigger an event. -> i also have multiple events trigger at the same time?

    I am wondering, does these events run on separate thread or do they run on the same thread as the program?

    Thanks


    The events run on the same thread as the SynchronizingObject.
  • 2012年8月1日 21:22
    版主
     
      包含代码

    I would write this as:

    // Get your array of elements
    double[] array1 = arrayTmp; 
    
    // Make a partitioner
    var rangePartitioner = Partitioner.Create(0, array1.Length);
    
    
    // Loop over the partitions in parallel.
    Parallel.ForEach(rangePartitioner, (range, loopState) =>
            {
                // Loop over each range element without a delegate invocation.
                for (int i = range.Item1; i < range.Item2; i++)
                {
                    double value = array1[i];
                    // do math here
                }
            });
    
    

    This will scale to any number of cores on the system, and automatically partition your array out for you correctly.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

  • 2012年8月2日 14:06
     
      包含代码

    I would write this as:

    // Get your array of elements
    double[] array1 = arrayTmp; 
    
    // Make a partitioner
    var rangePartitioner = Partitioner.Create(0, array1.Length);
    
    
    // Loop over the partitions in parallel.
    Parallel.ForEach(rangePartitioner, (range, loopState) =>
            {
                // Loop over each range element without a delegate invocation.
                for (int i = range.Item1; i < range.Item2; i++)
                {
                    double value = array1[i];
                    // do math here
                }
            });
    

    This will scale to any number of cores on the system, and automatically partition your array out for you correctly.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Hi

    I test your code, but what I see is that the output are not in order.

    I mean, the process of looping through the array are not in logical order.

    Is that expected? Is there a way I can make it output in order?

  • 2012年8月2日 14:30
     
     

    Hi,

    yes, this is expected, as Parallel.For makes them executing at the same time. But this should only be a problem if you want to display the values live and in order on your UI or if you want to write your data into an array/a list live.

    If you're talking about the output window in Visual Studio, the order shouldn't matter. It depends on how fast the code in every loop is, how your CPU is prioritizing it etc. 

    It really depends on when you want to use the calculated data, while you're still in the loop(s) or when the loops have finished?

  • 2012年8月2日 17:56
    版主
     
     


    I test your code, but what I see is that the output are not in order.

    I mean, the process of looping through the array are not in logical order.

    Is that expected? Is there a way I can make it output in order?

    The entire point of processing the items at the same time is to effectively not require that they are processed in order.

    I would recommend doing your math in parallel (like I showed), but then display the results in a loop when you're done, since this lets you get complete control over the order.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

  • 2012年8月2日 18:13
     
     

    Hi,

    yes, this is expected, as Parallel.For makes them executing at the same time. But this should only be a problem if you want to display the values live and in order on your UI or if you want to write your data into an array/a list live.

    If you're talking about the output window in Visual Studio, the order shouldn't matter. It depends on how fast the code in every loop is, how your CPU is prioritizing it etc. 

    It really depends on when you want to use the calculated data, while you're still in the loop(s) or when the loops have finished?

    Hi

    Thanks for the confirmation.

    Yes, I am doing the math while the program loop through the List<> in logical order. so its not when the loop have finished.