Skip to main content
 首页 » 编程设计

c#之Whenany() 在任务列表中退出程序

2024年02月06日13duanxz

我有一个控制台应用程序,我在其中使用 TPL,当我在任务中点击 whenany 时它会退出。我是线程的新手,有人可以指导我正确的方向吗(我单独执行了 RunJob,它没有异常(exception))。 .所以我不确定如何从这里进行调试。

代码如下:

// Program.cs 
static void Main(string[] args) 
{ 
    TaskHelper helper = new TaskHelper(); 
    helper.StartProcessing(); 
} 
 
// TaskHelper Class 
public async void StartProcessing() 
{ 
    var tasks = new List<Task<bool>>(); 
    int taskNum = _queueList.Count < maxThreads ? _queueList.Count : maxThreads; 
    for (int i = 0; i < taskNum; i++) 
    { 
        UCMDo doObj; 
        if (_taskQueue.TryDequeue(out doObj)) 
        { 
            tasks.Add(RunOps(doObj)); 
        } 
    } 
 
    while (tasks.Count > 0) 
    { 
        try 
        { 
            // Program exits here when its hitting WhenAny line 
            var t = await Task.WhenAny(tasks); 
            tasks.Remove(t); 
            await t; 
        } 
        catch (OperationCanceledException) 
        { 
        } 
        catch (Exception exc) { } 
        finally 
        { 
           // add to tasks, and RunOps 
        } 
    } 
} 
 
async Task<bool> RunOps(UCMDo doJ) 
{ 
    var result = await Task.Run(() => UCMFactory.RunJob(_job, doJ)); 
    return result; 
} 

请您参考如下方法:

发生的事情是您调用 StartProcessing 而不等待它(您不能,因为它是 async void),因此程序到达 Main< 的末尾 并在 StartProcessing 中的操作仍在运行时结束。

StartProcessing 应该返回一个任务,您应该等待该任务完成。这通常是通过等待任务(即 await helper.StartProcessing())来完成的,但是由于您不能在 Main 中使用 await,所以您应该这样做同步的(尽管这是唯一可以接受的地方):

static void Main(string[] args) 
{ 
    TaskHelper helper = new TaskHelper(); 
    helper.StartProcessingAsync().Wait(); 
} 

正如 Servy 正确指出的那样,一个更健壮和生产就绪的解决方案是使用某种消息循环。一个例子是 Stephen Cleary's AsyncContext :

static void Main(string[] args) 
{ 
    TaskHelper helper = new TaskHelper(); 
    AsyncContext.Run(() => helper.StartProcessingAsync()); 
} 

注意事项:

  • async void 永远不应在 UI 事件处理程序之外使用
  • async 方法通常应命名为 XAsync(即 StartProcessingAsync)