在启动C#WPF中的另一个线程时,如何避免UI阻止?

发布于 2025-02-08 08:01:37 字数 728 浏览 2 评论 0原文

我正在尝试执行线程而不阻止UI,我已经使用了此代码,但是当我执行应用程序时,它不会执行线程,单击dobutton事件后,没有显示任何

public void DoThread()
{
    BackgroundWorker worker = new BackgroundWorker();
    worker.DoWork += MyFunctionDoThread;
    var frame = new DispatcherFrame();
    worker.RunWorkerCompleted += (sender, args) => {
        frame.Continue = false;
    };
    worker.RunWorkerAsync();
    Dispatcher.PushFrame(frame);
}

private void Dobutton_Click(object sender, RoutedEventArgs e)
{
    DoThread(); // Process will be executed
}

public void MyFunctionDoThread()
{
    // Some Tasks
    ProcessStartInfo startInfo = new ProcessStartInfo();
    Process.Start(startInfo);
    // ...
}

内容可以在不阻止UI的情况下执行任务(线程)吗?

I'm trying to execute a thread without blocking UI , I've used this code but when I execute my application , it won't execute the thread and nothing is shown after clicking on DoButton event

public void DoThread()
{
    BackgroundWorker worker = new BackgroundWorker();
    worker.DoWork += MyFunctionDoThread;
    var frame = new DispatcherFrame();
    worker.RunWorkerCompleted += (sender, args) => {
        frame.Continue = false;
    };
    worker.RunWorkerAsync();
    Dispatcher.PushFrame(frame);
}

private void Dobutton_Click(object sender, RoutedEventArgs e)
{
    DoThread(); // Process will be executed
}

public void MyFunctionDoThread()
{
    // Some Tasks
    ProcessStartInfo startInfo = new ProcessStartInfo();
    Process.Start(startInfo);
    // ...
}

How I can perform a task ( thread ) without blocking the UI?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一花一树开 2025-02-15 08:01:37

您应该真正使用任务/异步/等待任何背景工作。 背景工作者相当古老。

public async void Dobutton_Click(object sender, RoutedEventArgs e)
{
    try{
        var result = await Task.Run(MyFunctionDoThread);
        // Update the UI, or otherwise deal with the result
    }
    catch{
        // deal with failures, like showing a dialog to the user
    }
}

我该如何使用它,等待返回任务操作的要求

需要用async标记的方法,它不需要需要 返回任务的方法。返回任务是指南,以便呼叫者可以处理任何失败。但是,对于诸如按钮事件处理程序之类的事情,您处于生产线的末端,没有其他人可以处理任何故障,因此您应该确保尝试/捕获。

You should really use Task/async/await for any background work. BackgroundWorker is rather old.

public async void Dobutton_Click(object sender, RoutedEventArgs e)
{
    try{
        var result = await Task.Run(MyFunctionDoThread);
        // Update the UI, or otherwise deal with the result
    }
    catch{
        // deal with failures, like showing a dialog to the user
    }
}

how can I use it , the await require to return task action

await requires the method to be marked with async, it does not require the method to return a task. It is a guideline to return a task, so that the caller can deal with any failures. But for things like button event handlers you are at the end of the line, there is no one else to deal with any failure, so you should instead make sure you do it yourself with a try/catch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文