简体   繁体   English

具有耗时任务的 ContentDialog 不允许我在完成之前使用该应用程序

[英]ContentDialog with time-consuming task does not allow me to use the App until it is finished

Is there a way to use the app while the task is still running?有没有办法在任务仍在运行时使用该应用程序?

With my current code, the app is unresponsive while the task executed from the ContentDialog does not finish.使用我当前的代码,应用程序没有响应,而从 ContentDialog 执行的任务没有完成。

private async void FooContentDialog(object sender, EventArgs e)
{
    ContentDialog fooDialog = new ContentDialog
    {
    // XamlRoot must be set in the case of a ContentDialog running in a Desktop app
    XamlRoot = this.Content.XamlRoot,
    Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style, /* fade effect*/
    Title = "Run time-consuming fooTask?",
    PrimaryButtonText = "Yes",
    CloseButtonText = "No",
    DefaultButton = ContentDialogButton.Primary,
    Content = "An extremely time-consuming task will run. Sit back and have a coffee in the meantime."
    };
    ContentDialogResult result = await fooDialog.ShowAsync();
    
    if (result == ContentDialogResult.None)
    {            
        return;
    }
    
    //time-consuming fooTask
    fooTask();
}

If the function you are invoking ( fooTask ) is a void returning function, then it means that it is doing CPU bound work.如果您正在调用的 function ( fooTask ) 是返回 function 的void值,那么这意味着它正在进行 CPU 绑定工作。

This kind of functions blocks the executing thread, which in your case is the UI thread.这种函数会阻塞执行线程,在您的例子中是 UI 线程。 Because you invoked the function from the UI thread, it's the UI thread which is in charge to execute the whole code of the function and while doing so it is completely stuck, unable to do any other activity: that's why your user interface is frozen and not responsive, basically the UI thread is unable to respond to user interactions and to move or resize the user interface accordingly to user clicks.因为您从 UI 线程调用了 function,所以它是负责执行 function 的全部代码的 UI 线程,并且在这样做时它完全卡住了,无法进行任何其他活动:这就是您的用户界面被冻结的原因,并且没有响应,基本上 UI 线程无法响应用户交互,也无法根据用户点击移动或调整用户界面的大小。

To summarize, your problem is the following:总而言之,您的问题如下:

I want to run some heavy CPU-bound work from the UI thread, but I don't want to freeze the user interface while doing that我想从 UI 线程运行一些繁重的 CPU 绑定工作,但我不想在这样做时冻结用户界面

The common solution to this problem is invoking the CPU-bound method by using Task.Run .此问题的常见解决方案是使用Task.Run调用受 CPU 限制的方法。 Your code will be changed this way:您的代码将以这种方式更改:

private async void FooContentDialog(object sender, EventArgs e)
{
    // previous code omitted for brevity
    
    await Task.Run(() => 
    {
      //executes the time-consuming fooTask
      fooTask();
    });

    // keep on working in the UI thread
}

By doing so, you are asking the .NET thread pool to execute the time consuming CPU-bound work for you and you are asynchronously waiting for the task to be completed (notice that this way you are not blocking the UI thread, which is free to keep on responding to user interactions).通过这样做,您要求 .NET 线程池为您执行耗时的 CPU 绑定工作,并且您正在异步等待任务完成(请注意,这样您不会阻塞 UI 线程,它可以自由地继续响应用户交互)。

Notice that, with a very few exceptions, this is the only proper usage of the Task.Run method.请注意,除了极少数例外,这是Task.Run方法的唯一正确用法

If you want to learn more about the usage of Task.Run to execute CPU-bound work from the UI thread I highly recommend to read a series of articles availabe on the Stephen Cleary blog.如果您想详细了解如何使用Task.Run从 UI 线程执行 CPU 密集型工作,我强烈建议您阅读 Stephen Cleary 博客上的一系列文章。 You can start from the first article of the series.您可以从本系列的第一篇文章开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MainForm中耗时的任务,另一种形式的进度条 - Time-consuming task in MainForm, progress bar in another form C#中的“lock”语句是否耗时? - Is the "lock" statement in C# time-consuming? 与HttpClient,WebApi和WCF的耗时通信 - Time-consuming communication with HttpClient, WebApi and WCF 关于C#中耗时过程的建议 - Advices on time-consuming procedure in C# 等待消息,同时执行耗时的任务 - Wait Message Whilst Performing Time-Consuming Tasks 如何在页面上异步加载耗时的方法并在加载时显示加载 gif? - How can I load time-consuming methods on a page asynchronously and displaying a loading gif while it loads? 构造函数初始值设定项不允许我使用“ this” - Constructor initializer does not allow me to use 'this' 以高效且省时的方式构建,启动ASP.NET Web应用程序并运行Microsoft CodedUI测试 - Build, Launch ASP.NET Web application and run Microsoft CodedUI tests in efficient and less time-consuming manner 运行耗时的任务+更新Winform - Running time consuming task + update winform 如何等待任务在 C# 中完成? - How do I wait until Task is finished in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM