唐朝程序员

我来自唐朝

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);
                }

            }

        }

    }

posted on 2007-10-27 10:36  唐朝程序员  阅读(259)  评论(0编辑  收藏  举报

导航