简体   繁体   English

如何根据文件下载时间使启动画面启动和结束?

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

In my form1 constructor i have: 在我的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);

This is the fileDownloadRadar method: 这是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();
            }
        }

The way it's working now the splash screen wait 3 seconds close and then the form is loading. 现在,启动屏幕的工作方式将关闭,等待3秒钟,然后加载表单。

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. 方法FinishWebRequest是文件下载的位置。 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). 无论哪种方式,我都将使用AutoResetEvent而不是splash_flag布尔值(它可以保留我猜的名称)。 It would start off in the Reset state. 它将在复位状态下开始。 The FinishWebRequest() method would call splash_flag.Set() (or whatever you decided to name it). FinishWebRequest()方法将调用splash_flag.Set() (或您决定命名的任何名称)。 This would trigger the splash screen to close. 这将触发启动屏幕关闭。 You'd replace your while loop with something simple like: 您将用以下类似的简单内容替换while循环:

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

splash_flag.WaitOne() will hang until FinishWebRequest() calls splash_flag.Set() . splash_flag.WaitOne()将挂起,直到FinishWebRequest()调用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. UI线程中存在循环将导致表单无响应。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使启动画面变为全屏? - How can I make splash screen to full screen? 我的启动画面结束得太早了。 如何在 Android 上手动隐藏启动画面? - My splash screen ends too early. How can I manually hide a splash screen on Android? 第一次完成后如何再次启动协程? - How can I start a coroutine again when the first time finish? 每次下载文件时,如何使文件名增加一? - How can I make the file names to be raise by one each time a file is downloading? 我怎样才能制作出一个出色的启动屏幕,同时执行两个操作? - How can I make a good splash screen that does two actions at once? 在使用progressBar加载所有数据之前,如何使用启动屏幕? - How can i use my splash screen until all data is loaded using a progressBar? 闪屏后黑屏。 如何在我的应用程序启动时显示图像并稍后在某个时间点将其删除? - Black screen after splash screen. How can I display an image when my application launches and remove it later at a certain point? 如何在初始屏幕上更改Textblock值? - How do I change the Textblock value on my splash screen? 如何在Windows应用程序的初始屏幕中制作进度指示器的动画 - How can I animate a progress indicator in a splash screen of a windows application 如何在C#中添加启动画面? - How I can add a Splash Screen in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM