WPF/C# 中这种线程方式效率低下吗?

发布于 2024-10-30 03:09:46 字数 797 浏览 1 评论 0原文

这样做是否比设置后台工作线程效率低?因为它运行良好并且看起来更干净。在循环中,我调用 BeginInvoke 3x - 将行添加到主窗口数据网格,并更新进度条和消息。

public MyConstructor()
{
    InitializeComponent();

    ThreadStart start = delegate()
    {
        for (...)
        {
            ...
            MyWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                new Action(delegate()
                {
                    // Do something on the MyWindow thread.
                }
            ));
            ...
        }

        // Intensive loop now done and we close this processing window.
        this.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
            new Action(delegate()
            {
                this.Close();
            }
        ));
    }; // ThreadStart

    new Thread(start).Start();
}

Is doing this less efficient than setting up a background worker thread? Because it is working well and seems cleaner. In the loop I am calling BeginInvoke 3x - to add rows to main window datagrid, and to update the progress bar and message.

public MyConstructor()
{
    InitializeComponent();

    ThreadStart start = delegate()
    {
        for (...)
        {
            ...
            MyWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                new Action(delegate()
                {
                    // Do something on the MyWindow thread.
                }
            ));
            ...
        }

        // Intensive loop now done and we close this processing window.
        this.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
            new Action(delegate()
            {
                this.Close();
            }
        ));
    }; // ThreadStart

    new Thread(start).Start();
}

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

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

发布评论

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

评论(2

贱贱哒 2024-11-06 03:09:46

对我来说它看起来不太干净......
但除此之外,我认为没有理由不这样做。但我也没有看到不使用BackgroundWorker的原因:

private void BWDoWork(...)
{
    for (...)
    {
        // ...
        worker.ReportProgress(...);
    }
}

private void BWReportProgress(...)
{
    // do something on the MyWindow thread
}

private void BWCompleted(...)
{
    this.Close();
}

它对我来说看起来更干净,因为你在线程中完成的实际工作和UI更新之间有一点分离......

It doesn't exactly look clean to me...
But other than that, I see no reason for not doing it. But I also do not see a reason, for not using the BackgroundWorker:

private void BWDoWork(...)
{
    for (...)
    {
        // ...
        worker.ReportProgress(...);
    }
}

private void BWReportProgress(...)
{
    // do something on the MyWindow thread
}

private void BWCompleted(...)
{
    this.Close();
}

It looks cleaner to me, because you have a little separation of the real work that is done in the thread and the UI updating...

茶花眉 2024-11-06 03:09:46

没关系,但有几点:

  • 您可能应该在 Thread 对象上设置 IsBackground,这样它就不会导致您的应用程序在退出时挂起。
  • 如果这是一个短期运行的活动,那么您不应该创建新线程,而应使用 ThreadPool.QueueUserWorkItem 或 .NET4 上的新“任务”内容。
  • 如果您的后台线程(无论是池线程还是手动创建的)存在未处理的异常,应用程序将会失败,而您对此无能为力。像“任务”这样的东西处理得更好。

您实际上并没有非常清楚地限定“高效”,但BackgroundWorker通常是做这类事情的更好方法 - 如果没有别的办法,它将使用池线程,这比池线程便宜得多手动创建的线程。

It's OK-ish, but a few points:

  • You should probably be setting IsBackground on the Thread object, so that it doesn't cause your application to hang at exit.
  • If this is a short-running activity, then you shouldn't create a new thread, instead use ThreadPool.QueueUserWorkItem or the new 'Task' stuff on .NET4.
  • If there's an unhandled exception on your background thread (either pool or manually created), the app's going to fail with very little you can do about it. Things like 'Task' handled that better.

You don't really qualify 'efficient' very clearly, but BackgroundWorker is generally a better way of doing this sort of thing - if nothing else, it will use a pool thread, which is much cheaper than a manually created thread.

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