解决 vs2005编译错误提示 "how to make cross-thread calls"超简单实例
2008-03-04 15:07
453 查看
这两天在学习WebRequest Web Response对header操作时,涉及到异步操作,按实例运行出错,vs2005提示“how to make cross-thread calls...”,查了一下资料。将原来的代码
google_ad_client = "pub-6924533005275861";
google_ad_slot = "4211741364";
google_ad_width = 336;
google_ad_height = 280;
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
namespace Accessing_the_internet
...{
public partial class Form1 : Form
...{
public Form1()
...{
InitializeComponent();
WebRequest wrq = WebRequest.Create("http://www.123de6.cn");
wrq.BeginGetResponse(new AsyncCallback(OnResponse), wrq);
}
protected void OnResponse(IAsyncResult ar)
...{
WebRequest wrq=(WebRequest)ar.AsyncState;
WebResponse wrs=wrq.EndGetResponse(ar);
//read the response
WebHeaderCollection whc = wrs.Headers;
MethodInvoker mi=new MethodInvoker()
for (int i = 0; i < whc.Count; i++)
...{
listBox1.Items.Add("Header"+ whc.GetKey(i)+" : " +whc[i]);
}
}
}
}
改为这样,就可以实现对主线程listbox的操作。
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
namespace Accessing_the_internet
...{
public partial class Form1 : Form
...{
WebHeaderCollection whc;
public Form1()
...{
InitializeComponent();
WebRequest wrq = WebRequest.Create("http://www.123de6.cn");
wrq.BeginGetResponse(new AsyncCallback(OnResponse), wrq);
}
protected void OnResponse(IAsyncResult ar)
...{
WebRequest wrq=(WebRequest)ar.AsyncState;
WebResponse wrs=wrq.EndGetResponse(ar);
//read the response
whc = wrs.Headers;
MethodInvoker mi = new MethodInvoker(controla);
this.BeginInvoke(mi);
}
private void controla()
...{
for (int i = 0; i < whc.Count; i++)
...{
listBox1.Items.Add("Header" + whc.GetKey(i) + " : " + whc[i]);
}
}
}
}
google_ad_client = "pub-6924533005275861";
google_ad_slot = "4211741364";
google_ad_width = 336;
google_ad_height = 280;
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
namespace Accessing_the_internet
...{
public partial class Form1 : Form
...{
public Form1()
...{
InitializeComponent();
WebRequest wrq = WebRequest.Create("http://www.123de6.cn");
wrq.BeginGetResponse(new AsyncCallback(OnResponse), wrq);
}
protected void OnResponse(IAsyncResult ar)
...{
WebRequest wrq=(WebRequest)ar.AsyncState;
WebResponse wrs=wrq.EndGetResponse(ar);
//read the response
WebHeaderCollection whc = wrs.Headers;
MethodInvoker mi=new MethodInvoker()
for (int i = 0; i < whc.Count; i++)
...{
listBox1.Items.Add("Header"+ whc.GetKey(i)+" : " +whc[i]);
}
}
}
}
改为这样,就可以实现对主线程listbox的操作。
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
namespace Accessing_the_internet
...{
public partial class Form1 : Form
...{
WebHeaderCollection whc;
public Form1()
...{
InitializeComponent();
WebRequest wrq = WebRequest.Create("http://www.123de6.cn");
wrq.BeginGetResponse(new AsyncCallback(OnResponse), wrq);
}
protected void OnResponse(IAsyncResult ar)
...{
WebRequest wrq=(WebRequest)ar.AsyncState;
WebResponse wrs=wrq.EndGetResponse(ar);
//read the response
whc = wrs.Headers;
MethodInvoker mi = new MethodInvoker(controla);
this.BeginInvoke(mi);
}
private void controla()
...{
for (int i = 0; i < whc.Count; i++)
...{
listBox1.Items.Add("Header" + whc.GetKey(i) + " : " + whc[i]);
}
}
}
}
相关文章推荐
- 解决vs2005"how to make cross-thread calls"超简单实例
- Qt提示 "Could not connect to AppWrapper, Please make sure TCP port 12005 is not blocked"解决方法
- Qt提示 "Could not connect to AppWrapper, Please make sure TCP port 12005 is not blocked"解决方法
- 编译3.10内核 出现错误 “undefined reference to...." 解决方法
- QT中添加Q_OBJECT时编译提示obj : error LNK2001: 无法解析的外部符号 "public: virtual struct QMetaObject const错误解决方法
- Web开发敏捷之道-应用Rails进行敏捷Web开发-第三版 92页 错误提示 syntax error, unexpected ')' ..._cart" , :id => product } do ).to_s) 解决办法
- 编译WINCE时出再“don’t know how to make “/””错误 的解决
- Qt提示 "Could not connect to AppWrapper, Please make sure TCP port 12005 is not blocked"解决方法
- 编译内核是出现No rule to make target `firmware/sd8686_helper.bin', 错误的解决
- Qt提示 "Could not connect to AppWrapper, Please make sure TCP port 12005 is not blocked"解决方法
- Please read "Security" section of the manual to find out how to run mysqld as root!错误解决
- 重装PB后第一次编译BSP出错的解决办法(don't know how to make)
- 如何解决sql server2005的“不允许远程连接”错误.(how to resolve the error: "SQL Server does not allow remote connections" for SQL Server2005)
- Eclipse编译JNI时提示"Fatal: Unable to open makefile"解决办法
- "The connection to adb is down, and a severe error has occured"错误解决方法
- 解决"T-SQL无法附加到进程"错误的补充,T-SQLcan not attach to process
- 解决 Linux下oracle导入(imp)导出(exp)出现"failed to open ...for reader/wirte" 错误
- 使用ASIHTTPRequest 编译提示找不到"libxml/HTMLparser.h"的解决方法
- 交叉编译libupnp,make时提示libtool: eval: line 944: syntax error near unexpected token `|'的解决办法
- 解决gcc 编译时报错"undefined reference to 'main'" 问题