In the file download function, often a bad network condition caused by file download failure, but in this case can not catch the exception or timeout callback, if the flow of gravity, the file will be in the download for a long time.
Based on the above situation to introduce timeouts, in the case of poor network status, set the time-out, such as 3 seconds, and then 3 seconds after the data is not received, disconnect the socket and reconnect, the breakpoint continues to pass.
We use
ThreadPool.RegisterWaitForSingleObject
To execute the timeout setting.
Clientsocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, Protocoltype.ip); Start connecting the socket and request HTTP header information, add timeout thread timing, 3 seconds timeout; AutoResetEvent AutoResetEvent = new AutoResetEvent (false); Registeredwaithandle WaitHandle = ThreadPool.RegisterWaitForSingleObject (AutoResetEvent, New WaitOrTimerCallback ( Timeoutcallback), NULL, n, true); Clientsocket.connect (host,); NetworkStream = new NetworkStream (clientsocket); var bytes = Encoding.Default.GetBytes (querystr); Networkstream.write (bytes, 0, bytes. Length); Waithandle.unregister (AutoResetEvent);//Request return, cancel timeout setting; var breader = new BinaryReader (NetworkStream, Encoding.default);
private void Timeoutcallback (object state, bool timeout) { Debug.Log ("Timeoutcallback"); if (timeout) { debug.logerror ("Timeoutcallback timeout close all"); Networkstream.close (); Clientsocket.close (); M_bdownloadstart = false; } }
public static Registeredwaithandle RegisterWaitForSingleObject (
WaitHandle Waitobject,
WaitOrTimerCallback CallBack,
Object State,
int millisecondsTimeOutInterval,//Interval time
BOOL executeOnlyOnce//false is executed in a loop, similar to a timer
)
The use of this method is to add a method to the thread pool that can be executed on a timed basis, and the fourth parameter millisecondstimeoutinterval is used to set the interval execution time, but here the fifth parameter executeonlyonce will work on the fourth parameter, when it is true, indicates that the task executes only once, that is, it does not, like a timer, executes once every time, this function can also be implemented with the timer control
On this basis, the method also provides a semaphore-based trigger to perform the task.
The signal volume is also called the switch amount, so the name Incredibles, it has only two states, not true is false,
WaitHandle is the basic class of this kind of switching volume, inheriting its class has mutex,manualresetevent,autoresetevent, generally we use the latter two
The last bit of code
AutoResetEvent AutoResetEvent = new AutoResetEvent (false); ThreadPool.RegisterWaitForSingleObject (AutoResetEvent, New WaitOrTimerCallback (obj, boo) = { Console.Write ("obj" + obj) , "lll", "N", "false"); This is very important, because ThreadPool is background, so to wait, otherwise executed console.read ();
Output "Objlll" every 3 seconds
ThreadPool.RegisterWaitForSingleObject Setting the Wait timeout event