C # multithreaded notes (i)

Source: Internet
Author: User
Tags execution join message queue mutex sleep thread
Notes | Multithreading 1. Each form has its own running on a different thread, and if you need to interact between forms, you need to interact between threads.

2. When the thread sleeps, the system exits the execution queue for a period of time, and when the sleep is over, the system produces a clock interrupt, which
Causes the thread to return to the execution queue, thereby resuming the execution of the thread.

3. If the parent thread ends before the child thread, the child thread is forced to end at the end of the parent thread. The Thread.Join () method causes the parent thread to wait until the child thread ends.
? The result of the Abort () method is a unrecoverable terminating thread.

4. The starting thread can be called the main thread, and if all foreground threads are stopped, the main thread can terminate, and all background threads will terminate unconditionally.
? There is only one difference between the background thread and the front thread, which is that the background thread does not interfere with the termination of the program. Once all of the foreground threads of a process are terminated, the CLR
? The process will be completely terminated by calling the Abort () method of any surviving background process.
?
6. Hang up, sleep (all can be called--blocking, pausing)
?? Unlike Thread.Sleep, Thread.Suspend does not stop the thread from executing immediately. It is not possible to suspend the thread until the thread arrives at a safe point. If the line Cheng
?? is not started or has stopped, it cannot be suspended. Calling Thread.Resume will cause another thread to jump out of the suspend state and cause the thread to continue executing.
?? One thread cannot call sleep on another thread, but one thread can call suspend on another thread.
?? You can also use many other ways to block threads. For example, you can make a thread wait for another thread (child thread) to stop by calling Thread.Join. Use
?? Monitor.Wait makes a thread wait to access a synchronization object.
?
5. Keyword lock can define a piece of code as a mutex segment (critical section), where the mutex only allows one thread to enter execution at a time while the other threads must wait.
? When multithreading is common to one object, the lock keyword should not be used, and here Monitor,monitor provides a scenario that allows threads to share resources.
? The Monitor class can lock an object, and a thread can manipulate the object only by getting the lock.
? Such as:
? Monitor.Enter (obj);
???????? Now the Oqueue object can only be manipulated by the current thread
? monitor.exit (obj);
?
6. A process begins with at least one main thread (that is, the primary execution instance), which is the master execution process that is created when the system loads your program.
? Message Queuing is related to thread (thread), and has one and only one message queue (queue) corresponding to the previous thread like Win2K.
? When was the message queue generated? On a win2k system, a thread is created from the start.
? A thread can create multiple forms. The messages sent to these windows are uniformly sent to the same message queue, thanks to the msg.hwnd in the message structure indicating that the message is
? Which window is relevant, the DispatchMessage () function is based on this guarantee that message dispatch handles automation without error!
?
7. Each form belongs to the thread that created it, and it is a run-time error (VS2005) to directly visit or indirectly ask a form in another thread.
? Workaround: Use the Control.Invoke (Delegate) method that the form inherits from Control. This method executes the method that the delegate points to on the thread on which the form is created.
? Note: Under VS2003, you can call the method of a form in another thread directly or indirectly in one thread without causing a run-time error.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.