简体   繁体   English

多线程应用程序

[英]Multithreaded Applications

I have been reading the articles on MSDN, but my mind is dead (this usually happens when I read MSDN (No offense MSDN, but your articles confuse me at times.)), and I'm trying to do some "background work" in my app, but not sure how. 我一直在阅读有关MSDN的文章,但我的思绪已经死了(这通常发生在我阅读MSDN时(没有攻击MSDN,但你的文章有时会让我感到困惑。)),而我正在尝试做一些“背景工作”在我的应用程序中,但不知道如何。 It's just a single method. 这只是一种方法。 But the application hangs, and I have to wait up to 1 - 3 minutes for it to become ...unhanged? 但应用程序挂起,我必须等待1到3分钟才能变成......没变?

Are there any simple examples that are laying 'round online somewhere that I can have a look at/play around with? 是否有任何简单的例子可以在网上铺设,我可以看看/玩耍?

Thank you all 谢谢你们

Jon Skeet wrote a nice introduction to multithreading in .NET that you might read. Jon Skeet写了一篇关于.NET多线程的精彩介绍 ,你可能会读到。 It also covers threading in WinForms . 它还包括WinForms中的线程 It may go among the lines: 它可能属于以下几行:

public partial class Form1 : Form
{
    private BackgroundWorker _worker;

    public Form1()
    {
        InitializeComponent();
        _worker = new BackgroundWorker();
        _worker.DoWork += (sender, e) =>
        {
            // do some work here and calculate a result
            e.Result = "This is the result of the calculation";
        };
        _worker.RunWorkerCompleted += (sender, e) =>
        {
            // the background work completed, we may no 
            // present the result to the GUI if no exception
            // was thrown in the DoWork method
            if (e.Error != null)
            {
                label1.Text = (string)e.Result;
            }
        };
        _worker.RunWorkerAsync();
    }
}

Darin already told you the theory. 达林已经告诉过你这个理论。

But you should check out the static ThreadPool.QueueUserWorkItem method. 但是你应该检查静态ThreadPool.QueueUserWorkItem方法。 It's more convenient. 它更方便。

There is already this decent question with lots of links to articles that are more easily digestable than MSDN. 已经有这个体面的问题,有很多链接到比MSDN更容易消化的文章。

Jon Skeet's article is the easiest and probably the most comprehensive to get started with, and Joe Duffy's series goes into a lot of depth. Jon Skeet的文章是最容易开始的文章,也可能是最全面的文章,而Joe Duffy的文章则深入探讨。 Browsing the C# & Multithreading tags in Stackoverflow also gives you some good answers. 浏览Stackoverflow中的C#和多线程标记也可以为您提供一些很好的答案。

You might find avoiding the BackgroundWorker the fastest way to get going, and simply use an Invoke: 您可能会发现避免使用BackgroundWorker是最快的方法,只需使用Invoke:

void ButtonClick(object sender,EventArgs e)
{
    Thread thread = new Thread(Worker);
    thread.Start();
}

void Worker()
{
    if (InvokeRequired)
    {
        Invoke(new Action(Worker));
        return;
    }

    MyLabel.Text = "Done item x";
}

Some people like using the BackgroundWorker on Stackoverflow, others don't (I'm in camp number 2). 有些人喜欢在Stackoverflow上使用BackgroundWorker,有些人则不喜欢(我在2号营地)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM