GowthamanSS
GowthamanSS

Reputation: 1484

running threads in c# .net

Let us consider a method which changes the string contains value often . I need to create thread which runs for every 1 min for getting a values from a string .

Is it possible?

I have tried following code which sleeps the entire process other that particular thread:

System.Threading.Thread.Sleep(10000);

Upvotes: 1

Views: 1757

Answers (5)

sa_ddam213
sa_ddam213

Reputation: 43636

If you wand to run a threaded process at a defined period of time the System.Threading.Timer class will be perfect

var timer = new System.Threading.Timer((o) =>
{
    // do stuff every minute(60000ms)

}, null, 0, 60000);

However if you are updating any UI code from this thread dont forget to invoke back on the UI thread

WPF:

var timer = new System.Threading.Timer((o) =>
{
    Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate
    {
        // do stuff WPF UI safe
    });

}, null, 0, 60000);

Winform

var timer = new System.Threading.Timer((o) =>
{
    base.Invoke((Action)delegate
    {
        // do stuff Winforms UI safe
    });

}, null, 0, 60000);

Example:

private void StartUpdateTimer()
{
    var timer = new System.Threading.Timer((o) => 
    { 
        string ss = "gowtham " + DateTime.Now.ToString(); 
        Response.Write(ss); 
    }, null, 0,1000);
}

Upvotes: 5

ZombieSpy
ZombieSpy

Reputation: 1376

Use:

new Thread(delegate()
    {
         while(true)
         {
             // Do stuff
             Thread.sleep(60000);
         }
    }).Start();

60 000 miliseconds is a minute

Thread.sleep puts the current thread to sleep

Upvotes: 1

dognose
dognose

Reputation: 20909

Instead of starting a new thread every 60 seconds, use one thread, that sleeps after finishing until the next run. Something like this:

Class Main{

public void startObserverThread(){
   Thread t = new Thread(this.observerThread);
   t.start();
}

private DateTime lastRun = null;
public void observerThread(){
   if (lastRun == null || DateTime.Now.Subtract(lastRun).Seconds >= 60){
       lastRun = DateTime.Now;
       //do thread work.
   }else{
       //check every second, if time elapsed
       Thread.sleep(1000);
   }
}

}

Change the waiting time if it needs to be more accurate. The big advantage vs resheduling a new thread at the end of the thread is, that you dont need to take care about the execution time of the task itself. If it takes 10 seconds, the thread will sleep for like 50 seconds. if it takes 50 seconds, the thread only sleeps 10 seconds after completing the task work.

Upvotes: 0

Nahum
Nahum

Reputation: 7197

C# or more spesificly .NET supports MULTITHREADING.

when you use Thread.Sleep() it will disable the one thread that used Thread.Sleep()

here is an exmaple of using the 'TPL' to lunch athread that samples the string every 60 seconds

System.Threading.Tasks.Task.Factory.StartNew(
()=>
{
System.Threading.Thread.Sleep(60000);
 ReadString()}
)

you should keep in mind that you need to protect your string from RACE CONDITION

use C# lock for this

Upvotes: 0

Alexei Levenkov
Alexei Levenkov

Reputation: 100600

Sleep does not start new thread, it blocks current thread (in your case UI thread) for given number of milliseconds.

Based on your description you want to start new thread and can sleep in that thread. Also it may be easier to use timers. Complete sample and information on Thread object avaialbe in MSDN Thread article:

new Thread(ThreadFunction).Start(); 

Upvotes: 1

Related Questions