代码是这样的public delegate void RtnDelegate(string com, int address, int cid, int rtn, int len, string data);private void Rtn_DataRecived(string com, int address, int cid, int rtn, int len, string data)
{
this.BeginInvoke(new RtnDelegate(ShowDataRecived), com, address, cid, rtn, len, data);
}private void ShowDataRecived(string com, int address, int cid, int rtn, int len, string data)
{ txtShow.Text = data; }Control.BeginInvoke在Winform中这样调用this.BeginInvoke(new RtnDelegate(ShowDataRecived), com, address, cid, rtn, len, data);但是换成Web中,怎么调用BeginInvoke我想到一个办法RtnDelegate rd = ShowDataRecived;
rd.BeginInvoke(com, address, cid, rtn, len, data, null, null);但是如果这样写的话就txtShow.Text就显示不出任何值。我断点调试,txtShow.Text明明有值,不晓得是什么原因
{
this.BeginInvoke(new RtnDelegate(ShowDataRecived), com, address, cid, rtn, len, data);
}private void ShowDataRecived(string com, int address, int cid, int rtn, int len, string data)
{ txtShow.Text = data; }Control.BeginInvoke在Winform中这样调用this.BeginInvoke(new RtnDelegate(ShowDataRecived), com, address, cid, rtn, len, data);但是换成Web中,怎么调用BeginInvoke我想到一个办法RtnDelegate rd = ShowDataRecived;
rd.BeginInvoke(com, address, cid, rtn, len, data, null, null);但是如果这样写的话就txtShow.Text就显示不出任何值。我断点调试,txtShow.Text明明有值,不晓得是什么原因
这是异步调用,跨线程的
换成Web中
应该不要代理吧,直接赋值txtShow.Text试试
private void Rtn_DataRecived(string com, int address, int cid, int rtn, int len, string data)
{
txtShow.Text = data;
}
不能这样做
这样的话报错,线程间操作无效: 从不是创建控件“txtShow”的线程访问它。
但是应该怎么写才回让text有值?
参考asp.net多线程开发,http://dev.yesky.com/msdn/404/2387904.shtml
本身asp.net提供自己的页内多线程方式
那你说应该用什么方法?
我知道
如果我在Rtn_DataRecived里面直接写ShowDataRecived(com,address,cid,rtn,len,data);
结果是一样的
但是碰到要用web做,是没有办法
只能请教了
是不容易,winform中用的是Control.BeginInvoke
没办法,我在web里面只好搞Delegate.BeginInvoke
在winform应用程序中form1_Load中加了一句
Control.CheckForIllegalCrossThreadCalls = false;winform就可以显示我想晓得,在web里面的解决方法应该怎样
我把
RtnDelegate rd = ShowDataRecived;
rd.BeginInvoke(com, address, cid, rtn, len, data, null, null);
改为
RtnDelegate rd = new RtnDelegate(ShowDataRecived);
rd(com, address, cid, rtn, len, data);还是不行~~~
要是在winform中,这个问题就不是问题勒。直接在winform应用程序中form1_Load中加了一句
Control.CheckForIllegalCrossThreadCalls = false;在webform就是稀奇社~~
所以呀要怎么做呀~各位帮忙看下啦
{
DelegateName asy = new DelegateName(MethodName);
int i=5;
int j=9;
IAsyncResult ia=asy.BeginInvoke(i,out j,null,null);
string r = asy.EndInvoke(out j, ia);
txtShow.Text = "异步完成了!i的值是" + j.ToString()+"r的值是:"+r;
} private string MethodName(int num, out int ret)
{
ret = num;
return "HelloWorld";
}
private delegate string DelegateName(int num, out int ret); private void CallBackMethod(IAsyncResult ar)
{
int num;
((DelegateName)ar.AsyncState).EndInvoke(out num,ar);
}
这个可以解决问题的,我测试了的