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: - 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 3238 vanc 211
Recognized Expert New Member
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.
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.
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
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 DataTransformerWorker(strApplicationFolder, strSupplier
strFile, lblProgress, ProgressBar1)
//create new threa
ThreadStart tdStart = new ThreadStart(dtw.StartProcess)
|
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 party COM component that takes about 5 seconds to run one of
its functions (Network IO bound call). Since I dont want my GUI to freeze
|
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 TimerState object similar to the
example in the System.Threading.Timer documentation. The method which
handles the timer events, among other things, periodically calls a method in
this TimerState object which raises an event to the startup form,...
|
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 base class for the worker class,
and made up some functions/delegates to handle the invoke stuff for the UI
and that was fine for a prototype. I rewrote this chunk, broke things out
into different classes - but the threading is still the same - and...
|
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 things.
The method I am writing queries a large dataset. As part of my feedback to
the user, I am updating the status bar when the connection is made and the
dataset is actually retrieved. The dataset retrieval method I have placed on
a separate...
| |
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 into a background thread and
get other data out and also update the main thread on which the main form
was created. It seems to work fine. The basic function of the app is
cheesy, I didn't spend any time on exception handling. northwind.mdb...
|
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 thread. so far so good. all contacts r added properly.
now when another login adds me in his contact, i recv a subscription, so i popup a form and ask for accept/reject. this all happens in a separate thread. popup form gets opened and choice is...
|
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 component (created
dynamically by, say, a button click, and then not referenced any more), the
GC runs in a different thread, which prohibits the DLL to destroy its
window, resulting in a GPF when the WndProc of that window is called - the
code is gone...
|
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 finished. I am
trying to follow this example :
http://www.codeproject.com/cs/miscctrl/progressdialog.asp
But although the messages still get moved, the progress window never
does anything. Here is my code in full, if anybody who knows...
|
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 Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim nt As New newThread(Me)
Dim t As New Threading.Thread(AddressOf nt.ThreadCode)
t.Start()
End Sub
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |