从后台工作者访问Windows控件 [英] Accessing a windows control from a backgroundworker

查看:115
本文介绍了从后台工作者访问Windows控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Notepad is a basic text-editing program and it's most commonly used to view or edit text files. A text file is a file type typically identified by the .txt file name extension.

推荐答案

您应该能够使用 Dispatcher [ ^ ]属性。使用Disptachers CheckAccess()方法检查如何访问控件。 (注意CheckAccess不会出现在intellisense中。)



这是一个示例,其中backgroundworker线程调用SetMsg函数来更新标签控件:



You should be able to use the Dispatcher[^] property on that. Use the Disptachers CheckAccess() method the check how to access the control. (note CheckAccess does not appear in intellisense.)

Here's a sample where the backgroundworker thread calls the SetMsg function to update the label control:

private void SetMsg(string txt, string fulltxt){
	if(lbl_info.Dispatcher.CheckAccess()){
		lbl_info.Content = txt;
		fullinfomessage = txt + "\r\n" + fulltxt;
	}				//end if
	else{
		lbl_info.Dispatcher.Invoke(new UpdateTxt(SetMsg), new object [] { txt, fulltxt });
	}				//end else
}

//the delegate is defined as follows: private delegate void UpdateTxt(string txt, string fulltxt);





希望这会有所帮助。



Hope this helps.


本文非常好地解释了这个主题。您应该使用CP上的搜索功能来查看问题的答案是否已经存在。干杯。



跨线程访问Windows窗体控件 [ ^ ]
This article explains this topic very well. You should use the search functionality on CP to see if the answers to your questions already exist. Cheers.

Accessing Windows Forms Controls across Threads[^]


这篇关于从后台工作者访问Windows控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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