简体   繁体   English

如果在 wpf c# 中使用 thread.join() 如何避免服务器繁忙窗口

[英]how to avoid Server busy window if used thread.join() in wpf c#

I have a c++ application and use clr to call the below c# method .我有一个c++ application并使用clr调用下面的c# method Everything is going good except for one thing.除了一件事,一切都很顺利。 As wpf window needs STA thread , I'm trying to create a new thread with STA state and start it.由于wpf window需要STA thread ,我正在尝试创建一个具有 STA 状态的新线程并启动它。 This makes the wpf window modeless , even if the window is started with show dialog .这使得 wpf 窗口是modeless ,即使窗口是通过show dialog启动的。 So I tried using thread.join() to make the caller thread to wait until the thread completes or window closes.所以我尝试使用thread.join()使调用者线程等待线程完成或窗口关闭。 using thread.join() shows server busy window after a few seconds.几秒钟后使用thread.join()显示server busy window (I'm not doing any operation related to the internet in my application). (我没有在我的应用程序中做任何与互联网相关的操作)。

How to make the caller thread to wait until the window closes?如何让调用者线程wait窗口关闭? or How to get rid of Server busy window?或如何摆脱服务器繁忙窗口?

        void ShowWindow()
        {
            _ownerHandle = Process.GetCurrentProcess().MainWindowHandle;
            Thread newWindowThread = new Thread(new ThreadStart(() =>
            {
                MyWpfWindow window = new MyWpfWindow();
                MyWpfWindowViewModel vm = new MyWpfWindowViewModel();

                WindowInteropHelper helper = new WindowInteropHelper(window);
                helper.Owner = _ownerHandle;

                window.DataContext = vm;

                window.ShowDialog();

            }));

            // set the apartment state  this will only invoke the WPF window  
            newWindowThread.SetApartmentState(ApartmentState.STA);
            newWindowThread.IsBackground = true;
            // start the thread  
            newWindowThread.Start();
            //waits this thread here untill the newWindowThread execution completes.
            newWindowThread.Join();

        }

Also, I tried with a while loop as below to block the current main thread.另外,我尝试使用如下的 while 循环来阻止当前的主线程。

while(!windowShown)
{
}

in the place of newWindowThread.join().代替 newWindowThread.join()。 After using this, my window is not visible.使用此后,我的窗口不可见。

I'm getting a server busy window as below我得到一个服务器繁忙的窗口,如下所示

在此处输入图片说明

Calling Join() blocks the current thread.调用Join()阻塞当前线程。 If you want to wait asynchronously, you could use a SemaphoreSlim :如果你想异步等待,你可以使用SemaphoreSlim

static async Task ShowWindowAsync()
{
    SemaphoreSlim semaphore = new SemaphoreSlim(0, 1);
    _ownerHandle = Process.GetCurrentProcess().MainWindowHandle;
    Thread newWindowThread = new Thread(new ThreadStart(() =>
    {
        MyWpfWindow window = new MyWpfWindow();
        MyWpfWindowViewModel vm = new MyWpfWindowViewModel();
        WindowInteropHelper helper = new WindowInteropHelper(window);
        helper.Owner = _ownerHandle;
        window.DataContext = vm;
        window.Closed += (s, e) =>
        {
            semaphore.Release();
            semaphore.Dispose();
        };
        window.ShowDialog();

    }));

    // set the apartment state  this will only invoke the WPF window  
    newWindowThread.SetApartmentState(ApartmentState.STA);
    newWindowThread.IsBackground = true;
    // start the thread  
    newWindowThread.Start();
    //waits this thread here untill the newWindowThread execution completes.
    await semaphore.WaitAsync();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM