Hairus
Hairus

Reputation: 168

Windows Form runs new thread with dialog box (form) and main thread owns the dialog box

I have a windows application written in C#. When the app started, it create new thread to run some background process while the main thread is idle. The background thread will show a dialog box. I found that the dialog box will be hidden if I click on the main UI.

The question is how can I make the dialog box is own by the main UI? I know that by setting the TopMost property to true will make the dialog stay on top of the screen, but I don't want this.

Please help.

Upvotes: 0

Views: 430

Answers (1)

Theo
Theo

Reputation: 2809

You should trigger all UI changes on the main thread. Have the background process return the result to the main UI thread and then trigger the dialog box from there. The async/await works very well for tasks such as this.

Here is a simple example of executing something asynchronously and then setting the UI element with the returned result... Accessing UI controls in Task.Run with async/await on WinForms

Upvotes: 1

Related Questions