DispatcherTimer没有在task内工作
本文关键字:工作 task DispatcherTimer | 更新日期: 2023-09-27 18:18:01
效果如下
public MainWindow()
{
CheckCrawlURLs.func_StartCrawlingWaitingUrls();
}
但是在
下面不起作用public MainWindow()
{
Task.Factory.StartNew(() =>
{
CheckCrawlURLs.func_StartCrawlingWaitingUrls();
});
}
下面是两个例子中执行的类
public static class CheckCrawlURLs
{
public static void func_StartCrawlingWaitingUrls()
{
PublicStaticFunctions.AddMsgToEvents("Checking waiting crawling urls process started");
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(func_CheckWaitingUrls);
dispatcherTimer.Interval = new TimeSpan(0, 0, 5);
dispatcherTimer.Start();
}
private static void func_CheckWaitingUrls(object sender, EventArgs e)
{
PublicStaticFunctions.AddMsgToMoreCommonEvents("Checking waiting to crawl urls...");
List<string> lstStartCrawling = CrawlURLDB.func_ReturnNextCrawlLinks();
PublicStaticFunctions.AddMsgToMoreCommonEvents("Number of links to start crawlin: " + lstStartCrawling.Count);
}
}
我的问题是这里的逻辑是什么?
在第二个任务工厂启动时,func_CheckWaitingUrls没有滴答。
顾名思义,DispatcherTimer
是基于WPF调度程序的。显然,这只适用于GUI线程。
看看System.Threading.Timer
,或者在一个新线程上启动计时器回调。