2016-11-19 54 views
1

我有一個運行後檢查的安裝。什麼我希望做的是調出功能運行函數直到成功

private void ApacheTest() 
{ 
    if(!File.Exists(HTTPD_PATH)) 
    { 
     amountdl.Text = "Apache Not Found! Installation Corrupt!"; 
    } 
    else 
    { 
     StartApacheServer(); 
    } 
    if(ApacheRunning() == false) 
    { 
     amountdl.Text = "Apache Is Starting"; 
    } 
    else 
    { 
     amountdl.Text = "Apache Started"; 
    } 
} 

我希望它繼續運行這個功能我希望發生的是,直到ApacheRunning() == true。這在C#中可能嗎?

+1

'ServiceController'有一個內置的['WaitForStatus'](https://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.waitforstatus(V = vs.110)。 aspx)如果你想啓動Apache服務。 –

+0

謝謝!我剛纔所做的是在'if(ApacheRunning()== false){ApacheTest(); }'但我有一種感覺'WaitForStatus'可能是運行這個的首選方式。 –

回答

-1

嘗試:

private void ApacheTest() 
{ 
    if(!File.Exists(HTTPD_PATH)) 
    { 
     amountdl.Text = "Apache Not Found! Installation Corrupt!"; 
    } 
    else 
    { 
     StartApacheServer(); 
    } 

    amountdl.Text = "Apache Is Starting"; 
    while(ApacheRunning() == false) 
    { 
     Task.Delay(1000); 
    } 

    amountdl.Text = "Apache Started";   
} 

在這裏,我用Task.Delay(1000)檢查每個秒的服務器狀態。

+1

可能想要做一個'Task.Delay'。 –

+0

@GillBates謝謝。好點 – Marusyk

+0

我已經使用了您的方法和Sahuagin的組合,現在我正在試圖決定將要發佈的人作爲我選擇的答案。我與他的主要事情是,我喜歡他的退出方法沒有找到實際的文件。 –

1

使用一個while循環,但也有一些其他的變化。

private void ApacheTest() { 
    if (!File.Exists(HTTPD_PATH)) { 
     amountdl.Text = "Apache Not Found! Installation Corrupt!"; 
     return; 
    } 

    amountdl.Text = "Apache Is Starting";  
    StartApacheServer(); 

    while (ApacheRunning() == false) { 
     // spin 
    } 

    amountdl.Text = "Apache Started"; 
} 

如果「找不到Apache」而不是繼續(return語句),則應該退出函數。

說「Apache正在啓動」儘快,並且不需要一遍又一遍地設置它。

+0

錯誤退出返回的目的是什麼?還是C#的新手,所以我討厭不知道自己在做什麼,但我注意到我在PHP中使用的很多練習在C#中都不起作用。 –

+0

@MorganGreen離開功能。如果您確定沒有滿足所需的條件,以至於無法首先啓動服務器,則需要退出。另一種可能是拋出異常,或返回成功/失敗值。 (請注意,如果沒有返回語句,函數將無限循環,否則,如果未找到apache,則輸出「Apache Started」,這兩者都不是好的。) –

+0

感謝您的輸入。我已經提出了你的答案,但是因爲我使用了更多的超級巨人,我選擇了他作爲最佳答案!如果我可以的話,會有結合並給予雙方的信任! –

0

不要忘記if語句塊中的return語句!

private void ApacheTest() 
{ 
    if(!File.Exists(HTTPD_PATH)) 
    { 
     amountdl.Text = "Apache Not Found! Installation Corrupt!"; 
     return; 
    } 
    else 
    { 
     StartApacheServer(); 
    } 

    amountdl.Text = "Apache Is Starting"; 

    while(ApacheRunning() == false) 
    { 
     Thread.Sleep(50); 
    } 

    amountdl.Text = "Apache Started"; 
} 
+0

不會'System.Thread.Sleep(5)'導致無響應的控制面板? –

+0

是摩根。 Task.Delay和Thread.Sleep都會導致無響應的用戶界面,而無需使用async/await。我加了一個關於這個的新答案。 – taydogan

1

這可能工作,但它也可能會凍結你的用戶界面(窗口)。

private void ApacheTest() 
{ 
    if(!File.Exists(HTTPD_PATH)) 
    { 
     amountdl.Text = "Apache Not Found! Installation Corrupt!"; 
    } 
    else 
    { 
     StartApacheServer(); 
    } 

    amountdl.Text = "Apache Is Starting"; 

    while(ApacheRunning() == false) 
    { 
     Thread.Sleep(200); 
    } 

    amountdl.Text = "Apache Started"; 
} 

如果出現這種情況,您可以嘗試這樣的事:

private void ApacheTest() 
{ 
    if(!File.Exists(HTTPD_PATH)) 
    { 
     amountdl.Text = "Apache Not Found! Installation Corrupt!"; 
     return; 
    } 

    amountdl.Text = "Apache Is Starting"; 

    Task.Factory.StartNew(() => 
     { 
      while(ApacheRunning() == false) 
      { 
       Thread.Sleep(200); 
      } 
      amountdl.Text = "Apache Started"; 
     }, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext()); // nicked from [the MSDN forums][1] 
} 

這樣,該函數將退出,「射後不理」的等待和更新文本任務。

0

如果你不希望你的表單不被鎖定,那麼你可以使用「異步/等待」功能與「Task.Delay」。

async private void ApacheTest() 
{ 
    if(!File.Exists(HTTPD_PATH)) 
    { 
     amountdl.Text = "Apache Not Found! Installation Corrupt!"; 
     return; 
    } 

    amountdl.Text = "Apache Is Starting"; 
    StartApacheServer(); 

    while(ApacheRunning() == false) 
    { 
     await Task.Delay(50); 
    } 

    amountdl.Text = "Apache Started"; 
} 
+0

'async void'基本上是通向地獄的高速公路; 「異步任務」在幾乎所有情況下都是正確的方式(不管用戶最終如何使用它)。 – sunside

相關問題