locked
Multithreading in a console application RRS feed

  • Question

  • I have a task to perform an ADSI query against a number of domain controllers, about 200 of them. The result of this query returned as a DataSet. I then add each returned DataSet into the List(Of DataSet) to perform some analysis later. The idea is to run these queries simultaneously, say 20 queries at a time, otherwise it takes forever to execute the task.

     It works very well in Windows Forms application. I inherited a class from .NET Control class. Within this class I use semaphore to make sure that the query is running on 20 threads at one time. Then from each thread I am raising events using Control.Invoke and passing dataset or exception description as properties of the custom event argument class. The event handler extracts DataSet from event argument and adds it to the collection.

    Now I need to transform this Forms application into the console one. The Control.Invoke-based approach I use does not work in a non-Windows Forms application. I looked through MSDN documentation but still wasn’t able to put all pieces together.

    I was wondering if anyone can help me with this.

    Thank you.

    Wednesday, December 5, 2007 8:22 PM

Answers

  • Hi VNZ,

     

    Based on your post, my understanding of your question is about the multithreading application. You need to implement a class which can receive notification that an event has occurred and responds to queries about the event. If I misunderstood your question, please tell me.

     

    In the console application scenario you can't use the control.Invoke method. It executes a delegate on the thread that owns the control's underlying window handle. You can make your class to implement the ISynchronizeInvoke Interface to provide a way to synchronously or asynchronously execute a delegate. For more information about this interface, visit: http://msdn2.microsoft.com/en-us/library/system.componentmodel.isynchronizeinvoke(VS.71).aspx.

     

    And also note that we need to ensure that the thread accesses the data by the thread-safe means in the multithreading application. You can create a thread-safe class to avoid the multi threads to simultaneously access the object. You can use the Mutex or Monitor to create the thread-safe class. If you have any further questions, tell me.

     

     

    Best regards,

    Riquel

    Friday, December 7, 2007 5:35 AM
    Moderator

All replies

  • Hi VNZ,

     

    Based on your post, my understanding of your question is about the multithreading application. You need to implement a class which can receive notification that an event has occurred and responds to queries about the event. If I misunderstood your question, please tell me.

     

    In the console application scenario you can't use the control.Invoke method. It executes a delegate on the thread that owns the control's underlying window handle. You can make your class to implement the ISynchronizeInvoke Interface to provide a way to synchronously or asynchronously execute a delegate. For more information about this interface, visit: http://msdn2.microsoft.com/en-us/library/system.componentmodel.isynchronizeinvoke(VS.71).aspx.

     

    And also note that we need to ensure that the thread accesses the data by the thread-safe means in the multithreading application. You can create a thread-safe class to avoid the multi threads to simultaneously access the object. You can use the Mutex or Monitor to create the thread-safe class. If you have any further questions, tell me.

     

     

    Best regards,

    Riquel

    Friday, December 7, 2007 5:35 AM
    Moderator
  • Thank you Riquel,

    I didn’t think about implementing ISynchronizeInvoke Interface, it seems like a good idea.

     I overcome the issue by removing the delegates from my class altogether. I found in a console application there is no problem to handle events raised by the method running on another thread. This problem (cross-thread reference) seems to occur only in Windows Forms application.  

     

    Friday, December 7, 2007 7:55 PM