Faradey Inimicos
Faradey Inimicos

Reputation: 767

ShowDialog method without blocking other windows in WPF

I write program for sorting music. For saving your time and better understanding of my issue,i will write short. Here is my problem. I have some cycle in MainMethod. Here is cycle

  private void OkButton(object sender, RoutedEventArgs e)//when i press ok button in Main window i run cycle
    {
        for (int i = 0; i < 50; i++)
        {
            //do something.

            Window window1 = new Window();
            window1.ShowDialog();//if i use ShowDialog it blocks MainWindow.
           //window1.Show();if i use Show it continues creating new windows. in cycle.
        }
    }

So i need to delay executing MainWindow OkButton method,while window1 is opened.Without blocking Main Window.

Upvotes: 1

Views: 1130

Answers (1)

brunnerh
brunnerh

Reputation: 185300

You can use async/await and a semaphore along these lines:

private async void Button_Click(object sender, RoutedEventArgs e)
{
    var signal = new SemaphoreSlim(0, 1);
    for (int i = 0; i < 10; i++)
    {
        var window = new Window();
        window.Closed += (s, _) => signal.Release();

        window.Show();

        await signal.WaitAsync();
    }
}

Upvotes: 2

Related Questions