By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,667 Members | 2,678 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,667 IT Pros & Developers. It's quick & easy.

VB.net Multithreading Problem

P: n/a
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
Jul 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Travis,

You need to setup a Delegate then call the delegate function from your invoke. You're on the right track for sure.

look at:
http://groups-beta.google.com/group/...ea5bacc8070fd3

www.projectthunder.com
-Calvin Luttrell
Projectthunder.com, Inc.
"thowle" <th****@brtc.net> wrote in message news:D8**********************************@microsof t.com...
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

Jul 21 '05 #2

P: n/a
thowle <th****@brtc.net> wrote:
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.


See http://www.pobox.com/~skeet/csharp/t...winforms.shtml

Note that even when it *was* working for you, there's no guarantee
about it - you really shouldn't touch the UI at all from a non-UI
thread.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3

P: n/a
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?

"thowle" wrote:
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

Jul 21 '05 #4

P: n/a
thowle <th****@brtc.net> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #5

P: n/a
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.


"Jon Skeet [C# MVP]" wrote:
thowle <th****@brtc.net> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #6

P: n/a
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 :)

"thowle" wrote:
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.


"Jon Skeet [C# MVP]" wrote:
thowle <th****@brtc.net> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.