大致思路如下:1、首先你需要提供一个函数来修改控件属性,例如: public void SetText( string strValue ) { yourTextBox.Text = strValue; }2、在你的线程函数中,需要用Invoke来调用,例如: // in your thread function MethodInvoker mi = new MethodInvoker( SetText ); this.Invoke( mi, new object[]{ "test" } );
winform delegate void SetTextCallback(string text); Thread m_thread; private void button1_Click(object sender, EventArgs e) { m_thread = new Thread(new ThreadStart(this.CallThread)); m_thread.Start(); } private void CallThread() { this.setText("This text was set safely."); } private void setText(string text) { if (this.textBox1.InvokeRequired && this.listBox1.InvokeRequired) { SetTextCallback d = new SetTextCallback(setText); this.Invoke(d, new object[] { text }); } else { this.textBox1.Text = text; } }
winfx(WPF) Thread m_thread; delegate void SetTextCallback(string text); void OnClick(object sender, RoutedEventArgs e) { m_thread = new Thread(new ThreadStart(CallThread)); m_thread.SetApartmentState(ApartmentState.STA); m_thread.Start(); } private void CallThread() { for (int i = 1; i <= 5000; i++) { SetText(i.ToString()); } } private void SetText(string text) { if (!textBox1.Dispatcher.CheckAccess()) { this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new SetTextCallback(SetText), text); } else { textBox1.Text = text; } }
if (this.textBox1.InvokeRequired && this.listBox1.InvokeRequired) 有错误,应该是 if (this.textBox1.InvokeRequired) 原先写的是两个控件,忘了删除了,不好意思
因为控件都属于FORM的主线程,
而我重新开辟的线城当然不能直接访问主线城中的变量了啊
http://blog.csdn.net/knight94/archive/2006/03/21/631238.aspx
to 如果要设置控件值怎摸写啊参看我上面给的文章,就给出例子了。//--------------------------ThreadFun Class------------------------------------//-----------------------------------------------------------------------------//---File:clsThreadFun.cs//---Description:This class demonstrates how to use thread class.//---Author:Knight//---Date:Mar.21, 2006//-----------------------------------------------------------------------------//------------------------{ ThreadFun Class }----------------------------------using System; namespace CSNewTest{ /// <summary> /// Summary description for clsThreadFun. /// </summary> public class clsThreadFun { private string strUserName; public string UserName { get{ return strUserName;} set{ strUserName = value; } } public clsThreadFun( string sUserName) { // // TODO: Add constructor logic here // strUserName = sUserName; } /// <summary> /// Thread interface function /// </summary> public void ThreadFun() { //Detail thread handle } }} 接着,是调用代码: clsThreadFun myThreadFun = new clsThreadFun( "Test" ); //Set parameter through “myThreadFun.UserName” Thread myThread = new Thread( new ThreadStart( myThreadFun.ThreadFun ) ); myThread.Start(); 你这是什么啊??
哪有啊,晕!
http://blog.csdn.net/knight94/archive/2006/03/16/626584.aspx
public void SetText( string strValue )
{
yourTextBox.Text = strValue;
}2、在你的线程函数中,需要用Invoke来调用,例如:
// in your thread function
MethodInvoker mi = new MethodInvoker( SetText );
this.Invoke( mi, new object[]{ "test" } );
delegate void SetTextCallback(string text);
Thread m_thread;
private void button1_Click(object sender, EventArgs e)
{
m_thread = new Thread(new ThreadStart(this.CallThread));
m_thread.Start();
}
private void CallThread()
{
this.setText("This text was set safely.");
}
private void setText(string text)
{
if (this.textBox1.InvokeRequired && this.listBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(setText);
this.Invoke(d, new object[] { text });
}
else
{
this.textBox1.Text = text;
}
}
Thread m_thread;
delegate void SetTextCallback(string text);
void OnClick(object sender, RoutedEventArgs e)
{
m_thread = new Thread(new ThreadStart(CallThread));
m_thread.SetApartmentState(ApartmentState.STA);
m_thread.Start();
}
private void CallThread()
{
for (int i = 1; i <= 5000; i++)
{
SetText(i.ToString());
}
}
private void SetText(string text)
{
if (!textBox1.Dispatcher.CheckAccess())
{
this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
new SetTextCallback(SetText), text);
}
else
{
textBox1.Text = text;
} }
有错误,应该是
if (this.textBox1.InvokeRequired)
原先写的是两个控件,忘了删除了,不好意思
http://blog.csdn.net/tjvictor/archive/2006/06/21/818312.aspx