簡體   English   中英

UI線程被阻止,但僅在第一次調用Webclient異步方法時

[英]Ui thread blocked but only on 1st call to Webclient asynch methods

根據客戶的請求,我編寫了一個從webclient繼承的通信類。 他們希望能夠“將自定義Windows表單作為進度條傳遞”,而不是我創建了一個實現3個屬性的界面。 無論如何,我遇到的問題是應用程序啟動了,我單擊開始按鈕可以這么說,第一次執行此操作時,ui線程被凍結,幾秒鍾后它解凍,進度條和數據開始下降。

但是,在此初始凍結之后,隨后按下啟動按鈕的任何操作都可以正常工作,並且不會阻塞線程的任何想法嗎?

這是表格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;
    }
}

修復它,我將webclient類的Proxy屬性設置為null,這似乎可以解決它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM