locked
waiting for a process to get completed before starting a new thread RRS feed

  • Question

  • Hi,

    i am using a com component and a method in the com component fires an event , and i want my other methods to wait for getting executed till that event is fired.

    i tried writing thread.sleep(10000) after the com method is called, but that is not working.

     

    how can i do that.


    litu Here
    Tuesday, March 8, 2011 4:30 AM

Answers

  • Hi Litu,

    Welcome to the MSDN forum.

    As far as I know, there are following ways to achieve the Thread Synchronization.

    1. Monitor Class

    The Monitor class (in the System.Threading namespace) is useful in situations where you want a particular region of code to be used only by a single thread at a given point of time. All the methods in this class are static so you do not need to instantiate this class. These static methods provide a mechanism to synchronize access to objects thus protecting them against data-races or deadlocks. A few of the methods are shown below:  

    • Enter, TryEnter 
    • Exit 
    • Pulse / PulseAll 
    • Wait

    A small function to study the Monitor class methods as shown below:

    public void some_method()
    {
    int a=100;
    int b=0;
    Monitor.Enter(this);
    //say we do something here.
    int c=a/b;
    Monitor.Exit(this);
    }

     

    The above code when written using the C#'s lock() method looks like this:

    public void some_method()
    {
    int a=100;
    int b=0;
    lock(this);
    //say we do something here.
    int c=a/b;

    2.WaitHandle Class 

    The WaitHandle class (in the System.Threading namespace) is used as a base class for all synchronization objects that allow multiple wait operations. This class encapsulates the Win32 synchronization handles. WaitHandle objects signal the status of one thread to another thereby notifying other threads that they need exclusive access to a resource. Other threads must then wait, until the wait handle is no longer in use, to use this resource. Classes derived from this are:

    • Mutex 
    • AutoResetEvent 
    • ManualResetEvent

    3. Mutex Class 

    The Mutex class (in the System.Threading namespace) is another way of achieving synchronization between threads and across processes. This class provides interprocess synchronization. It allows a thread to have exclusive access to a shared resource thus preventing simultaneous access by multiple threads or processes. The name mutex itself suggests that the ownership of the mutex is mutually exclusive. Once one thread acquires a mutex the other thread that wants to acquire the mutex is suspended till the first thread releases it. The method Mutex.ReleaseMutex must be called to release the mutex. A thread can request the same mutex in repeated calls to Wait but must call the Mutex.ReleaseMutex the same number of times to release the ownership of the mutex. If no thread owns a mutex (or the thread that owns a mutex, terminates normally) then the state of the Mutex object is set to signaled otherwise nonsignaled.  Once the state is set to signaled the next thread waiting in the queue acquires the mutex. The Mutex class corresponds to the Win32 CreateMutex call.

    Following code is a very simple illustration of how to create a Mutex object.

    public void some_method()
    {
    int a=100;
    int b=20;
    Mutex firstMutex = new Mutex(false);
    FirstMutex.WaitOne();
    //some kind of processing can be done here.
    Int x=a/b;
    FirstMutex.Close();
    }

    For more information about Thread Synchronization, please refer to the following links.

    Thread Synchronization

    Thread Synchronization (C# and Visual Basic)

    Hope it is helpful for you.
    Best Regards

    Cookie Luo[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Cookie Luo Friday, March 18, 2011 1:34 AM
    Wednesday, March 9, 2011 8:07 AM

All replies

  • Hello litusahoo,

    Try using the Join method. Here's an example: http://www.java2s.com/Tutorial/CSharp/0420__Thread/UseJointowaitforthreadstoend.htm.


    Cornel Croitoriu - Senior Software Developer & Entrepreneur

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    CWS SoftwareBiz-Forward.comCroitoriu.NET

    Tuesday, March 8, 2011 6:39 AM
  • Hi,

    i am using a com component and a method in the com component fires an event , and i want my other methods to wait for getting executed till that event is fired.

    i tried writing thread.sleep(10000) after the com method is called, but that is not working.

     

    how can i do that.


    litu Here

    Use System.Threading.WaitHandle

    http://msdn.microsoft.com/en-us/library/system.threading.waithandle.aspx

     

     

    Tuesday, March 8, 2011 6:51 AM
  • Hi Litu,

    Welcome to the MSDN forum.

    As far as I know, there are following ways to achieve the Thread Synchronization.

    1. Monitor Class

    The Monitor class (in the System.Threading namespace) is useful in situations where you want a particular region of code to be used only by a single thread at a given point of time. All the methods in this class are static so you do not need to instantiate this class. These static methods provide a mechanism to synchronize access to objects thus protecting them against data-races or deadlocks. A few of the methods are shown below:  

    • Enter, TryEnter 
    • Exit 
    • Pulse / PulseAll 
    • Wait

    A small function to study the Monitor class methods as shown below:

    public void some_method()
    {
    int a=100;
    int b=0;
    Monitor.Enter(this);
    //say we do something here.
    int c=a/b;
    Monitor.Exit(this);
    }

     

    The above code when written using the C#'s lock() method looks like this:

    public void some_method()
    {
    int a=100;
    int b=0;
    lock(this);
    //say we do something here.
    int c=a/b;

    2.WaitHandle Class 

    The WaitHandle class (in the System.Threading namespace) is used as a base class for all synchronization objects that allow multiple wait operations. This class encapsulates the Win32 synchronization handles. WaitHandle objects signal the status of one thread to another thereby notifying other threads that they need exclusive access to a resource. Other threads must then wait, until the wait handle is no longer in use, to use this resource. Classes derived from this are:

    • Mutex 
    • AutoResetEvent 
    • ManualResetEvent

    3. Mutex Class 

    The Mutex class (in the System.Threading namespace) is another way of achieving synchronization between threads and across processes. This class provides interprocess synchronization. It allows a thread to have exclusive access to a shared resource thus preventing simultaneous access by multiple threads or processes. The name mutex itself suggests that the ownership of the mutex is mutually exclusive. Once one thread acquires a mutex the other thread that wants to acquire the mutex is suspended till the first thread releases it. The method Mutex.ReleaseMutex must be called to release the mutex. A thread can request the same mutex in repeated calls to Wait but must call the Mutex.ReleaseMutex the same number of times to release the ownership of the mutex. If no thread owns a mutex (or the thread that owns a mutex, terminates normally) then the state of the Mutex object is set to signaled otherwise nonsignaled.  Once the state is set to signaled the next thread waiting in the queue acquires the mutex. The Mutex class corresponds to the Win32 CreateMutex call.

    Following code is a very simple illustration of how to create a Mutex object.

    public void some_method()
    {
    int a=100;
    int b=20;
    Mutex firstMutex = new Mutex(false);
    FirstMutex.WaitOne();
    //some kind of processing can be done here.
    Int x=a/b;
    FirstMutex.Close();
    }

    For more information about Thread Synchronization, please refer to the following links.

    Thread Synchronization

    Thread Synchronization (C# and Visual Basic)

    Hope it is helpful for you.
    Best Regards

    Cookie Luo[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Cookie Luo Friday, March 18, 2011 1:34 AM
    Wednesday, March 9, 2011 8:07 AM