I have a class that I want to make thread-safe and am investigating the ISyncronizeInvoke interface and wondering just what it will take to implement this interface. So far the basic concept of my code is: - 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: - 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
3 3202 vanc 211
Expert 100+
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(WorkingMethod);
worker.BeginInvoke(new AsyncCallBack(FinishMethod),"status"); //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.
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(WorkingMethod);
worker.BeginInvoke(new AsyncCallBack(FinishMethod),"status"); //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.
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 ISynchronizeInvoke interface - lets call it MyInvokeClass (because imagination is running short) . I pass a delegate into MyInvokeClass.Invoke 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.Invoke 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
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Edward |
last post by:
Hello
I hope someone could help me
I'm trying to prevent code from running before the thread I created completes. Here's the code snippet
DataTransformerWorker dtw = new...
|
by: Z D |
last post by:
Hello,
I'm having a strange problem that is probably due to my lack of
understanding of how threading & COM Interop works in a WinForms.NET
application.
Here's the situation:
I have a 3rd...
|
by: Dan |
last post by:
I've created a pocketpc app which has a startup form containing a listview.
The form creates an object which in turn creates a System.Threading.Timer.
It keeps track of the Timer state using a...
|
by: RCS |
last post by:
I have a UI that needs a couple of threads to do some significant processing
on a couple of different forms - and while it's at it, update the UI (set
textboxes, fill in listviews). I created a...
|
by: Elliot Rodriguez |
last post by:
Hi:
I am writing a WinForm app that contains a DataGrid control and a StatusBar
control. My goal is to update the status bar using events from a separate
class, as well as some other simple...
|
by: Eric Sabine |
last post by:
OK, I'm trying to further my understanding of threading. The code below I
wrote as kind of a primer to myself and maybe a template that I could use in
the future. What I tried to do was pass data...
|
by: Pawan Narula via DotNetMonster.com |
last post by:
hi all,
i'm using VB.NET and trying to code for contact management in a tree. all my contacts r saved in a text file and my C dll reads them one by one and sends to VB callback in a sync mode...
|
by: Christian Kaiser |
last post by:
We have a component that has no window. Well, no window in managed code - it
uses a DLL which itself uses a window, and this is our problem!
When the garbage collector runs and removes our...
|
by: Mike P |
last post by:
I am trying to write my first program using threading..basically I am
moving messages from an Outlook inbox and want to show the user where
the process is up to without having to wait until it has...
|
by: Sebouh |
last post by:
Hi guys. I was messing with Threading and stuff, and i have reached a point where i'm not sure what's causing the current behavior.
Here's the code:
Public Class Form1
Private Sub...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |