473,668 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Threading and updating form question.

I have a function that is currently wrapped up in a Class so I can pass a
variable to it.

This function is going to be threaded out and I would like the class
function to be able to update a control on my form. (Treeview).

Is this possible and how do I do it?
Here is a simplisitc overview of what I am doing...
Public Class Form1

Public Sub Button1_Click
GetDirs()
end sub

Public Sub GetDirs()
Dim dir As String
Dim i As Integer = 1
Dim dirs() As String = Directory.GetDi rectories("P:\" )
For Each dir In dirs
Dim t As System.Threadin g.Thread
dim oListView as new SubClass1
t = New System.Threadin g.Thread(Addres sOf
oListView.FileL istToListView)
oListView.sFold er = dir
'''' I thought by passing the Treeview1 control I could update
it in the class...
oListView.tView = TreeView1
t.Start()
i += 1
ProgressBar1.Va lue = 100 * (i / CLng(UBound(dir s)))
Next
End Sub

end class

Public Class SubClass1
Public sFolder as string
public tView as treeview
public sub FileListToListV iew
'''' **** Here is where I want to update the Form1.treeview1 control
tview.nodes.add (sFolder)
end sub
end class
Nov 21 '05 #1
4 1203
Hi Roger,

If I have understand your question, you should use something similar to:
[...]
dim oListView as new SubClass1
oListView.tView = Me.Treeview1
t = New System.Threadin g.Thread(Addres sOf oListView.FileL istToListView)
[...]

I hope that helps.

King Regards,

Jorge Serrano Pérez
MVP VB.NET

"Roger" wrote:
I have a function that is currently wrapped up in a Class so I can pass a
variable to it.

This function is going to be threaded out and I would like the class
function to be able to update a control on my form. (Treeview).

Is this possible and how do I do it?
Here is a simplisitc overview of what I am doing...
Public Class Form1

Public Sub Button1_Click
GetDirs()
end sub

Public Sub GetDirs()
Dim dir As String
Dim i As Integer = 1
Dim dirs() As String = Directory.GetDi rectories("P:\" )
For Each dir In dirs
Dim t As System.Threadin g.Thread
dim oListView as new SubClass1
t = New System.Threadin g.Thread(Addres sOf
oListView.FileL istToListView)
oListView.sFold er = dir
'''' I thought by passing the Treeview1 control I could update
it in the class...
oListView.tView = TreeView1
t.Start()
i += 1
ProgressBar1.Va lue = 100 * (i / CLng(UBound(dir s)))
Next
End Sub

end class

Public Class SubClass1
Public sFolder as string
public tView as treeview
public sub FileListToListV iew
'''' **** Here is where I want to update the Form1.treeview1 control
tview.nodes.add (sFolder)
end sub
end class

Nov 21 '05 #2
Use delegates:

Delegate Sub TreeViewAddNode Delegate(ByVal sNode As String)
Private AddNode As New TreeViewAddNode Delegate(Addres sOf AddTvNode)

Private Sub AddTvNode(ByVal sNode As String)
TreeView1.Nodes .Add(sNode)
End Sub

Then use the Invoke method to marshal the call from the executing thread to
the main UI thread:

TreeView1.Invok e(AddNode, New Object() {"Node1"})
hope that helps..
Imran.

"Roger" <da*****@netins .net> wrote in message
news:e3******** ******@TK2MSFTN GP09.phx.gbl...
I have a function that is currently wrapped up in a Class so I can pass a
variable to it.

This function is going to be threaded out and I would like the class
function to be able to update a control on my form. (Treeview).

Is this possible and how do I do it?
Here is a simplisitc overview of what I am doing...
Public Class Form1

Public Sub Button1_Click
GetDirs()
end sub

Public Sub GetDirs()
Dim dir As String
Dim i As Integer = 1
Dim dirs() As String = Directory.GetDi rectories("P:\" )
For Each dir In dirs
Dim t As System.Threadin g.Thread
dim oListView as new SubClass1
t = New System.Threadin g.Thread(Addres sOf
oListView.FileL istToListView)
oListView.sFold er = dir
'''' I thought by passing the Treeview1 control I could update
it in the class...
oListView.tView = TreeView1
t.Start()
i += 1
ProgressBar1.Va lue = 100 * (i / CLng(UBound(dir s)))
Next
End Sub

end class

Public Class SubClass1
Public sFolder as string
public tView as treeview
public sub FileListToListV iew
'''' **** Here is where I want to update the Form1.treeview1 control
tview.nodes.add (sFolder)
end sub
end class

Nov 21 '05 #3
When I add this I get the following error....

Additional information: The action being performed on this control is being
called from the wrong thread. You must marshal to the correct thread using
Control.Invoke or Control.BeginIn voke to perform this action.

Roger

"Jorge Serrano [MVP VB]"
<NO************ *******@NOQUIER OSPAMportalvbNO SPAM.com.NOQUIE ROSPAM> wrote in
message news:B5******** *************** ***********@mic rosoft.com...
Hi Roger,

If I have understand your question, you should use something similar to:
[...]
dim oListView as new SubClass1
oListView.tView = Me.Treeview1
t = New System.Threadin g.Thread(Addres sOf oListView.FileL istToListView)
[...]

I hope that helps.

King Regards,

Jorge Serrano Pérez
MVP VB.NET

"Roger" wrote:
I have a function that is currently wrapped up in a Class so I can pass a variable to it.

This function is going to be threaded out and I would like the class
function to be able to update a control on my form. (Treeview).

Is this possible and how do I do it?
Here is a simplisitc overview of what I am doing...
Public Class Form1

Public Sub Button1_Click
GetDirs()
end sub

Public Sub GetDirs()
Dim dir As String
Dim i As Integer = 1
Dim dirs() As String = Directory.GetDi rectories("P:\" )
For Each dir In dirs
Dim t As System.Threadin g.Thread
dim oListView as new SubClass1
t = New System.Threadin g.Thread(Addres sOf
oListView.FileL istToListView)
oListView.sFold er = dir
'''' I thought by passing the Treeview1 control I could update it in the class...
oListView.tView = TreeView1
t.Start()
i += 1
ProgressBar1.Va lue = 100 * (i / CLng(UBound(dir s)))
Next
End Sub

end class

Public Class SubClass1
Public sFolder as string
public tView as treeview
public sub FileListToListV iew
'''' **** Here is where I want to update the Form1.treeview1 control tview.nodes.add (sFolder)
end sub
end class

Nov 21 '05 #4
Roger <da*****@netins .net> wrote:
I have a function that is currently wrapped up in a Class so I can pass a
variable to it.

This function is going to be threaded out and I would like the class
function to be able to update a control on my form. (Treeview).


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

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
393
by: Tom B | last post by:
I've written the code below to try and figure out how threading works. My assumption is that when starting a new thread it would run at the same time as the original thread. Am I wrong? I've made a simple form with two text labels and a button. When I click the button, it creates a thread that updates one label, and the main code updates the other label. It appears that the created thread doesn't run until the main thread is
8
8192
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
17
1494
by: Arun Kumar | last post by:
What is wrong with this code. All i am trying to test is 3 progressbar and one button. On buttonclick i create 3 threads and each thread calls a method which in turn updates the progressbar and it works. I would to know if this can be used. Thanks private void button1_Click(object sender, System.EventArgs e) { ThreadStart job = new ThreadStart(onemethod);
5
2036
by: Robert W. | last post by:
I'm creating a WinForms app that will act as a companion (think administrator functionality) to a Pocket PC app. Generally the WinForms app works under just the UI thread. But if a Pocket PC connects to the desktop via ActiveSync then a separate thread is spawned. In addition to the main desktop app window I also have a Notification form that appears in the lower right of the screen and provides feedback to the user about what is...
7
318
by: Anthony Nystrom | last post by:
What is the correct way to stop a thread? abort? sleep? Will it start up again... Just curious... If the thread is enabling a form, if the form is disposed is the thread as well? Thanks, Anthony Nystrom
4
3569
by: Charles Law | last post by:
Hi guys. I have two threads: a main thread and a background thread. Lots of stuff happens in the background thread that means I have to update several (lots) of controls on a form. It is quite tiresome to have to write code to call MyControl.Invoke for each control on the form, along with the delegates that are required for each. Is there a better way to do this? What I mean is, if I could marshal the
2
4068
by: Chris Ashley | last post by:
Hi, I'm overriding WndProc to process some custom messages like so: protected override void WndProc(ref Message m) { if (m.Msg == ImageFileMsg.MSG_IF_NEW_DATA) { ProcessNewDataMessage(m);
3
395
by: Bill Schanks | last post by:
I have a form that gets opened, and it a separate thread I want a timer at the bottom of the form to update while the datagridview is updating. In my main form I have this: Dim t As New Thread(AddressOf ChildForm.UpdateRetrieveTimer) t.Start() <<Snip>> Here is the code for the thread:
1
1185
by: =?Utf-8?B?RnJhbmsgVXJheQ==?= | last post by:
Hi all I have a question about threading with forms. On my form I have a Progressbar and a Timer for updating it. On the timer update I call Invoke and this works fine. When it runs, the form is free. When I now execute something on the form, for this time my progreebar is stopping.
0
8459
marktang
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...
0
8378
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,...
0
8890
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8791
jinu1996
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...
1
8577
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8653
tracyyun
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...
0
7398
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5677
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();...
1
2786
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.