简体   繁体   中英

Suspend Main Thread, Execute some Callbacks, and then resume Main Thread

I need to do somethings, execute some callbacks events, and then resume my routine. I'm using a Win Forms Application... I write you a little example.

 void Do()   // On main Thread
    {
         DoPartOne();

         // Her i want to execute the callbacks events... and then execute 
        //parttwo

         DoPartTwo();
    }

    void DoPartOne() // during this method i send commands to my connected service
    {
         StaticClass.Property = null;
    }

    void DoPartTwo() 
    {
         StaticClass.Property = something...
    }

    void MyCallBackOnEvent(MyEvent)
    {
       if(StaticClass.Property == null)
           DoThis();
       else
           DoThat();
    }

During Party One my Client send a command to my connected Service and it response me with some variable numbers of events.... I need to execute callbacks first of my PartTwo() method, but i fix that callback execute it when i finish the Do() method. Do you suggest me any other implementations?

Do your action, then wait for a variable to become true:


while(!workIsDone)
{
   Thread.sleep(100);
}

Then in your event, set the variable in your event, like so:

void MyCallBackOnEvent(MyEvent)
{  

    // Event has fired, so we can set workIsDone to true and then the main thread will stop the while loop, and continue normally.

    workIsDone = true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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