0
我基本上有一種方法,在不同的線程中不斷檢查服務。這工作正常,但我需要進行檢查,以便如果主機關閉,它會繼續嘗試,直到服務恢復。試圖做一個嘗試不斷循環,直到服務恢復
我想這會工作
private void GetTankLevels()
{
Service1Client client = new Service1Client();
while (true)
{
try
{
this.DieselBox.Invoke(new MethodInvoker(delegate { DieselBox.Text = client.GetTankAmounts("Diesel").ToString(); }));
this.PetrolBox.Invoke(new MethodInvoker(delegate { PetrolBox.Text = client.GetTankAmounts("Unleaded").ToString(); }));
double DFuelLvl = client.GetTankAmounts("Diesel");
double PFuelLvl = client.GetTankAmounts("Unleaded");
int DieselProgress = (int)DFuelLvl;
int petrolProgress = (int)PFuelLvl;
if (DFuelLvl < 300)
{
this.DieselBar.Invoke(new MethodInvoker(delegate { DieselBar.Value = DieselProgress; }));
}
else if (DFuelLvl > 300 && DFuelLvl < 500)
{
this.DieselBar.Invoke(new MethodInvoker(delegate { DieselBar.Value = DieselProgress; }));
}
else if (DFuelLvl > 500 && DFuelLvl < 850)
{
this.DieselBar.Invoke(new MethodInvoker(delegate { DieselBar.Value = DieselProgress; }));
}
else
{
this.DieselBar.Invoke(new MethodInvoker(delegate { DieselBar.Value = DieselProgress; }));
}
if (PFuelLvl < 300)
{
this.Petrolbar.Invoke(new MethodInvoker(delegate { Petrolbar.Value = petrolProgress; }));
}
else if (PFuelLvl > 300 && PFuelLvl < 500)
{
this.Petrolbar.Invoke(new MethodInvoker(delegate { Petrolbar.Value = petrolProgress; }));
}
else if (PFuelLvl > 500 && PFuelLvl < 850)
{
this.Petrolbar.Invoke(new MethodInvoker(delegate { Petrolbar.Value = petrolProgress; }));
}
else
{
this.Petrolbar.Invoke(new MethodInvoker(delegate { Petrolbar.Value = petrolProgress; }));
}
}
catch
{
MessageBox.Show("Lost connection to the host... Please wait");
}
}
}
但它是一個有點難以預料。 想法?
哦,所以將變量中返回的數據從我的服務中傳遞出來,然後執行trycatch – 2013-04-10 15:29:00
@RhysDrury,yep – alex 2013-04-10 15:31:48
完成工作。謝謝 – 2013-04-10 15:41:37