简体   繁体   中英

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

In my form1 constructor i have:

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

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

This is the fileDownloadRadar method:

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

The way it's working now the splash screen wait 3 seconds close and then the form is loading.

But i want that the splash screen will start when the download file start and to to be closed when the download completed and then the form to be loaded.

The method FinishWebRequest is where the file downloaded. Like completed download.

My problem is how to calculate the time or how to make that the splash screen will start with the file download and close when finished ?

The splash screen and the download will have to be run in different threads. You can either create a new thread or you can switch to using an asynchronous download method.

Either way, instead of a splash_flag boolean, I'd use an AutoResetEvent (it could retain the name I guess). It would start off in the Reset state. The FinishWebRequest() method would call splash_flag.Set() (or whatever you decided to name it). This would trigger the splash screen to close. You'd replace your while loop with something simple like:

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

splash_flag.WaitOne() will hang until FinishWebRequest() calls splash_flag.Set() .

EDIT: Since your splash screen has to be animated. You'll have to just poll for completion. Having a loop in the UI thread will cause the form to become unresponsive.

You could just create a timer to do the polling:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM