我正在使用 Mango,所以我有后台任务。 如何创建处理数据的线程?(超时) 我需要创建线程来检查传入的消息。我需要为其设置超时。线程必须在所有页面的后台运行。
我想要类似的东西
public startApp() {
Thread th = new Thread(function_to_check,5)//last is timeout
}
最佳答案
放一个DispatcherTimer在您的 App.xaml.cs 文件中。
...
private static readonly DispatcherTimer myTimer = new DispatcherTimer();
myTimer .Interval = TimeSpan.FromSeconds(5);
myTimer .Tick += myTimerTick;
myTimer .Start();
...
private void myTimerTick(object sender, EventArgs e)
{
//do something here
}
编辑:
这使您可以在一个位置上做您想做的事。如果您需要每个页面的自定义逻辑,您可以创建一个 switch
语句来检查当前页面
或者您可以使用创建此计时器的页面覆盖 PhoneApplicationPage
,并覆盖每个子页面中的 Tick
函数。
关于c# - 如何在超时的WP7中创建后台线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6603971/