如何使用与Winform的多线程? [英] How to use multithreading with Winform?

查看:159
本文介绍了如何使用与Winform的多线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与多线程是一个新手。我有一个WinForm有一个标签和一个进度条。

I'm a newbie with multithreading. I have a winform that have a label and a progress bar.

我想告诉的处理结果。首先,我用 Application.DoEvents()评判。但我发现的形式冻结。

I wanna show the processing result. Firstly, I use Application.DoEvents() mothod. But I find that the form is freezing.

后来我读到的多线程在MSDN一些文章。

Then I read some article about multithreading at MSDN.

其次,我用一个BackgroundWorker来做到这一点。

Secondly, I use a BackgroundWorker to do it.

this.bwForm.DoWork += (o, arg) => { DoConvert(); };
this.bwForm.RunWorkerAsync();

的形式不冻结,我可以处理时,拖动/ drog。不幸的是,引发InvalidOperationException。所以我必须用这个。 Control.CheckForIllegalCrossThreadCalls = FALSE; 我保证,这不是一个最终的解决方案。

The form don't freeze that I can drag/drog when processing. Unfortunately, throw an InvalidOperationException. So I have to use this. Control.CheckForIllegalCrossThreadCalls = false; I ensure that's not a final solution.

你有一些建议的话,专家?

Do you have some suggest to do so, Experts?

编辑: 当我打电话的ListView扔InvalidOperationException异常。这code是在DoWork的()。

When I call listview throw InvalidOperationException. This code is in DoWork().

          foreach (ListViewItem item in this.listView1.Items)
            {
                //........some operation
                 lbFilesCount.Text = string.Format("{0} files", listView1.Items.Count);
                 progressBar1.Value++;
            }

EDIT2: 我使用委托并调用中的DoWork(),它不乱扔exeption。但形式再次冻结。如何做到这一点的形式可能是投掷的/ drappable在处理?

I use delegate and invoke in DoWork() and it don't throw exeption. But the form freezing again. How to do that the form could be droppable/drappable while processing?

推荐答案

您只能设置从UI线程(在WinForm的)进度条的用户控件的属性。 与您的BackgroundWorker做到这一点最简单的方法是使用ProgressChanged事件:

You can only set progress bar user control properties from the UI thread (the WinForm one). The easiest way to do it with your BackgroundWorker is to use the ProgressChanged event:

private BackgroundWorker bwForm;
private ProgressBar progressBar;

在WinForm的构造函数:

In WinForm constructor:

this.progressBar = new ProgressBar();
this.progressBar.Maximum = 100;
this.bwForm = new BackgroundWorker();
this.bwForm.DoWork += new DoWorkEventHandler(this.BwForm_DoWork);
this.bwForm.ProgressChanged += new ProgressChangedEventHandler(this.BwForm_ProgressChanged);
this.bwForm.RunWorkerAsync();

...

void BwForm_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker bgw = sender as BackgroundWorker;
    // Your DoConvert code here
    // ...          
    int percent = 0;
    bgw.ReportProgress(percent);
    // ...
}

void BwForm_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    this.progressBar.Value = e.ProgressPercentage;
}

在这里看到:<一href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx">http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

修改

所以,我不明白你的问题,我想应该是展示工作中的进度条的进展。 如果是关于工作中使用e.Result在BwForm_DoWork事件的结果。 添加一个新的事件处理程序完成的事件和管理的结果:

So I didn't understand your question, I thought it was about showing progress bar progression during work. If it's about result of work use e.Result in the BwForm_DoWork event. Add a new event handler for completed event and manage result:

this.bwForm.RunWorkerCompleted += new RunWorkerCompletedEventHandler(this.BwForm_RunWorkerCompleted);

...

private void BwForm_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    YourResultStruct result = e.Result as YourResultStruct;
    if (e.Error != null && result != null)
    {
        // Handle result here
    }
}

这篇关于如何使用与Winform的多线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆