Results 1 to 8 of 8

Thread: Calling .DLL Method from Timer_Elapsed

  1. #1

    Thread Starter
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

    Calling .DLL Method from Timer_Elapsed

    Hi all,

    I have a .dll file (Interop.ACTMULTILib.dll) that I use to connect to a PLC. This .dll contains a sub called ReadDeviceBlock2(byval devicename as string, byval size as integer, byref data as short).

    My console application startup thingy is a module. When I call this method in my main it works fine, if I call it from another method that was called by main it works as well.

    However, it doesn't work when I call it from my Timer_Elapsed sub? I guess this has to do something with threads but I can't figure it out.

    Any ideas ? Been staring at it for 2 hours now .


    [EDIT]
    Timer.Start is called from Main()... maybe that has something to with it?
    Code:
    Module Main
    
        Private Timer As New System.Timers.Timer
        Private PLC As New ACTMULTILib.ActEasyIF
        Private DataSet As new DataSet
    
        Private _lanes As New List(Of Lane)
    
        Public Sub Main()
    
            LoadDataSet()
            ConnectToPLC()
    
            Console.WriteLine("Press any key to continue ...")
            Console.ReadKey()
            Console.Clear()
    
            'This works:
            Dim data As Short
            PLC.ReadDeviceBlock2("D880", 1, data)
    
            AddHandler Timer.Elapsed, New Timers.ElapsedEventHandler(AddressOf Timer_Elapsed)
            Timer.Interval = 1000
            Timer.Start()
    
            Console.WriteLine("Press any key to exit ...")
            Console.ReadKey()
    
        End Sub
    Code:
        Private Sub Timer_Elapsed(ByVal sender As Object, ByVal e As Timers.ElapsedEventArgs)
    
            'This doesn't work, it will hang here forever without throwing a error:
            Dim data As Short
            PLC.ReadDeviceBlock2("D880", 1, data)
    
        End Sub
    Last edited by gonzalioz; Nov 25th, 2009 at 07:43 AM.

  2. #2

    Thread Starter
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

    Re: Calling .DLL Method from Timer_Elapsed

    It works when I make a instance of ACTMULTILib.ACTEASYIF inside the timer elapsed and call readdeviceblock2 on that. So it has something to do with declaring the dll file on top of the code and the timer working on a different thread I think.

    Please help me .

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Calling .DLL Method from Timer_Elapsed

    I guess it's the way you addhandler to your timer...
    Try this and see if it works:
    Code:
    Module Main
        'Declare the timer with events
        Private WithEvents Timer1 As New System.Timers.Timer
        Private PLC As New ACTMULTILib.ActEasyIF
        Private DataSet As new DataSet
    
        Private _lanes As New List(Of Lane)
    
        Public Sub Main()
    
            LoadDataSet()
            ConnectToPLC()
    
            Console.WriteLine("Press any key to continue ...")
            Console.ReadKey()
            Console.Clear()
    
            'This works:
            Dim data As Short
            PLC.ReadDeviceBlock2("D880", 1, data)
    
            Timer1.Interval = 1000
            Timer1.Start()
    
            Console.WriteLine("Press any key to exit ...")
            Console.ReadKey()
    
        End Sub
    
        Private Sub Timer1_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
            Dim data As Short
            PLC.ReadDeviceBlock2("D880", 1, data)
            Console.WriteLine(data.ToString)
        End Sub
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  4. #4

    Thread Starter
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

    Re: Calling .DLL Method from Timer_Elapsed

    Thank you very much for your reply. That is a much neater way of adding the handler, thnx for that. However, it didn't work . I am going to copy paste the code in another project tommorrow and see if it makes a difference.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Calling .DLL Method from Timer_Elapsed

    Can you confirm that the call is even being made?

    The point to my question is this: The PLC object, declared in the module as it is, has no affinity for any particular thread. It is shared between all threads. In fact, it would only have thread affinity, normally, if it had been declared as a local variable within a method, which it is not. However, I don't know whether the interop or PLC does anything to restrict the thread that calls it to just the thread that created it. If that is the case, you could post the call to the synchronizationContext of the main thread. That would be easy to do, as you simply store the current synchronizationContext when you start the method, then Post a call to it. You may have to create a sub that calls the method in the PLC, and post a call to that sub, but you may be able to post the call directly to the PLC.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

    Re: Calling .DLL Method from Timer_Elapsed

    Thanks for your reply Shaggy Hiker.

    I found out that if I create a windows form application and do exactly the same thing it does work! Is there a difference between the way a windowsform app starts and a console app starts?

    ps : I placed the code from Main() to fire when a button is clicked.

  7. #7

    Thread Starter
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

    Re: Calling .DLL Method from Timer_Elapsed

    bump, anyone ?

  8. #8
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Calling .DLL Method from Timer_Elapsed

    Does the timer elapsed event fires at all?
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width