2013-01-24 72 views
1

我想使用異步並等待,而我的問題是它不會等待後臺進程完成。也許你想知道爲什麼我不能同步運行應用程序? 我想盡快完成我的任務的一部分,其餘的可以等待,如本例中所示。 謝謝你們的幫助! =)如何知道使用異步時Task或Void方法是否仍在運行

class Program 
{ 
    static void Main(string[] args) 
    { 
     Run(); 
     //Problem or Maybe Not? Needs this 
     //So that my application won't close immediately 
     Console.ReadLine(); 
    } 

    private async static void Run() 
    { 
     Task<bool> TBool = ProcessRecords(); 

     if (await TBool) 
     { 
      //Problem #1 This Line Doesn't Show 
      Console.WriteLine("End of Code"); 
      //SHould be safe to close the application by Now 
      //But goes through here not waiting for the return 
      //of the last process. 

      Environment.Exit(0); 

      //My temporary solution is to indicate a Task.Delay(80000) 
      //to make sure that all the logging in the background 
      //are all done. I dont know if there is a function that shows 
      //that there are task running on the background or any 
      //other workaroung will help. =) thanks 
     } 

    } 

    private async static Task<bool> ProcessRecords() 
    { 
     Task newTask = null; 
     //Loop through all the records and send 
     //all the records to MQ ASAP 
     for (int x = 0; x < 10; x++) 
     { 
      //I wont wait for this Task so 
      //I can send the next record 
      newTask = SendToMQ(x); 
     } 
     //I only need to wait for the last Task to 
     //indicate that I can exit the system 
     //as soon as it finish 
     //Problem #2 The Console.WriteLine doesnt show the last record. 
     await newTask; 
     return true; 
    } 

    private async static Task SendToMQ(int count) 
    { 
     //actual sending of message (Important) 
     await Task.Delay(1000); 
     //Process of Logging Connect to DB etc, (Not so Important, but takes most of the time) 
     await LoggingRecord(); 
     Console.Clear(); 
     Console.WriteLine("Done Processing " + count.ToString() + " records"); 
    } 

    //Logging of each record 
    private async static Task LoggingRecord() 
    { 
     await Task.Delay(5000); 
     //Problem #3 This Line Doesn't Show 
     Console.WriteLine("Last Log Finished"); 
    } 
} 
+0

你能不能設置一些布爾標誌或分配和檢查某些屬性..?當你通過代碼的時候會發生什麼,最後的日誌完成寫入控制檯主窗口..? – MethodMan

+0

我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

+0

顯然Logging沒有完成他的事情,但應用程序強制關閉整個應用程序,即使Logging尚未完成寫入,這就是爲什麼我給我的應用程序Task.Delay(8000)等待它登錄,但這不僅僅是好的編碼,也不是動態的。感謝所有的幫助=) –

回答

1

你應該用等待儘可能:

await Run(); 

然而,在這種情況下,你不能,那麼你必須使用等待

static void Main(string[] args) 
{ 
    Run().Wait(); 

    //Problem or Maybe Not? Needs this 
    //So that my application won't close immediately 
    //Console.ReadLine(); 
} 

// note the return type is Task 
private async static Task Run() 
{ 
... 
} 

在ProcessRecords()你有以下行 - 我不太清楚你的意思,所以我沒有解決它:

//Problem #2 The Console.WriteLine doesnt show the last record. 

上面打印出

Done Processing 9 records 
End of Code 
+0

謝謝,我是如此陷入使用void而不是它應該是任務,無效沒有Wait()。感謝您的新洞察=)。 –

+1

雖然這是一個適用於控制檯應用程序的正確解決方案,但在GUI應用程序或ASP.NET中使用Wait()會非常小心,但它幾乎總是會導致死鎖。 – svick

相關問題