当自定义 MessageBox 启动时,我应该怎么做才能冻结父线程?

发布于 2024-11-06 17:24:34 字数 2113 浏览 0 评论 0原文

我使用 Coding4Fun 工具包中的 MessagePrompt 创建了自定义 MessageBox。 当我运行 ResetData_Click 时出现问题。我预计在启动 ComplexMessage.Show 后,ResetData_Click 内的其余代码会在 ComplexMessage 打开时停止执行。实际情况是完全不同的。所有代码都会立即执行,用户在 ComplexMessage 中选择什么并不重要,因为

if (ComplexMessage.Result)...

它已经执行了。 我应该怎么做才能使我的 ComplexMessage 表现得像 System.Windos.MessageBox?这意味着当调用 MessageBox 时,父线程正在等待用户的决定。

        private void ResetData_Click(object sender, RoutedEventArgs e)
        {
            ComplexMessage.Show("You are about to delete all data", "Are you sure?", true);

            if (ComplexMessage.Result)
            {
                DataControl.DataFileReset();
            }
        }

 public class ComplexMessage
    {
        private static MessagePrompt messageprompt;
        private static bool messageresult;

        public static void Show(string message, string title, bool vibrate)
        {
            if (!(!(messageprompt == null) && messageprompt.IsOpen))
            {
                messageprompt = new MessagePrompt
                {
                    Title = title,
                    Message = message
                };

                messageprompt.Completed += new EventHandler<PopUpEventArgs<string, PopUpResult>>(messageprompt_Completed);
                messageprompt.IsCancelVisible = true;
                messageprompt.Show();
                if (vibrate) { Tools.VibrateMessage(); }
            }
        }

        static void messageprompt_Completed(object sender, PopUpEventArgs<string, PopUpResult> e)
        {
            if (!e.PopUpResult.Equals(PopUpResult.Cancelled))
            {
                messageresult = true;
            }
            else
            {
                messageresult = false;
            }
            ((MessagePrompt)sender).Completed -= messageprompt_Completed;
        }

        public static bool Result
        {
            get { return messageresult; }
        }
    }

I've created my custom MessageBox using MessagePrompt from the Coding4Fun toolkit.
The problem occurs when I run ResetData_Click. I expected that after launching ComplexMessage.Show rest of the code inside ResetData_Click stops executing while ComplexMessage is open. As occurred it is completely different. All code is executed at once and it doesn't matter what user will chose in ComplexMessage because

if (ComplexMessage.Result)...

is already executed.
What should I do to make my ComplexMessage act like System.Windos.MessageBox? It means when MessageBox is called the parent's thread is waiting for the user's decision.

        private void ResetData_Click(object sender, RoutedEventArgs e)
        {
            ComplexMessage.Show("You are about to delete all data", "Are you sure?", true);

            if (ComplexMessage.Result)
            {
                DataControl.DataFileReset();
            }
        }

 public class ComplexMessage
    {
        private static MessagePrompt messageprompt;
        private static bool messageresult;

        public static void Show(string message, string title, bool vibrate)
        {
            if (!(!(messageprompt == null) && messageprompt.IsOpen))
            {
                messageprompt = new MessagePrompt
                {
                    Title = title,
                    Message = message
                };

                messageprompt.Completed += new EventHandler<PopUpEventArgs<string, PopUpResult>>(messageprompt_Completed);
                messageprompt.IsCancelVisible = true;
                messageprompt.Show();
                if (vibrate) { Tools.VibrateMessage(); }
            }
        }

        static void messageprompt_Completed(object sender, PopUpEventArgs<string, PopUpResult> e)
        {
            if (!e.PopUpResult.Equals(PopUpResult.Cancelled))
            {
                messageresult = true;
            }
            else
            {
                messageresult = false;
            }
            ((MessagePrompt)sender).Completed -= messageprompt_Completed;
        }

        public static bool Result
        {
            get { return messageresult; }
        }
    }

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

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

发布评论

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

评论(1

苍景流年 2024-11-13 17:24:34

由于您是通过单击事件显示 MessageBox,因此您正在 UI 线程上运行,您不想冻结该线程。

一种选择是让 ComplexMessage 公开一个静态事件,该事件在 messageprompt_Completed 中触发。

然后,在 ResetData_Click 中,在调用 ComplexMessage.Show 之前订阅该事件,并在事件处理程序中,根据结果,调用 DataControl.DataFileReset 并取消订阅。

另一种方法是重新考虑将 ComplexMessage 的成员设为静态,并将“Actioncallback”参数传递给 Show 方法,将其存储在私有成员中,然后在 messageprompt_Completed 中调用回调。

Since you are displaying the MessageBox from a click event, you are running on the UI thread, which you don't want to freeze.

One option is to make ComplexMessage expose a static event, which it fires in messageprompt_Completed.

Then in ResetData_Click subscribe to the event prior to calling ComplexMessage.Show, and in the event handler, depending on the result, call DataControl.DataFileReset, and unsubscribe.

An alternative is to rethink making the members of ComplexMessage static, and instead to pass an "Action<bool> callback" parameter to the Show method, which you store away in a private member, and then invoke the callback in messageprompt_Completed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文