Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

BeginInvoke problem in Windows Service

Status
Not open for further replies.

jubble

Programmer
Mar 6, 2002
207
GB
I'm trying to convert a Windows Forms application to a Windows Service and am having problems with BeginInvoke with the error...

'Reference to a non-shared member requires an object reference'


Code:
 Dim ar As IAsyncResult = BeginInvoke(New ClearDataDelegate(AddressOf ClearData), o)
 EndInvoke(ar)
My understanding is that if BeginInvoke is called in a Windows Application without a control reference it will run on the thread that the form was created on. Is this correct?

How then can I use BeginInvoke in a Windows Service which has no UI?

Along the same vein, can anyone share some light on the best way to retrieve the thread that the Windows Service is running on?
 
Along the same vein, can anyone share some light on the best way to retrieve the thread that the Windows Service is running on?

Try Threading.CurrentThread err is it Threading.Thread.CurrentThread. one of them.

Combining the error message, with what you said, I'm going to guess that the line causing the problem is in a shared method. You are correct that BeginInvoke runs on the same thread that is processing the form. BeginInvoke is a member of the form class, so if you don't specify frmMain.BeginInvoke, or Me.BeginInvoke, it uses what ever is instatiated from the current class (Me). But in your case, this being a shared method, you can't access Me. So if you want to use BeginInvoke you need to instantiate the form (you don't have to show it!) and call BeginInvoke from a non-shared sub.

-Rick

----------------------

[monkey] I believe in killer coding ninja monkeys.[monkey]
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top