2012-06-21 46 views
0

根据客户端的请求我写了一个继承自webclient的通信类。他们希望能够“将自定义窗体作为进度条传递”,而不是创建一个实现3个属性的接口。无论如何,我遇到的问题是应用程序启动,我点击开始按钮说话,第一次我这样做时,UI线程被冻结,几秒钟后解冻,进度条和数据开始下降。Ui线程被阻塞,但仅在第一次调用Webclient异步方法时

虽然初步冻结后,任何后续按下的开始按钮工作完美,不要阻止线程的任何想法?

以下是代码形式1

private void btnSubmit_Click(object sender, EventArgs e) 
    { 
     txtResponse.Text = ""; 
     progressForm = new ProgressFormTest(); 
     myCommunication = new CommunicationClient(progressForm); 
     myCommunication.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_RequestComplete); 
     // myCommunication.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged); 
     myCommunication.Timeout += new EventHandler(wc_TimeOut); 
     myCommunication.Cancelled += new EventHandler(myCommunication_Cancelled); 


     progressForm.Show(); 
     myCommunication.TimeoutValue = (int)numConnectionTimeout.Value * 1000; 

     myCommunication.DownloadStringAsync(new Uri(txtUrl.Text)); 


    } 

这里的相关片在通信类

public class CommunicationClient:WebClient 
    { 
    private int step = 1000; 
    private long bytesReceived; 
    private long bytesSent; 
    private Status status; 
    private System.Timers.Timer _timer; 
    private IProgress myProgressForm; 

    /// <summary> 
    /// Sets the timeout value in milliseconds 
    /// </summary> 
    public int TimeoutValue { get; set; } 
    public int TimeElapsed { get; set; } 
    public long BytesReceived 
    { 
     get 
     { 
      return bytesReceived; 
     } 
    } 
    public long BytesSent 
    { 
     get 
     { 
      return bytesSent; 
     } 
    } 

    public event EventHandler Timeout; 
    public event EventHandler Cancelled; 

    public CommunicationClient(IProgress progressForm) 
    { 
     myProgressForm = progressForm; 
     _timer = new System.Timers.Timer(step); 
     _timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); 
    } 





    protected override void OnDownloadStringCompleted(DownloadStringCompletedEventArgs e) 
    { 
     _timer.Stop(); 
     if (status == Status.Completed) 
     { 
      myProgressForm.PercentComplete = 100; 
      base.OnDownloadStringCompleted(e); 
     } 
    } 





    protected override void OnDownloadProgressChanged(DownloadProgressChangedEventArgs e) 
    { 
     bytesReceived = e.BytesReceived; 
     myProgressForm.BytesReceived = bytesReceived; 
     if (e.TotalBytesToReceive == -1) 
      myProgressForm.PercentComplete = calculateFakePercentage(); 
     else 
      myProgressForm.PercentComplete = e.ProgressPercentage; 
     base.OnDownloadProgressChanged(e); 
    } 

    protected virtual void OnTimeout(EventArgs e) 
    { 
     if (Timeout != null) 
     { 
      CancelAsync(); 
      this.Dispose(); 
      Timeout(this, e); 
     } 
    } 

    protected virtual void OnCancelled(EventArgs e) 
    { 
     if (Cancelled != null) 
     { 
      this.Dispose(); 
      Cancelled(this, e); 
     } 
    } 

    /// <summary> 
    /// Cancels a pending asynchronous operation and raises the Cancelled Event 
    /// </summary> 
    /// <param name="Event">Set to true to raise the event</param> 
    public void CancelAsync(bool Event) 
    { 
     CancelAsync(); 
     if (Event) 
     { 
      status = Status.Cancelled; 
      OnCancelled(new EventArgs()); 
     } 
    } 

    private void initialize() 
    { 
     status = Status.Completed; 
     TimeElapsed = 0; 
     _timer.Start(); 
    } 



    new public void DownloadStringAsync(Uri url) 
    { 
     initialize(); 
     base.DownloadStringAsync(url); 
    } 

    private void timer_Elapsed(object sender, ElapsedEventArgs e) 
    { 
     TimeElapsed += step; 

     if (TimeElapsed >= TimeoutValue) 
     { 
      _timer.Stop(); 
      status = Status.Timeout; 
      OnTimeout(e); 
     } 
    }  

    //used when the website in question doesnt provide the content length 
    private int calculateFakePercentage() 
    { 
     return (int)bytesReceived * 100/60000 ; 
    }   
} 

这里是简单接口IProgressForm

public interface IProgress 
{ 
    int PercentComplete 
    { 
     get; 
     set; 
    } 
    long BytesReceived 
    { 
     get; 
     set; 
    } 
    long BytesSent 
    { 
     get; 
     set; 
    } 
} 

回答

0

固定它,我将webclient类的Proxy属性设置为null,似乎可以修复它