Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / October 2004

Tip: Looking for answers? Try searching our database.

Threading and updating form question.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Roger - 27 Oct 2004 21:03 GMT
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.GetDirectories("P:\")
       For Each dir In dirs
           Dim t As System.Threading.Thread
           dim oListView as new SubClass1
           t = New System.Threading.Thread(AddressOf
oListView.FileListToListView)
           oListView.sFolder = dir
           '''' I thought by passing the Treeview1 control I could update
it in the class...
           oListView.tView = TreeView1
           t.Start()
           i += 1
           ProgressBar1.Value = 100 * (i / CLng(UBound(dirs)))
       Next
End Sub

end class

Public Class SubClass1
Public sFolder as string
public tView as treeview
public sub FileListToListView
    '''' **** Here is where I want to update the Form1.treeview1 control
    tview.nodes.add(sFolder)
end sub
end class
Jorge Serrano [MVP VB] - 27 Oct 2004 21:39 GMT
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.Threading.Thread(AddressOf oListView.FileListToListView)
[...]

I hope that helps.

King Regards,

Jorge Serrano Pérez
MVP VB.NET

> I have a function that is currently wrapped up in a Class so I can pass a
> variable to it.
[quoted text clipped - 40 lines]
>  end sub
> end class
Roger - 27 Oct 2004 21:48 GMT
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.BeginInvoke to perform this action.

Roger

> Hi Roger,
>
[quoted text clipped - 56 lines]
> >  end sub
> > end class
Imran Koradia - 27 Oct 2004 21:45 GMT
Use delegates:

Delegate Sub TreeViewAddNodeDelegate(ByVal sNode As String)
Private AddNode As New TreeViewAddNodeDelegate(AddressOf 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.Invoke(AddNode, New Object() {"Node1"})

hope that helps..
Imran.

> I have a function that is currently wrapped up in a Class so I can pass a
> variable to it.
[quoted text clipped - 40 lines]
>  end sub
> end class
Jon Skeet [C# MVP] - 27 Oct 2004 22:24 GMT
> 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/threads/winforms.shtml

Signature

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.