Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To create a foreground task?

I seem to fail to create a foreground task. my main thread is supppose to call another thread and then exit. the other thread suppose to run forever

void MainThreadMain()
{
    task_main = Task.Factory.StartNew(() => OtherThread()) ;
    return;
}

void OtherThread()
{
  while(true)
  {
     TellChuckNorrisJoke();
  }
}

how can I ensure task_main will continue running even that Main Thread is dead? I assumed il do:

task_main.IsBackgorund = false; 

but no such option :\ I can make my main thread to wait a signal from my other thread that it passed to Foreground mode. but thats plain silly.

like image 314
Nahum Avatar asked Mar 14 '12 11:03

Nahum


1 Answers

The obvious question is: why don't you run your work on the main thread?

Assuming this is not an option, you should use a Thread not a Task. Then you can set:

Thread.IsBackground = false;

This will prevent your application from terminating while the worker thread is running.

like image 188
Nick Butler Avatar answered Nov 14 '22 15:11

Nick Butler