Can events trigger only once at a time?

Say I have an event like this:

SomeClass.SomeEvent += MyMethodToCall();

...

public void MyMethodToCall()
{
     if (CheckToSeeIfFormIsAlreadyShowing())
     {
         SomeForm someForm = new SomeForm();
         someForm.ShowDialog();
     }
     else
     {
         DoSomeStuff();
     }
}

If the user interaction with SomeFormcauses SomeEvent to start again, will MyMethodToCall be called again while SomeFormit is still displayed?

My testimonies seem to show that it will not.

My question is why? The event happened again. Why not MyMethodToCallcall the call again (and then finally call DoSomeStuff()?

I guess this is just not possible (still don't know why). Assuming this is not possible, can I show the dialog in a separate thread ( begininvoke)? (I seem to remember that the user interface should happen on a single thread, so I hesitate to try this).

, , ( SomeForm . , , !

. , , , . (, , .)

+3
5

, .

SomeClass.SomeEvent , . , , , .

, ShowDialog . , , , .

, , , . , , BeginInvoke , ShowDialog .

, :

SomeClass.SomeEvent += SomeEventHandler;
...
...
...
void SomeEventHandler()
{
    // I'm assuming this code is in a class derived from Form
    this.BeginInvoke(new MethodInvoker(HandleEventOnUIThread));
}

void HandlerEventOnUIThread()
{
    if (CheckToSeeIfFormIsAlreadyShowing())
    {
        SomeForm someForm = new SomeForm();
        someForm.ShowDialog();
    }
    else
    {
        DoSomeStuff();
    }
}

, SomeClass.SomeEvent , , .

+1

, , . SomeEvent , . , , .

ShowDialog() , . DoEvents(), , . ShowDialog , , . , , , , . , , , DoEvents().

, , , . , , . . .

+3

.

, ShowDialog . ShowDialog , , .

, ShowDialog , .

, , , SomeClass.SomeEvent (, ), SomeClass.SomeEvent.

, , ShowDialog, , , SomeClass.SomeEvent.

, , , ? , , , ?

, .

+3
source

using:

someForm.Show();

instead of ShowDialog (), and the event will continue to fire :)

+1
source

Fire SomeEvent in the background thread. In the handler, when you want to display the dialog, you must march the call into the UI thread. DoSomeStuff will execute on the workflow, even if the dialog is still open.

+1
source

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


All Articles