How To create a foreground task?

只愿长相守 提交于 2019-12-10 13:59:05

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/9701368/how-to-create-a-foreground-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!