【问题标题】:Trying to make a try catch continuously loop until service resumes尝试不断循环尝试捕获,直到服务恢复
【发布时间】:2013-04-02 12:55:16
【问题描述】:

我基本上有一种方法可以在不同的线程中不断检查服务。这工作正常,但我需要进行检查,以便如果主机关闭,它会继续尝试直到服务恢复。

我认为这会起作用

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");

                }

            }
        }

但这有点不可预测。 想法?

【问题讨论】:

    标签: c# winforms wcf error-handling


    【解决方案1】:

    您正在从Invoke() 访问您的服务,因此代码不会在您的线程上执行,而是在主 UI 线程上执行。并且当那里抛出异常时,它不会被您的 try/catch 块捕获。

    您可以将所有服务调用移到 Invoke 之外,以便它们在您的线程上执行。

    【讨论】:

    • 哦,所以将我的服务返回的数据传递给变量,然后从 trycatch 中调用
    猜你喜欢
    • 1970-01-01
    • 2013-12-15
    • 2014-08-02
    • 1970-01-01
    • 2014-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多