Threading and Invoke

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • balabaster
    Recognized Expert Contributor
    • Mar 2007
    • 798

    #1

    Threading and Invoke

    I have a class that I want to make thread-safe and am investigating the ISyncronizeInvo ke interface and wondering just what it will take to implement this interface. So far the basic concept of my code is:

    Code:
    Public Class MyClass1
        Implements System.ComponentModel.ISynchronizeInvoke
    
        Private _InvokeRequired As Boolean = False
    
        Public Function BeginInvoke(ByVal method As System.Delegate, ByVal args() As Object) As System.IAsyncResult Implements System.ComponentModel.ISynchronizeInvoke.BeginInvoke
    
        End Function
    
        Public Function EndInvoke(ByVal result As System.IAsyncResult) As Object Implements System.ComponentModel.ISynchronizeInvoke.EndInvoke
    
        End Function
    
        Public Function Invoke(ByVal method As System.Delegate, ByVal args() As Object) As Object Implements System.ComponentModel.ISynchronizeInvoke.Invoke
    
        End Function
    
        Public ReadOnly Property InvokeRequired() As Boolean Implements System.ComponentModel.ISynchronizeInvoke.InvokeRequired
            Get
                Return _InvokeRequired
            End Get
        End Property
    
    End Class
    So...where do I go from here? My first thought is that it seems reasonable to do my work in the Invoke function; in the BeginInvoke method, I spin off a thread that calls the Invoke method; in the EndInvoke I would just stop the thread started by my invoke method.

    The question is, what goes on in the Invoke method? I can't seem to wrap my head around the fact that the Invoke method doesn't actually have any static purpose except to run the delegate...but what does it do with it?

    Maybe I'm just short on caffeine today, but I'm having trouble getting to grips with what exactly I'm supposed to do now that I'm looking at this empty class. If anyone can give me a couple of pointers I'm sure I can figure the rest out for myself.

    I understand delegates from the other side of the fence...i.e. calling the Invoke function using the concept:
    Code:
    Public Delegate Sub MyDelegate(ByVal MyParams As Object)
    Public Function MyFunction(ByVal MyParams As Object)
      If Me.InvokeRequired Then
        Me.Invoke(New MyDelegate(AddressOf MyFunction), MyParams)
      Else
        'Do stuff
      End If
    End Function
    But on the side of actually implementing the Invoke method, I'm a little bit lost...

    I'm grateful for any direction anyone can provide.
    Cheers

    C# or VB code is fine
  • vanc
    Recognized Expert New Member
    • Mar 2007
    • 211

    #2
    The Invoke method will jump to the method that you pass to it and work on that method, so the Invoke basically is to call another method in simple. The method that you pass to Invoke method is actual target method that you should implemented. EndInvoke is to force the thread to stop the invoke process and end it, normally if you want to make some work that should be done on another thread and you don't care when it finish you can use MethodInvoker, which will use ThreadSpool threads to do the job, so that you don't have to initiate threads yourself.
    If you want the working thread notify you when it's done the job you can use BeginInvoke method instead of just Invoke and provide a CallBack method to perform any action that you want it to show you when it's done the job.
    I'll brief you with the following e.g.
    MethodInvoker worker = new MethodInvoker(W orkingMethod);
    worker.BeginInv oke(new AsyncCallBack(F inishMethod),"s tatus"); //status can be null
    By the above lines, you have to implement WorkingMethod and FinishMethod, so that when the worker thread is done its job, it then call the FinishMethod to notify you, but remember don't use cross thread method in those methods :D, you know it already don't you!!!

    cheers.

    Comment

    • balabaster
      Recognized Expert Contributor
      • Mar 2007
      • 798

      #3
      Originally posted by vanc
      The Invoke method will jump to the method that you pass to it and work on that method, so the Invoke basically is to call another method in simple. The method that you pass to Invoke method is actual target method that you should implemented. EndInvoke is to force the thread to stop the invoke process and end it, normally if you want to make some work that should be done on another thread and you don't care when it finish you can use MethodInvoker, which will use ThreadSpool threads to do the job, so that you don't have to initiate threads yourself.
      If you want the working thread notify you when it's done the job you can use BeginInvoke method instead of just Invoke and provide a CallBack method to perform any action that you want it to show you when it's done the job.
      I'll brief you with the following e.g.
      MethodInvoker worker = new MethodInvoker(W orkingMethod);
      worker.BeginInv oke(new AsyncCallBack(F inishMethod),"s tatus"); //status can be null
      By the above lines, you have to implement WorkingMethod and FinishMethod, so that when the worker thread is done its job, it then call the FinishMethod to notify you, but remember don't use cross thread method in those methods :D, you know it already don't you!!!

      cheers.
      Hey Vanc, thanks for your input, it's late here and I've given what you've said the once over. I think this will make more sense in the morning once I've slept on it. And yes, I already know about cross threading :oP Always good to throw in the extra info though, you never know who it might help after me.

      Comment

      • balabaster
        Recognized Expert Contributor
        • Mar 2007
        • 798

        #4
        Originally posted by balabaster
        Hey Vanc, thanks for your input, it's late here and I've given what you've said the once over. I think this will make more sense in the morning once I've slept on it. And yes, I already know about cross threading :oP Always good to throw in the extra info though, you never know who it might help after me.
        Okay, I'm just revisiting this issue and finding that I must be missing something because it's just not jiving in my head.

        I have my class that is implementing the ISynchronizeInv oke interface - lets call it MyInvokeClass (because imagination is running short) . I pass a delegate into MyInvokeClass.I nvoke which will be invoked by the thread that created the instance of the class initially instead of the thread that actually called the Invoke method. I get that concept...I think.

        One thing that's bugging me is: Is there anything stopping me passing any random delegate function into my class's invoke method rather than a method contained within my InvokeClass? It seems to me that I could pass any delegate into the method parameter which isn't really what I want...

        Another thing is how do I tell my InvokeClass.Inv oke method to actually process the method passed in the method parameter i.e. process the delegate methods that are waiting in the invoke queue? I've passed the address of my method (which is what the delegate is, right?) to the invoke method so now this is held in the method parameter. So what do I do with that? How do I tell the Invoke method what to do with that?

        I think once I understand that tiny bit of information I should be able to take the rest of it from there.

        I'm having trouble finding satisfactory documentation on threading. As with most of Microsoft's (and everyone else's) documentation, all the basic "consumer" stuff is there in minute detail, but if you want to do anything half way complicated, the most complicated thing is finding the information that explains it properly.

        Cheers

        Comment

        Working...