Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys

Public Class1
{
         public int Func()
         {

            Blah Blah Blah

         }

         public int Func2()
         {

           Blah Blah Blah

         }


         void main()
         {
           int value1;
           Class1 CL1 =new Class1();
           var thread1 = new Thread(() =>{value1 = CL1.Func();}); 
 var thread2 = new Thread(() =>{ System.ServiceProcess.ServiceBase.Run(Func2());});
         }
}

Here goes my question,
I want to schedule the thread1 i.e I want to call method(Func) at regular intervals and I will compare the values returned by it each time.Based on the comparison outcome I want to restart the thread2 even though it is running.Is it possible to achieve this.
Thanks in advance.
Posted

1 solution

There is no such concept as "restart a thread". You can have some thread terminated (say, abort it, or wait until it exits) and create a thread with the same method, but it will be another thread. The question is: why?

The better approach is to reuse the same thread in cycle, throttling it by some other thread using an event way handle. This technique is described in my past answers:
http://www.codeproject.com/Answers/317777/pause-running-thread#answer2[^],
Making Code Thread Safe[^],
ManualResetEvent and AutoResetEvent in Thread[^],
Running exactly one job/thread/process in webservice that will never get terminated (asp.net)[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900