I have a normal C # while loop question.
This code will continue to execute only after that, the RDP session has been really disconnected.
When the connected property is converted to 0, it means that the RDP session connection has actually been terminated. When the asset is 1, it is still connected and the connection is not yet finished.
Is there something naturally bad about this code? Is there a better way to go about it?
Private void Reconnect () {rdp1.Disconnect (); // When RDP is still connected (// none) rdp1.Connect (); when compelling to disconnect the RDP session (rdp1.Connected == 1) // // execute this code after loop break}}
/ ************************************************************* ************************* ********************************************************************* ************** James' answer counter is enough as the timeout for my purpose.
int i = 0; Rdp1.Disconnect (); While (rdp1.Connected == 1) {if (i == 1000 * 10) breaks; Second thread Sleep (100); I ++; } Rdp1.Connect ();
You should do something in the body of the loop, or This will consume all of your CPU (for at least one core), usually in this type of loop, you can use some time using System.Threading.Thread.Sleep (100)
or something Sleep for Sleep
takes the number of milliseconds to wait before testing the condition again. Ideally, there will be a mute x or event in the RDP object, or something you were disconnected, But I could block it, but it would not surprise me if they got out.
Edit: As Ben said, it is always a good idea to have a way out of the loop. Something similar (in your answer will depend on the speed of the CPU, which can break in the future when the CPU is very fast):
DateTime = Datetime. UTACENAdSeconds (30); While (rdp1.Connected) {if (DateTime.UtcNow> Stop) releases new ApplicationException ("RDP Disconnect Timeout!"); System threading. Thread Sleep (100); }
Of course you can possibly timeout with a constant, a readable timezone
, or a dynamically configurable TimeSpan
Would like to specify a magic number, and you should probably have a specific exception class for this matter.
No comments:
Post a Comment