What are the ways to implement threads in C #?
There are various ways to use streaming; some of which are associated with explicit thread creation, while others use already running threads.
How do you create a dead end, and if there is a dead end, how do you exit it (in C #)?
Here are three different ways to turn off a dead end. This list is not exhaustive.
Call the lock method from the lock section.
In this example, thread A gets a lock, and then immediately calls the lock method, while thread B tries to get the same lock, but gets a hang because thread A waits for thread B to signal an event before it will release the lock.
public class Example { ManualResetEvent m_Event = new ManualResetEvent(false); void ThreadA() { lock (this) { m_Event.WaitOne(); } } void ThreadB() { lock (this) { m_Event.Set(); } } }
Get two locks out of order.
No explanation is required here, as this is a well-known problem.
public class Example { private object m_LockObjectA = new object(); private object m_LockObjectB = new Object(); void ThreadA() { lock (m_LockObjectA) lock (m_LockObjectB) { } } void ThreadB() { lock (m_LockObjectB) lock (m_LockObjectA) { } } }
Lock lock.
This is one of my favorite illustrations of the dead end because the lock or block method is not involved. The subtleties of the problem are enough to confuse even those who are familiar with threads. The problem here is the lack of memory barriers. Thread A expects thread B to set the signal flag, while thread B waits for thread A to reset, all this time, none of the threads see the changes that the other makes, because the compiler, JIT and hardware are free to optimize reading and writing a flag in a way that is not intuitive.
public class Example { private bool m_Signal = false; void ThreadA() { while (!m_Signal); m_Signal = false; } void ThreadB() { m_Signal = true; while (m_Signal); } }
How does a background worker work implemented?
Here is a very simple step-by-step procedure for you to get started.
- Add an event handler that does the actual work with the
DoWork event. - Add an event handler to get progress information in the
ProgressChanged event. - Add an event handler that will be executed after the
RunWorkerCompleted event is RunWorkerCompleted . - Call
RunWorkerAsync from the user interface thread to start the background. This raises the DoWork event in a separate thread. - Call
ReportProgress periodically from the DoWork event DoWork to publish new progress information. This raises the ProgressChanged event in the user interface thread.
Brian gideon
source share