控制在同一个线程中 [英] Control in the same Thread

查看:88
本文介绍了控制在同一个线程中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Heloo,



我有一个应用程序,我想使用请稍候......加载窗口。

我有一个例子,它的工作,但我不能执行我的代码,因为不在同一个线程。



一个例子,



在加载事件中的winForm1中我有:



  string  NumeClientCautat = txtCautaDupaNume.Text; 
this .sClienti.DataSource = SetariAmanet.TotiClienti(NumeClientCautat);
InformatiiDespreClientContracte();





要使用请稍候......我必须将代码重写为类似的东西:



 winProgresIncarcare.AsyncProcessDelegate method =  delegate  
{
string NumeClientCautat = txtCautaDupaNume.Text;
this .sClienti.DataSource = SetariAmanet.TotiClienti(NumeClientCautat);
InformatiiDespreClientContracte();
};
base .StartWait(method);





他给出我错了:



控件不在同一个线程中....

  this  .sClienti.DataSource = SetariAmanet.TotiClienti(NumeClientCautat); 
InformatiiDespreClientContracte();

解决方案

您只能从创建它们的线程 - UI线程访问控件。 />
为了从不同的线程对它们做任何事情,你必须使用Invoke。我使用的代码是:

  private   void  AddNewTab( string  tabName)
{
if (InvokeRequired)
{
Invoke( new MethodInvoker( delegate {AddNewTab(tabName);})) ;
}
else
{
TabPage tp = new TabPage(tabName);
myTabControl.TabPages.Add(tp);
}
}
私有 void ShowProgress( int %)
{
if (InvokeRequired)
{
Invoke ( new MethodInvoker( delegate {ShowProgress(percent);}));
}
else
{
myProgressBar.Value = percent;
}
}

如果您使用的是BackgroundWorker而不是aysnc委托,则无需进行调用。

BackgroundWorker自行提供跨越边界的状态报告。

1.将BackgroundWorker.WorkerReportsProgress属性设置为true。

2.在工作例程调用中( (BackgroundWorker)sender).ReportProgress()。

3.在BackgroundWorker.ProgressChanged事件处理程序中更改UI(包括进度条)。


Hi


控件有Invoke方法,您可以这样称呼:



 label1.Invoke(  new 操作(()= >  
{
// 在这里做gui work
}));





你可以在内部完成需要GUI线程的工作,比如更新标签等。

Heloo,

I have an application and i want to use a "Please wait...loading" window.
I have an example, its work, but i cant execute my code because is not in the same thread.

An example,

In a winForm1 in the load event i have:

string NumeClientCautat = txtCautaDupaNume.Text;
this.sClienti.DataSource = SetariAmanet.TotiClienti(NumeClientCautat);
InformatiiDespreClientContracte();



To use "Please wait..." i have to rewrite the code to something like that:

winProgresIncarcare.AsyncProcessDelegate method = delegate
			{
string NumeClientCautat = txtCautaDupaNume.Text;
this.sClienti.DataSource = SetariAmanet.TotiClienti(NumeClientCautat);
InformatiiDespreClientContracte();
			};
			base.StartWait(method);



He gives me an error:

the control is not in the same thread....

this.sClienti.DataSource = SetariAmanet.TotiClienti(NumeClientCautat);
InformatiiDespreClientContracte();

解决方案

You can only access controls from the thread that created them - the UI thread.
In order to do anything with them from a different thread, you have to use Invoke. The code I use is:

private void AddNewTab(string tabName)
    {
    if (InvokeRequired)
        {
        Invoke(new MethodInvoker(delegate { AddNewTab(tabName); }));
        }
    else
        {
        TabPage tp = new TabPage(tabName);
        myTabControl.TabPages.Add(tp);
        }
    }
private void ShowProgress(int percent)
    {
    if (InvokeRequired)
        {
        Invoke(new MethodInvoker(delegate { ShowProgress(percent); }));
        }
    else
        {
        myProgressBar.Value = percent;
        }
    }

If instead of an aysnc delegate you used a BackgroundWorker, there''s no need for you to do the invoking.
The BackgroundWorker offers status reporting across thead borders on its own.
1. Set the BackgroundWorker.WorkerReportsProgress property to true.
2. Within the worker routine call ((BackgroundWorker)sender).ReportProgress().
3. Do the UI changes (including progress bar) within BackgroundWorker.ProgressChanged event handler.


Hi
Controls have Invoke method which you can call it like this:

label1.Invoke(new Action(() =>
{
    // do here gui work
}));



Inside you can do work that requires the GUI thread like updating a label or so.


这篇关于控制在同一个线程中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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