簡體   English   中英

如何根據文件下載時間使啟動畫面啟動和結束?

[英]How can i make that my splash screen will start and finish according to the file downloading time?

在我的form1構造函數中,我有:

while (splash_flag == true)
            {
                splash.Show();
                Thread.Sleep(3000);
                splash_flag = false;
            }
            if (splash_flag == false)
            {

                splash.Close();
            }
            fileDownloadRadar(remote_image_on_server,combinedTemp);

這是fileDownloadRadar方法:

HttpWebRequest request;
        void fileDownloadRadar(string uri, string fileName)
        {
            request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
            request.CookieContainer = new CookieContainer();
            request.AllowAutoRedirect = true;

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.ContentType == "")
            {
                Logger.Write("ContentType is Empty download was not fine !!!!!");
            }
            if ((response.StatusCode == HttpStatusCode.OK ||
                response.StatusCode == HttpStatusCode.Moved ||
                response.StatusCode == HttpStatusCode.Redirect) &&
                response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase))
            {
                Logger.Write("ContentType is not empty meaning download is fine");
                using (Stream inputStream = response.GetResponseStream())
                using (Stream outputStream = File.OpenWrite(fileName))
                {
                    byte[] buffer = new byte[4096];
                    int bytesRead;
                    do
                    {
                        bytesRead = inputStream.Read(buffer, 0, buffer.Length);
                        outputStream.Write(buffer, 0, bytesRead);
                    } while (bytesRead != 0);

                }
                FinishWebRequest();
            }
            else
            {
                timer1.Stop();
                timer3.Start();
            }
        }

現在,啟動屏幕的工作方式將關閉,等待3秒鍾,然后加載表單。

但是我希望啟動屏幕將在下載文件開始時啟動,並在下載完成然后要加載的表單時關閉。

方法FinishWebRequest是文件下載的位置。 喜歡完成下載。

我的問題是如何計算時間或如何使啟動屏幕從文件下載開始並在完成時關閉?

初始屏幕和下載將必須在不同的線程中運行。 您可以創建一個新線程,也可以切換到使用異步下載方法。

無論哪種方式,我都將使用AutoResetEvent而不是splash_flag布爾值(它可以保留我猜的名稱)。 它將在復位狀態下開始。 FinishWebRequest()方法將調用splash_flag.Set() (或您決定命名的任何名稱)。 這將觸發啟動屏幕關閉。 您將用以下類似的簡單內容替換while循環:

splash.Show();
splash_flag.WaitOne();
splash.Close();

splash_flag.WaitOne()將掛起,直到FinishWebRequest()調用splash_flag.Set()為止。

編輯:由於您的啟動畫面必須進行動畫處理。 您只需要輪詢完成即可。 UI線程中存在循環將導致表單無響應。

您可以創建一個計時器來執行輪詢:

splash.Show();
// Poll every 100ms. Change as desired.
var timer = new System.Timers.Timer(100) { AutoReset = true };
timer.Elapsed += (s, e) =>
    {
        if (splash_flag.WaitOne(0)) // Check for signal and return without blocking
        {
            // Use Invoke() so you only deal with the form in the UI thread
            splash.Invoke(new Action(() => { splash.Close(); }));
            timer.Stop();
        }
    }
timer.Start();

暫無
暫無

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

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