2011-12-01 63 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); 

} 
相關問題