【问题标题】:How can I control in what thread I show my popup如何控制在哪个线程中显示我的弹出窗口
【发布时间】:2012-03-17 13:27:00
【问题描述】:

我使用一个弹出窗口的控件。我有一个显示和隐藏它的方法,我需要实现一个功能来防止在不同线程中显示和隐藏我的弹出窗口。我可以在非 UI 线程中显示我的弹出窗口吗?

更新

我的问题的主要目标是:

方法 Show 从哪个线程开始并不重要,Method Hide 应该在同一个线程中。如何实现?

 public void Show()
        {
            IsShown = true;
            if (this.ChildWindowPopup == null)
            {
                this.ChildWindowPopup = new Popup();
                    try
                {
                    this.ChildWindowPopup.Child = this;
                }
                catch (ArgumentException)
                {
                    throw new InvalidOperationException("The control is already shown.");
                }
            }       
            if (this.ChildWindowPopup != null && Application.Current.RootVisual != null)
            {
                // Configure accordingly to the type
                InitializeProgressType();
                    // Show popup
                this.ChildWindowPopup.IsOpen = true;
            }
        }

 public void Hide()
        {
            IsShown = false;
            // Restore system tray
            SystemTray.IsVisible = currentSystemTrayState;
            this.progressBar.IsIndeterminate = false;
            this.ChildWindowPopup.IsOpen = false;
            }

【问题讨论】:

  • 能不能简单的在显示的时候保存Thread.CurrentThread,隐藏的时候看看是否一样?仍然 - 我认为这个问题表明另一个问题。为什么要检查线程?如果目标是显示某些操作的进度条 - 您可以创建一些对象(令牌)以将进度条与操作相关联。除了调试另一个问题之外,我不明白您为什么要按照建议的方式管理线程。

标签: c# silverlight windows-phone-7 silverlight-4.0 silverlight-3.0


【解决方案1】:

通常只能在 UI 线程中处理 UI 组件。如果您想阻止您的特定弹出窗口从 UI 线程启动,您需要提供一个接口来启动它,该接口在内部检查它已执行的线程 - 可能使用 SynchronizationContext 或 Thread 类,但最终 - 它需要通过在 UI 线程上调用方法来显示 Popup。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 2014-11-26
    • 2019-11-13
    • 2021-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多