C# 在MessageBox.Show()处暂停基于计时器的程序

C# 在MessageBox.Show()处暂停基于计时器的程序,c#,.net,timer,messagebox,dialogresult,C#,.net,Timer,Messagebox,Dialogresult,我有一个MessageBox.Show事件,我想在MessageBox保持打开状态时阻止基于计时器的方法运行 这是我的代码(每x分钟更改网络上文件位置中的值): 我有这样的方法,每30秒调用一次。也就是说,每隔30秒,就会弹出另一个消息框。是否有办法使用MessageBox暂停应用程序?如果没有,解决此问题的最佳方法是什么?如果可能,我希望避免使用Timer.Stop(),因为它会重置计时器计数。最简单的解决方案是使用一个标志指示消息框当前是否打开: private bool isMessage

我有一个MessageBox.Show事件,我想在MessageBox保持打开状态时阻止基于计时器的方法运行

这是我的代码(每x分钟更改网络上文件位置中的值):


我有这样的方法,每30秒调用一次。也就是说,每隔30秒,就会弹出另一个消息框。是否有办法使用MessageBox暂停应用程序?如果没有,解决此问题的最佳方法是什么?如果可能,我希望避免使用Timer.Stop(),因为它会重置计时器计数。

最简单的解决方案是使用一个标志指示消息框当前是否打开:

private bool isMessageBoxOpen = false;

public void offlineSetTurn()
{
    if (isMessageBoxOpen)
        return;

    try
    {
        using (StreamWriter sWriter = new StreamWriter("FileLocation"))
        {
            sWriter.WriteLine(Variable);
        }
    }
    catch (Exception ex)
    {
        isMessageBoxOpen = true;
        DialogResult result = MessageBox.Show("Can't find file.  Click Okay to try again and Cancel to kill program",MessageBoxButtons.OKCancel);
        isMessageBoxOpen = false;

        if (result == DialogResult.OK)
        {
            offlineSetTurn();
        }
        else if (result == DialogResult.Cancel)
        {
            Application.Exit();
        }
    }
}

我认为最简单的解决方案是有第二个非模态窗口,可以从第一个窗口中假脱机消息。根据第二个窗口中的用户输入,您可以退出或调用
offlineSetTurn()
.Ah,这很有意义。例如,添加第二个bool canConnect()方法,该方法仅在canConnect()返回true时调用我前面提到的offlineSetTurn()方法?谢谢,这似乎是一种方法。
private bool isMessageBoxOpen = false;

public void offlineSetTurn()
{
    if (isMessageBoxOpen)
        return;

    try
    {
        using (StreamWriter sWriter = new StreamWriter("FileLocation"))
        {
            sWriter.WriteLine(Variable);
        }
    }
    catch (Exception ex)
    {
        isMessageBoxOpen = true;
        DialogResult result = MessageBox.Show("Can't find file.  Click Okay to try again and Cancel to kill program",MessageBoxButtons.OKCancel);
        isMessageBoxOpen = false;

        if (result == DialogResult.OK)
        {
            offlineSetTurn();
        }
        else if (result == DialogResult.Cancel)
        {
            Application.Exit();
        }
    }
}