Novice asked, and a device for TCP communications, after I send instruction to equipment, equipment will reply corresponding instructions, now I hope to send instructions after waiting for 1 s, if received equipment reply, pop-up prompts,
how to add code?
Private void Newopenbutton_Click (object sender, EventArgs e)
{
Singleclient=new RSUTcpClient (port, serverip);
Singleclient. ReceviedData +=Singleclient_ReceviedData;
Singleclient. Send (order);//send instructions
}
Private void Singleclient_ReceviedData (object sender, NetEventArgs e)
{
,,,,,,,,,, for equipment return information,,,,
}
CodePudding user response:
Add a serial number
CodePudding user response:
At first I write so, but received the reply, obviously will pop up the error, this is why
Private bool t=false;
Private void Newopenbutton_Click (object sender, EventArgs e)
{
Singleclient=new RSUTcpClient (port, serverip);
Singleclient. ReceviedData +=Singleclient_ReceviedData;
Singleclient. Send (order);//send instructions
Thread.sleep (1000);
If (t==false)
{
Messagebox. Show (" error ");
}
}
Private void Singleclient_ReceviedData (object sender, NetEventArgs e)
{
T=true;
,,,,,,,,,, for equipment return information,,,,
}
CodePudding user response:
The
big watermelon a WuYiJin reference 1 floor? Response: add a serial number Can comfortably in detail about it, and why not according to the below that I write CodePudding user response:
This kind of question is not too much recently Ok, asynchronous tasks, asynchronous semaphore -- -- -- -- -- -- -- -- -- -- means don't be too much A few days ago someone said that the functional garbage, mix tuo, the ans, says the problem is you do not accept the functional, scatter a logic is everywhere, the results meet don't come back I also don't don't like to see you, you will just get, with you would like to accept the way to play Pseudo code: Semaphore Singleclient=new RSUTcpClient (port, serverip); Singleclient. ReceviedData +=(send, e)=& gt; { //if the received, release the semaphore } Semaphore. WaitOne (timeout) occupy the semaphore Singleclient. Send (order);//send instructions Semaphore. WaitOne ()//he will be stuck waiting for the last time in the release, timeout, of course also release //because he didn't like you don't like it, so your decision is received release, or a timeout release Finally released in full semaphore Because the process is asynchronous, suggest you had better use asynchronous semaphore -- -- -- -- -- -- -- -- -- -- - don't talk here the signal is not high, you do not receive more simplified version of Microsoft, you only like to play with Microsoft's internal implementation of advanced stuff, euphemistically called "technology" CodePudding user response:
reference 4 floor wanghui0380 response: recently this kind of question is not too much Ok, asynchronous tasks, asynchronous semaphore -- -- -- -- -- -- -- -- -- -- means don't be too much A few days ago someone said that the functional garbage, mix tuo, the ans, says the problem is you do not accept the functional, scatter a logic is everywhere, the results meet don't come back I also don't don't like to see you, you will just get, with you would like to accept the way to play Pseudo code: Semaphore Singleclient=new RSUTcpClient (port, serverip); Singleclient. ReceviedData +=(send, e)=& gt; { //if the received, release the semaphore } Semaphore. WaitOne (timeout) occupy the semaphore Singleclient. Send (order);//send instructions Semaphore. WaitOne ()//he will be stuck waiting for the last time in the release, timeout, of course also release //because he didn't like you don't like it, so your decision is received release, or a timeout release Finally released in full semaphore Because the process is asynchronous, suggest you had better use asynchronous semaphore -- -- -- -- -- -- -- -- -- -- - don't talk here the signal is not high, you do not receive more simplified version of Microsoft, you only like to play with Microsoft's internal implementation of advanced stuff, euphemistically called "technology" Brother, this is not high not advanced, then do not accept the problem, new shivering, MOE, CodePudding user response:
Below is normal "low-level" approach, rather than a "senior" play Simulation of button3 start an asynchronous task, received button4 event that normal end for 10 seconds, or as a timeout private async void button3_Click (object sender, EventArgs e) { Try { CancellationTokenSource CTS=new CancellationTokenSource (TimeSpan. FromSeconds (10)); Await wait4buttclck (CTS) Token); MessageBox. Show (" normal until button4 events "); } The catch (Exception Exception) { MessageBox. Show (" timeout or other abnormal "); } } Task Wait4buttclck (CancellationToken token) { TaskCompletionSource TCS=new TaskCompletionSource (a); Task. Factory. StartNew (()=& gt; { This. Button4. Click +=(send, e)=& gt; {the TCS. TrySetResult (true); }; }, token); Token. Register (()=& gt; {the TCS. TrySetException (new TimeoutException ()); }); Return the TCS. Task; } } CodePudding user response:
Change, avoid events articulated leak, task end removal articulated private async void button3_Click (object sender, EventArgs e) { Try { CancellationTokenSource CTS=new CancellationTokenSource (TimeSpan. FromSeconds (10)); Await wait4buttclck (CTS) Token); MessageBox. Show (" normal until button4 events "); } The catch (Exception Exception) { MessageBox. Show (" timeout or other abnormal "); } } Task Wait4buttclck (CancellationToken token) { TaskCompletionSource TCS=new TaskCompletionSource (a); EventHandler handler=(send, e)=& gt; { TCS. TrySetResult (true); }; Task. Factory. StartNew (()=& gt; {this. Button4. Click +=handler; }, token); TCS. Task. ContinueWith (p=& gt; { Enclosing button4. Click -=handler; }); Token. Register (()=& gt; { TCS. TrySetException (new TimeoutException ()); }); Return the TCS. Task; }