您的位置:首页 > 编程语言 > C#

C#中Invoke的用法

2012-05-18 23:42 323 查看
在做一个测试Tcp连接的DEMO,想用TcpClient.BeginConnect异步调用,在回调方法里更新winform.TextBox的值输出结果,直接使用TextBox.Text出错(线程间操作无效,不是从创建控件XX的线程访问他),想过使用winform.Invoke不过一直想Inovke将Tcp连接进行封装委托,原来应该将更新界面的方法进行委托。
最后代码
public partial class MainForm : Form

{

private string _serverAddress = string.Empty;

private int _port = 80;
delegate void SetTextCallback(string text);
public MainForm()

{

InitializeComponent();

}
private void connectButton_Click(object sender,
EventArgs e)

{

if (string.IsNullOrEmpty(this.serverAddressTextBox.Text))

{

this.tipsTextBox.Text = "请输入服务器IP地址\r\n";

}

if (string.IsNullOrEmpty(this.portTextBox.Text))

{

this.tipsTextBox.Text="请输入端口号\r\n";

}

else

{

_serverAddress = this.serverAddressTextBox.Text;//连接IP地址

_port = Convert.ToInt32(this.portTextBox.Text);//端口号

this.tipsTextBox.Text = this.tipsTextBox.Text + "开始建立连接.....\r\n";
this.tipsTextBox.Text
= this.tipsTextBox.Text + "请等待.....\r\n";
TcpClient
tcpClient = new TcpClient();

//开启异步TCP连接

IAsyncResult asr=tcpClient.BeginConnect(_serverAddress, _port, new AsyncCallback(RequestCallBack), tcpClient);

//try

//{

//
tcpClient.EndConnect(asr);

//}

//catch (System.Exception ex)

//{

//
this.tipsTextBox.Text = this.tipsTextBox.Text + "错误:" + ex.Message + "\r\n";

//}

//finally

//{

//
this.tipsTextBox.Text = this.tipsTextBox.Text + "操作是否完成:" + asr.IsCompleted + "\r\n";

//
this.tipsTextBox.Text = this.tipsTextBox.Text + "连接结果:" + ((TcpClient)asr.AsyncState).Connected + "\r\n";

//}

}

}
/// <summary>

/// 异步回调函数

/// </summary>

/// <param name="asr"></param>

private void RequestCallBack(IAsyncResult asr)

{

TcpClient client = (TcpClient)asr.AsyncState;

SetText("连接结果:"+client.Connected.ToString());

if (client.Connected)

{

client.EndConnect(asr);

}
}
private void SetText(string text)

{

// InvokeRequired需要比较调用线程ID和创建线程ID

// 如果它们不相同则返回true

if (this.tipsTextBox.InvokeRequired)

{

this.Invoke(new SetTextCallback(SetText), new object[] { text });

}

else

{

this.tipsTextBox.Text = this.tipsTextBox.Text + "\n" + text;

}

}
private void MainForm_Load(object sender, EventArgs
e)

{

// Control.CheckForIllegalCrossThreadCalls = false;

}

}

转自:http://blog.3snews.net/html/30/34530-27563.html

在多线程编程中,我们经常要在工作线程中去更新界面显示,而在多线程中直接调用界面控件的方法是错误的做法,Invoke 和 BeginInvoke 就是为了解决这个问题而出现的,使你在多线程中安全的更新界面显示。

正确的做法是将工作线程中涉及更新界面的代码封装为一个方法,通过 Invoke 或者 BeginInvoke 去调用,两者的区别就是一个导致工作线程等待,而另外一个则不会。

而所谓的“一面响应操作,一面添加节点”永远只能是相对的,使 UI 线程的负担不至于太大而已,因为界面的正确更新始终要通过 UI 线程去做,我们要做的事情是在工作线程中包揽大部分的运算,而将对纯粹的界面更新放到 UI 线程中去做,这样也就达到了减轻 UI 线程负担的目的了。

举个简单例子说明下使用方法,比如你在启动一个线程,在线程的方法中想更新窗体中的一个TextBox..

using System.Threading;

//启动一个线程

Thread thread=new Thread(new ThreadStart(DoWork));

thread.Start();

//线程方法

private void DoWork()

{

this.TextBox1.Text="我是一个文本框";

}

如果你像上面操作,在VS2005或2008里是会有异常的...

正确的做法是用Invoke\BeginInvoke

using System.Threading;

namespace test

{

public partial class Form1 : Form

{

public delegate void MyInvoke(string str1,string str2);

public Form1()

{

InitializeComponent();

}

public void DoWork()

{

MyInvoke mi = new MyInvoke(UpdateForm);

this.BeginInvoke(mi, new Object[] {"我是文本框","haha"});

}

public void UpdateForm(string param1,string parm2)

{

this.textBox1.Text = param1+parm2;

}

private void button1_Click(object sender, EventArgs e)

{

Thread thread = new Thread(new ThreadStart(DoWork));

thread.Start();

}

}

}

注意代理的使用!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: