2011-12-01 79 views
0

在C#中,如何让程序只处理一件事?我一直在做一个补丁系统,我认为我的代码都是正确的,但是无法测试,因为很多函数都试图在需要按顺序处理时一次处理所有的函数。在开始尝试处理所有事情之前,程序甚至不会让显示器显示出来。因为它们都没有返回一个值,所以主函数的所有函数都被设置为void。我考虑过在循环中使用返回值,以确保程序在继续前先完成这一步骤,但它仍然使程序的问题甚至不会显示出来,直到所有事情都完成处理,并假设它显示一切进展。任何建议的提示?C#在推进之前完成程序完成过程

编辑:我不知道该代码后什么,所以即时通讯发布的所有主要功能:

public void DSP_Load(object sender, EventArgs e) 
    { 
     if (v1 >= v2) 
     { 
      File_Progress_Title.Text = "100%"; 
      Update_Status.Text = "Divine Shadows is currently up to date."; 
      Application.DoEvents(); 
      Process.Start("Divine Shadows.exe"); 
      Close(); 
     } 
     else 
     { 
      Update_Status.Text = "Checking For Updates..."; 
      Application.DoEvents(); 

      if (!Directory.Exists(tempFilePath)) 
      { 
       Directory.CreateDirectory(tempFilePath); 
      } 

      using (SqlCon = new MySqlConnection(connString)) 
      { 
       SqlCon.Open(); 
       string command = "SELECT * FROM version where version > '" + v1 + "' ORDER BY version LIMIT 1"; 
       MySqlCommand GetLatestVersion = new MySqlCommand(command, SqlCon); 

       using (MySqlDataReader DR = GetLatestVersion.ExecuteReader()) 
       { 
        while(DR.Read()) 
        { 
         do 
         { 
          string LatestVersion = Convert.ToString(DR.GetValue(1)); 
          string WebURL = Convert.ToString(DR.GetValue(2)); 
          update.DownloadFileAsync(new Uri(WebURL), tempFilePath + "patch" + LatestVersion + ".zip"); 
          update.DownloadProgressChanged += new DownloadProgressChangedEventHandler(download); 
          update.DownloadFileCompleted += new AsyncCompletedEventHandler(extration); 
          Application.Restart(); 
         } 
         while (v1 < v2); 
         Process.Start("Divine Shadows.exe"); 
         Close(); 
        } 
       } 
      } 
     } 
    } 

    public void download(object sender, DownloadProgressChangedEventArgs e) 
    { 
     if (v1 >= v2) 
     { 
      File_Progress_Title.Text = "100%"; 
      Update_Status.Text = "Divine Shadows is currently up to date."; 
      Application.DoEvents(); 
      Process.Start("Divine Shadows.exe"); 
      Close(); 
     } 
     else 
     { 
      Update_Status.Text = "Downloading Updates..."; 
      Application.DoEvents(); 
      File_Progress_Display.Value = e.ProgressPercentage; 
      File_Progress_Title.Text = Convert.ToString(e.ProgressPercentage) + "%"; 
     } 
    } 

    public void extration(object sender, AsyncCompletedEventArgs e) 
    { 
     if (v1 >= v2) 
     { 
      File_Progress_Title.Text = "100%"; 
      Update_Status.Text = "Divine Shadows is currently up to date."; 
      Application.DoEvents(); 
      Process.Start("Divine Shadows.exe"); 
      Close(); 
     } 
     else 
     { 
      Update_Status.Text = "Installing Updates, Please Wait..."; 
      Application.DoEvents(); 
      UnzipFile(extactFile, extractLocation); 
     } 
    } 

    public static void UnzipFile(string extactFile, string extractLocation) 
    { 
     try 
     { 
      FastZip fastZip = new FastZip(); 
      fastZip.CreateEmptyDirectories = false; 
      fastZip.ExtractZip(extactFile, extractLocation, FastZip.Overwrite.Always, null, null, null, false); 
     } 
     catch (Exception ex) 
     { 
      throw new Exception("Error unzipping file \"" + extactFile + "\"", ex); 
     } 
     File.Delete(extactFile); 
    } 
+1

有些代码可能的帮助。 –

+0

您是否使用任何并行库方法进行处理? –

+1

鉴于你对代码的唯一引用是你“使用WebClient()函数”(它是一个类,而不是一个函数),这将需要心灵的力量。发布一些代码。 – spender

回答

0

你的问题不是Web客户端()特定的,其对你的应用程序是如何一起工作线程。

通常,winforms应用程序有一个GUI线程。此线程用于执行您的方法并更新用户界面。如果你开始一个长期的过程,gui线程会被锁定,直到操作完成。这就是为什么你的显示器没有显示。

您可以通过执行BackgroundWorker来解决该问题。在该网站上,您还可以找到如何实施它的示例。让BackgroundWorker执行修补过程并使用BackgroundWorker.RunWorkerAsync()方法中的事件更新GUI。

+0

谢谢,这实际上可能会有所帮助。我会尽快尝试,看看它是否有效 – dpg199200

+0

如果您需要关于如何实现BackgroundWorker的进一步说明,请告诉我。 – Grrbrr404

+0

好吧,我想我明白了该怎么做,但如果你不介意,请你看看脚本,看看我做对了吗? http://pastebin.com/Kqby8i8t 我的主机现在正在运行,所以我无法准确测试脚本。 – dpg199200

0

如果您使用的是c#4或更新版本,则可以使用任务并行库异步执行任务,从而在下载任务时留下您的UI响应。首先你需要一个参考:

using System.Threading.Tasks; 

而且一些代码:

public void YourMainFunction() 
{ 
    var urls = new List<string>(); 
    urls.Add("http://google.com"); 
    urls.Add("http://yahoo.com"); 

    foreach(var url in urls) 
    { 
     Task.Factory.StartNew<DownloadResult>(() => 
      DownloadIt(url)) 
      .ContinueWith(WorkDone, TaskScheduler.FromCurrentSynchronizationContext()); 
    } 
} 

private class DownloadResult 
{ 
    public string Url {get; set;} 
    public string Result {get; set;} 
} 

private DownloadResult DownloadIt(string url) 
{ 
    var downloadResult = new DownloadResult{ Url = url }; 
    var client = new WebClient(); 
    downloadResult.Result = client.DownloadString(url); 
    return downloadResult; 
} 

private void WorkDone(Task<DownloadResult> task) 
{ 
    if(task.IsFaulted) 
    { 
     //An exception was thrown 
     MessageBox.Show(task.Exception.ToString()); 
     return; 
    } 

    //Everything went well 
    var downloadResult = task.Result; 
    //Here you can update your UI to reflect progress. 
    MessageBox.Show(downloadResult.Result); 

} 
相关问题