zoukankan      html  css  js  c++  java
  • net 2.0 中如何在线程引发的事件中控制forms controls

    这个是安全方法.

     private void Form1_Load(object sender, EventArgs e)
            
    {
                Post p 
    = new Post();
                p.OnPost 
    += new Post.OnPostHandler(p_Post);
                t 
    = new Thread(new ThreadStart(p.Go));
            }


            
    void p_Post(int m, string result)
            
    {
                
    if (this.textBox1.InvokeRequired)
                
    {
                    Post.OnPostHandler d 
    = new Post.OnPostHandler(p_Post);
                    
    this.Invoke(d,new object[]{m,result});
                   
                }

                
    else
                
    {
                    
    this.textBox1.Text = m.ToString();
                }

               
            }

    class Post
        
    {
           
    public Post()
           
    {
           }

            
    public delegate void OnPostHandler(int m,string result);
            
    public event OnPostHandler OnPost;
            
    private static int count = 0;
            
    public void Go()
            
    {

                WebClient webClient 
    = new WebClient();
                webClient.Headers.Add(
    "Accept""image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,*/*");
                webClient.Headers.Add(
    "Referer""http://www.xmsell.com/zhuanti/jjworld/match_show.asp?id=3");
                webClient.Headers.Add(
    "Accept-Language""zh-cn");
                webClient.Headers.Add(
    "Content-Type""application/x-www-form-urlencoded");
                webClient.Headers.Add(
    "Accept-Encoding""gzip, deflate");
                webClient.Headers.Add(
    "User-Agent""Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1;)");
                
    string postStr = "Grade=%CD%B6%CB%FB%D2%BB%C6%B1%A3%A1%A3%A1%A3%A1&Submit=+%CD%B6+%C6%B1+";
                
    byte[] data = Encoding.ASCII.GetBytes(postStr);
                
    while (true)
                
    {
                    count
    ++;
                    
    byte[] responseArray = webClient.UploadData("http://www.xmsell.com/zhuanti/jjworld/match_show.asp?action=add&id=3""POST", data);
                    
    // Thread.Sleep(500);
                    string responseStr = Encoding.GetEncoding("Gb2312").GetString(responseArray);

                    
    if (OnPost != null)
                    
    {
                        OnPost(count, responseStr);
                    }

                }

            }

        }
  • 相关阅读:
    springboot---Shiro
    spring MVC 使用 modelAndView.setViewName("forward:*.action") 发送重定向
    数据库负载均衡、读写分离技术
    redis详解
    分布式--锁
    springboot---数据整合篇
    hadoop安装单机
    NIO系列之MINA
    JAVA中IO流模型BIO,NIO,AIO
    CPU-bound(计算密集型) 和I/O bound(I/O密集型)
  • 原文地址:https://www.cnblogs.com/kokoliu/p/939427.html
Copyright © 2011-2022 走看看