如何使跨线程调用一个ToolStripStatusLabel? [英] How do I make cross-threaded calls to a ToolStripStatusLabel?

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

问题描述

我倾向于使用一个StatusStrip中在我的大多数应用程序简单的状态更新和偶尔进度条的底部。

不过,这似乎ToolStripStatusLabels不从控制继承,所以他们没有.Invoke,或.InvokeRequired。因此,如何将我线程安全的,打个电话来改变它的Text属性?

codeD答案为后人和其他人前来查询:

 动作<字符串>测试=(文字)=>
{
如果(this._statusStrip.InvokeRequired)this._statusStrip.Invoke(
新MethodInvoker(()=> this._lblStatus.Text =文本));
否则this._lblStatus.Text =文本;
};
 

 私人无效TestInvoker(文本字符串)
{
如果(this._statusStrip.InvokeRequired)
            this._statusStrip.Invoke(
新MethodInvoker(()=> this._lblStatus.Text =文本));
否则this._lblStatus.Text =文本;
}
 

解决方案

这是一个很好的问题!

ToolStripStatusLabel 没有从控制继承,包含的ToolStrip 呢!使用含的ToolStrip 的调用,使对呼叫 ToolStripStatusLabel

这是因为ToolStrip的手动处理它的组成位的图中,许多相同的方式,WPF管理的绘制的所有的其分量的比特,而不会产生一个单独的手柄为每一个。因为它很容易忘记这是非常有用的,每个控制有一个相关的 HANDLE ,系统只有有限数量的那些抛出,如:

(我以前也遇到了这一点。我在侧边栏提到的<一个href="http://stackoverflow.com/questions/714666/is-it-appropriate-to-extend-control-to-provide-consistently-safe-invoke-begininvo">another问题,比如我应该更新文本,以反映我最近的了解。)

I tend to use a StatusStrip at the bottom of most of my applications for simple status updates and occasionally a progress bar.

However, it appears ToolStripStatusLabels do not inherit from control, so they have no .Invoke, or .InvokeRequired. So how would I thread-safe make a call to change it's text property?

Coded answers for posterity and others that come searching:

Action<string> test=(text) =>
			{
				if (this._statusStrip.InvokeRequired) this._statusStrip.Invoke(
			   new MethodInvoker(() => this._lblStatus.Text = text));
				else this._lblStatus.Text = text;
			};

or

private void TestInvoker(string text)
	{
		if (this._statusStrip.InvokeRequired) 
            this._statusStrip.Invoke(
				   new MethodInvoker(() => this._lblStatus.Text = text));
		else this._lblStatus.Text = text;
	}

解决方案

This is a good question!

While ToolStripStatusLabel does not inherit from control, the containing ToolStrip does! Use the containing ToolStrip's Invoke to make calls against the ToolStripStatusLabel.

This is because the ToolStrip manually handles the drawing of its component bits, much the same way that WPF manages the drawing of all of its component bits, without generating a separate handle for each one. This is useful because it's easy to forget that every Control has an associated HANDLE and the system only has a finite number of those to dish out, e.g..

(I've run into this before also. I mentioned it in a sidebar on another question, for example. I should update that text to reflect my more recent understanding.)

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

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