如何创建前台任务? [英] How To create a foreground task?

查看:118
本文介绍了如何创建前台任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法创建一个前台任务。
我的主线程supppose调用另一个线程,然后退出。
其他线程想运行永远

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();
  }
}



我怎么能保证task_main将继续运行,即使是主主题是死了吗?
我认为金正日做的:

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.

推荐答案

最明显的问题是:你为什么不运行在主线程工作

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.

这篇关于如何创建前台任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆