如何使用另一个线程隐藏窗口?

时间:2015-01-16 08:33:46

标签: wpf multithreading notifyicon

我使用NotifyIcon创建了一个WPF应用程序,并使用this.Hide()使其最小化,但现在我希望它一旦被执行就可以最小化,所以我调用{方法this.Hide()中{1}},但是一旦启动应用,窗口的内容就会变暗。

然后我尝试在另一个线程中调用MainWindow_Loaded,这就是我的代码看起来像......

this.Hide()

然后我遇到了问题,当调用private void MainWindow_Loaded(object sender, RoutedEventArgs e) { ... Thread thread = new Thread(DoWork); thread.Start(); Console.WriteLine("thread start"); while (!thread.IsAlive) ; Thread.Sleep(500); thread.Join(); Console.WriteLine("thread has terminated."); ... } public void DoWork() { this.Hide(); } 时,它显示DoWork()我应该怎么做以避免这种情况?谢谢!

1 个答案:

答案 0 :(得分:0)

您需要使用Dispatcher对象。



Dispatcher.BeginInvoke((Action) (() =>
    {
        // your code
    }));




相关问题