VB.net Multithreading Problem

G

Guest

Okay, I'm sure there is a very simple solution to my problems here and I am
just too tired, but then again maybe not.

Here is an example of my code:
Dim Conn_Thrd as new system.threading.thread(addressof connecttoserver())

public sub connecttoserver()
'This works good
Treeview1.nodes.add("Test Node")

'This doesnt
clrnd()

'Neither does this
me.treeview1.invoke(clrnd)
end sub

'This ONLY works good when it is called inside the thread containing the
treeview1 control.
public function clrnd()
Treeview1.nodes.clear()
end sub


'This works good
Private sub Button1_click (...) handles button1.click
clrnd()
end sub

'This works good
Private sub Button2_click (...) handles button2.click
Conn_Thrd = new system.threading.thread(addressof connecttoserver())
Conn_Thrd.start()
end sub


Please help :)

Thanks,
Travis
 
G

Guest

Okay, here is what I've done now. I created a class called "ConnectToServer"

in that class, it will fire off an event called "ConnectComplete"

In the mainform that contains GUI I have the sub to handle the event, and I
have it calling a function "GetServerList". However, it is still calling it
from inside the thread inwhich does not contain the GUI therefore it fails,
EVEN if i have a treeview1.invoke(GetServerList()).

How can I get this to work?
 
J

Jon Skeet [C# MVP]

thowle said:
Okay, here is what I've done now. I created a class called "ConnectToServer"

in that class, it will fire off an event called "ConnectComplete"

In the mainform that contains GUI I have the sub to handle the event, and I
have it calling a function "GetServerList". However, it is still calling it
from inside the thread inwhich does not contain the GUI therefore it fails,
EVEN if i have a treeview1.invoke(GetServerList()).

How can I get this to work?

You need to create a delegate from GetServerList, not call it directly.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
G

Guest

Bacically, here is the concept of my application in a smaller form.
(Not all of it is complete)

private sub Button1_click(...) handles button1.click
AddHandler Connect.Connected, AddressOf Connected

Dim ConnectThread as new system.threading.thread(address of Connect)
ConnectThread.start()
end sub

Sub Connected()
treeview1.invoke(GetServerList())
End sub

Public function GetServerList()
....access database....
treeview1.nodes.add("Value")
....close recordset, and db conn....
end function



Public Class Connect
Public Event Connected()
....connect to server....
....yay, we are connected :)....
RaiseEvent Connected()
End class

Thats about it.
 
G

Guest

Okay I got it working. and it was pretty wild :)

As Jon Skeet said, you must make a delegate for the GetServerList() function
as follows:

Delegate Function GetServerTView()
Public ReadOnly fnGetServerTView As GetServerTView

then:

Public Sub New()
fnGetServerTView = New GetServerTView(AddressOf Me.GetServerList)
end Sub

from here, you can call this from any thread as
treeview1.invoke(fnGetServerTView)!


Thx for help yall :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top