I don't really want someone to hang up or resume a thread, because no one knows where the thread is going,
Suspending a thread after resuming the thread may cause some problems, and then you will easily be unable to resolve it, so Microsoft
Now it's the Thread. Resume () and Thread. Suspend () is obsolete and will be removed soon. NET Library
But won't you be able to hang up or recover from the removal? I think it's impossible, after all, it's so much better that we go back
to the Chase . If you need to do this, I want you to use AutoResetEvent. (Automatically resets single event objects)
, because it will be safe but safe enough to see how the developer 's own code is written.
private void Form1_Load (object sender, EventArgs e) { thread fd_thr = new Thread (this. FileDownload); Fd_thr. Start (); Fd_thr. Suspend (); Hangs thread, obsolete fd_thr. Resume (); Recovery thread, Obsolete } private void FileDownload () { byte[] buffer = null; using (WebClient client = new WebClient ()) buffer = client. Downloaddata ("http://www.baidu.com"); Console.WriteLine (Encoding.UTF8.GetString (buffer)); }
The following code does not seem to be a problem, but you may get an error in the output panel " thrown by the exception:
" System.IO.IOException" (in mscorlib.dll)"then this will involve a problem, I
People are annoying code throwing errors, first you need to know that one but throwing an error means that you cannot release resources and do not
say The most important throw error frequency is too high your program means that the stability difference will probably hang itself
off, Each throw error means a stack overflow, trycatch as little as possible, why you need to understand
Private AutoResetEvent fd_thr_supend = new AutoResetEvent (false); private void Form1_Load (object sender, EventArgs e) { thread fd_thr = new Thread (this. FileDownload); Fd_thr. Start (); This.fd_thr_supend. Reset (); Suspends thread this.fd_thr_supend. Set (); Recovery Thread } private void FileDownload () { byte[] buffer = null; using (WebClient client = new WebClient ()) buffer = client. Downloaddata ("http://www.baidu.com"); This.fd_thr_supend. WaitOne (); Blocking, waiting for signal Console.WriteLine (Encoding.UTF8.GetString (buffer)); }
The above is used in conjunction with the AutoResetEvent method to suspend or resume thread, so that the beneficial code
Security and reduce the chance of error, because you are not really the thread to hang up but to block the thread that one
Once an error occurs, you can troubleshoot the error as early as possible, not because of Thread. Resume () and Thread. Sus
pend () mode a hang, once the error occurs you can not find the cause of the error, greatly increased the stability
Sex and security.
[DllImport ("kernel32.dll", SetLastError = True)] private static extern int GetCurrentThreadID (); [DllImport ("kernel32.dll", SetLastError = True)] private static extern int SuspendThread (IntPtr hthread); [DllImport ("kernel32.dll", SetLastError = true)] public static extern uint ResumeThread (IntPtr hthread); public int hcurrenththreadid; [DllImport ("kernel32.dll", SetLastError = true)] public static extern IntPtr openthread (int dwdesiredaccess, BOOL b Inherithandle, uint dwThreadID); public const int threadaccess_suspend_resume = 0x0002; private void Form1_Load (object sender, EventArgs e) {thread fd_thr = new Thread (this. FileDownload); Fd_thr. Start (); while (This.hcurrenththreadid = = 0); Wait thread is started IntPtr hcurrenththread = Openthread (Threadaccess_suspend_resume, False, (UINT) This.hcurrenththreadid ); Suspend thread SuspendThread (hcurrenththread); Recovery thread ResumeThread (hcurrenththread); } private void FileDownload () {this.hcurrenththreadid = GetCurrentThreadID (); byte[] buffer = NULL; using (WebClient client = new WebClient ()) buffer = client. Downloaddata ("http://www.baidu.com"); Console.WriteLine (Encoding.UTF8.GetString (buffer)); }
The above method is also unsafe, because the above approach is actuallyThread. Resume () andThread. Suspend ()of two functions
function, but a WINAPI scheme for replacing outdated commands, and of course some approach. Anyway suspend or resume
A thread approach and many of the above three methods can actually be considered stability is true.
C # thread hangs and restores