单例消息框

发布于 2025-01-02 23:45:53 字数 383 浏览 1 评论 0原文

我正在编写一个应用程序,其中当连接发生问题时,我想弹出一个消息框并向用户显示错误...

为此目的,当程序抛出异常时,它将进入 catch 块,并且我想要向用户显示消息,这里是代码:

        catch (WebException ex)
        {
            if (!(ex.Message == "The operation has timed out."))
            {
                  MessageBox.Show(ex.Message);
            }

        }

因为看起来程序会永远捕获类似的东西,直到连接修复,所以我应该怎么做才能一次只更新一个消息框上的消息?

I am writing an application in which when something happened to the connection I want to pop up a messagebox and show the user the error...

for this purpose when the program throw an exception it will come to the catch block and in that I want to show the user the message here is the code :

        catch (WebException ex)
        {
            if (!(ex.Message == "The operation has timed out."))
            {
                  MessageBox.Show(ex.Message);
            }

        }

As it seems the program will come to this catch something like forever till the connection is become fixed so what should I do to update my message on just one messagebox at a time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玉环 2025-01-09 23:45:53

MessageBox 显示时没有太多控制。我将使用另一个以模态模式显示的 Form 。在显示之前,您可以启动一个单独的线程并放置监视连接的逻辑。重新建立后,通知消息表单并将其关闭。

There is not much control over MessageBox when it's displayed. I would use another Form displayed in a modal mode. Before displaying, you can start a separate thread and put the logic to monitor the connection. When re-established, notify the message form and close it.

安稳善良 2025-01-09 23:45:53

您可以使用类似的内容:

public static class FailureMessagebox
{
    private static bool _hasBeenSuccessful = true;

    public static void ShowIfFailure(Action someAction)
    {
        try
        {
            someAction();
            _hasBeenSuccessful = false;
        }
        catch (Exception err)
        {
            if (_hasBeenSuccessful)
                MessageBox.Show(ex.Message);

            _hasBeenSuccessful = false;
                    throw;
        }
    }
}

示例用法:

try
{
   WebResponse response;
   FailureMessagebox.ShowIfFailure(() => response = webRequest.GetResponse());
}
catch (WebException err)
{
   //handle the exception here.
}

You can use something like:

public static class FailureMessagebox
{
    private static bool _hasBeenSuccessful = true;

    public static void ShowIfFailure(Action someAction)
    {
        try
        {
            someAction();
            _hasBeenSuccessful = false;
        }
        catch (Exception err)
        {
            if (_hasBeenSuccessful)
                MessageBox.Show(ex.Message);

            _hasBeenSuccessful = false;
                    throw;
        }
    }
}

Sample usage:

try
{
   WebResponse response;
   FailureMessagebox.ShowIfFailure(() => response = webRequest.GetResponse());
}
catch (WebException err)
{
   //handle the exception here.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文