zoukankan      html  css  js  c++  java
  • 解决vs2005"how to make crossthread calls"超简单实例

    在学习WebRequest Web Response对header操作时,涉及到异步操作,按实例运行出错,vs2005提示“how to make cross-thread calls...”,查了一下资料。将原来的代码
    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]);
                }

            }

                
                
                
            }

        
    }
  • 相关阅读:
    渗透测试学习 二十一、 JSP相关漏洞
    渗透测试学习 二十、 其他漏洞汇总之PHP相关漏洞
    渗透测试学习 十九、 XSS跨站脚本漏洞详解 续2
    渗透测试学习 十八、 XSS跨站脚本漏洞详解 续
    渗透测试学习 十七、 XSS跨站脚本漏洞详解
    渗透测试学习 十六、 常见编辑器漏洞解析
    渗透测试学习 十五、 文件上传&&解析漏洞
    渗透测试学习 十四、 脚本木马的制作与原理
    渗透测试学习 十三、 SQLmap使用详解
    渗透测试学习 十二、 其他注入漏洞汇总 续
  • 原文地址:https://www.cnblogs.com/meta/p/1090251.html
Copyright © 2011-2022 走看看