C #: two forms, one is the other.

I have this problem: I have two Winforms, f1 and f2.

f1 will start the loop when the button is pressed, this loop checks the condition and decides to call another form, which is f2 or not.

The problem is that a loop can call f2 many times, so every time another form f2 is called the first form, f1 must pause its execution .

So, I solved it like this, I used backgroundWorker + AutoResetEvent . I put backgroundWorker in the first form and inside the DoWork event handler, which I called f2.Show (), then I called WaitOne in AutoResetEvent, let it be A.

In another form, "f2" on the "Exit" button, I called Install on the same A.

But unfortunately, f2 freezes when you press this button in f1, what should I change?

+3
source share
2 answers

Instead of handling threads and reset events, why not just call form F2 as a dialog?

var f2 = new Form2();
// ...
f2.ShowDialog(this);

Thus, the F1 process will continue only after the completion of form 2.

+4
source

You should not stop execution in the GUI thread in WinForms, so most likely you are wrong (for example, call WaitOne, which blocks your GUI).

"" . , . . BackgroundWorker, , .

, , f2.ShowDialog f2.Show. ShowDialog , , , .

, , .

+2

Source: https://habr.com/ru/post/1744571/


All Articles