我怎样才能让我的闪屏根据文件下载时间开始和结束?
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();
在我的 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();