Home > Net >  How to wait for a method execution after the success of the callback to the back of the code
How to wait for a method execution after the success of the callback to the back of the code

Time:11-05

I have a thread, you need to call a message is sent, the news if you don't send success will continue to the next message will appear to repeat several situation, sending a message function I added the Action parameter, if after sending a message no matter whether success will callback the Action, so that the outside can see inside the message has been sent you won't get duplicate messages, but I don't know how to perform outside
Such as sending a message is:
Instance. SendMsg (string MSG, string userid, Action Success=null);
I am now outside direct call
for(int i=0; i<10; I++)
{
The Instance. The sendMsg (" hello ", 10086);
}
How to write this kind of circumstance can according to the action to do the next Instance. The sendMsg?
The queue? Queue also can't determine whether to send a success?

CodePudding user response:

With a Task to perform and Task where waiting, performed to give the mainline cenfa notice, for example,

 
Task t=new Task (()=& gt; {the Instance. The sendMsg (" hello ", 10086); });
T.S tart ();
T.W ait ();
This. The Dispatcher. Invoke (()=& gt; {MessageBox. Show (" do next "); });

CodePudding user response:

reference 1st floor icoolno1 response:
with the Task to perform, and the Task where waiting, performed to give the mainline cenfa notice, for example,

 
Task t=new Task (()=& gt; {the Instance. The sendMsg (" hello ", 10086); });
T.S tart ();
T.W ait ();
This. The Dispatcher. Invoke (()=& gt; {MessageBox. Show (" do next "); });


SendMsg inside is a WebSocket SendAsync, frontal you wrote, you should still have problems, a Task can Wait until SendAsync callback of success?

CodePudding user response:

Using threads, add status identification, according to identify the operation, I have been so used, absolutely can,

CodePudding user response:

With a Task to perform and Task where waiting, performed to give the mainline cenfa notice, for example,
SendMsg inside is a WebSocket SendAsync, frontal you wrote, you should still have problems, a Task can Wait until SendAsync callback of success?

CodePudding user response:

refer to the second floor JankingChen response:
Quote: refer to 1st floor icoolno1 response:
with the Task to perform, and the Task where waiting, performed to give the mainline cenfa notice, for example,

 
Task t=new Task (()=& gt; {the Instance. The sendMsg (" hello ", 10086); });
T.S tart ();
T.W ait ();
This. The Dispatcher. Invoke (()=& gt; {MessageBox. Show (" do next "); });


SendMsg inside is a WebSocket SendAsync, frontal you wrote, you should still have problems, a Task can Wait until SendAsync callback of success?


Stick the complete code,

CodePudding user response:

ThreadPool thread pool, waiting for the results after back on to perform the next step

CodePudding user response:

After your thread to perform this action, so how to judge the thread has been completed? Use the Task can achieve this requirement, see Task. ContinueWith method,

CodePudding user response:

Every time with a message queue, send only take message (peek), send the completed according to the results of the callback to dequeue, if successful, and continue to peek the next, if failure is directly to peek,

CodePudding user response:

 
//the return value is what, is void or bool?
Instance. SendMsg (string MSG, string userid, Action Success=null);
Is written by yourself, this method can directly change the methods, such as not using the Action, direct return Task
  •  Tags:  
  • C#
  • Related