none
How to access a singlethread driver dll form a multitreaded application RRS feed

  • Question

  • Hi,

    I got the following problem:

    A Class as part of a .net dll with several functions:
    - This class can be created several times within one application by various threads
    - The functions take several miliseconds to execute and contain also have idle times (Thread.Sleep(x))
    - Under no circumstance must any of the functions be called at the same time (Multiple threads calling the functions in different copies of the class at the same time is also not allowed)
    - Optional: It would be nice if it was possibe for the functions to be stil compatible with the COM-Interface

    The Situation:
    The Class is part of a driver library that translates .Net commands into a series of Text Commands and Queries for RS232, COM-Objects and LAN-Sockets.
    For accessing the library with a multitreaded Application the problem arises that the driver tries to send multiple queries to the hardware before the previous queries have been processed causing the hardware to crash.

    Current Solution:
    Twice redundant interlocking procedures for every function with a class-wide shared interlocking flag
    Twice redundand because i had the issue that several treads were requesting operation at the same time and therefore detected both that the interlock was open, so i had to change the interlock to an integer value and try the interlocking procedure again and again until only one process gets through.

    Idea:
    Have a public function that passes the command on to one central thread for this class and wait until that thread has processed the command and return the result.
    Unfortunately i have not yet found a way to do that without complex interlocked command lists and 100s of lines of code.
    I would like to keep it as simple as possible due to the application being optimized for performance

    • Moved by Robert Breitenhofer Wednesday, July 18, 2012 10:44 AM English instead German (From:Visual Studio)
    Saturday, July 14, 2012 10:24 AM

Answers

  • Hi Cnotsch,

    Welcome to the MSDN Forum.

    >>Unfortunately i have not yet found a way to do that without complex interlocked command lists and 100s of lines of code.

    I don't agree with this. On my side, I just need three lines code to accomplish this goal:

        Public Shared _lock As New Object
        Public Sub JustAMethod()
            SyncLock _lock
    
            End SyncLock
        End Sub

    If I have misunderstood anything, please feel free to let me know.

    >>Have a public function that passes the command on to one central thread for this class and wait until that thread has processed the command and return the result.

    This is almost the same synchronous way. I have thought about threadpool, queue and so on, they are all lower efficiency than Lock.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by cnotsch Sunday, July 22, 2012 10:43 AM
    Thursday, July 19, 2012 10:11 AM
    Moderator

All replies

  • Hi Cnotsch,

    Welcome to the MSDN Forum.

    >>Unfortunately i have not yet found a way to do that without complex interlocked command lists and 100s of lines of code.

    I don't agree with this. On my side, I just need three lines code to accomplish this goal:

        Public Shared _lock As New Object
        Public Sub JustAMethod()
            SyncLock _lock
    
            End SyncLock
        End Sub

    If I have misunderstood anything, please feel free to let me know.

    >>Have a public function that passes the command on to one central thread for this class and wait until that thread has processed the command and return the result.

    This is almost the same synchronous way. I have thought about threadpool, queue and so on, they are all lower efficiency than Lock.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by cnotsch Sunday, July 22, 2012 10:43 AM
    Thursday, July 19, 2012 10:11 AM
    Moderator
  • Interesting.. inever saw the synclock statement. let me do some reading on that.

    So far i have been locking a class with a Bool/Int variable that indicates if it is locked or not.

    Sunday, July 22, 2012 8:35 AM
  • I just tried out what you suggested and it seems to work perfectly, thanks
    Sunday, July 22, 2012 10:43 AM