相关疑难解决方法(0)

在单独的线程池中执行某些后台任务,以避免主线程中执行的关键任务出现饥饿现象

在单独的线程池中执行某些后台任务(不是线程),以避免主线程中执行的关键任务(不是线程)饥饿

我们的场景

我们托管一个大容量的 WCF Web 服务,其逻辑上具有以下代码:

void WcfApiMethod()
{
   // logic

   // invoke other tasks which are critical
   var mainTask = Task.Factory.StartNew(() => { /* important task */ });
   mainTask.Wait();

   // invoke background task which is not critical
   var backgroundTask = Task.Factory.StartNew(() => { /* some low-priority background action (not entirely async) */ });
   // no need to wait, as this task is best effort. Fire and forget

   // other logic
}

// other APIs
Run Code Online (Sandbox Code Playgroud)

现在的问题是,在某些情况下,低优先级后台任务可能需要更长的时间(约 30 秒),例如,检测 SQL 连接问题、DB …

c# multithreading task task-parallel-library async-await

4
推荐指数
1
解决办法
1072
查看次数