Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > How do I access a component in my form from a thread created in a different form?


Reply
 
Thread Tools Display Modes
  #1  
Old 06-18-2011, 10:39 AM
Michael___ Michael___ is offline
Centurion
 
Join Date: Sep 2009
Posts: 111
Question How do I access a component in my form from a thread created in a different form?


Hello, I have attached my project files but, for any who doesn't want to download it or hasn't got access to Visual Basic, here is the scenario:

I have 2 forms main_frm and copying_frm. In main_frm, there is a list view that is populated with file names. The user then clicks save and all the files in the list are copied to a folder that they choose using a folder browser dialog. When this happens, I do copying_frm.ShowDialog() to display the copying_frm. The copying_frm contains a progress bar and a cancel button. I can get the progress bar to indicate how much has been copied. But that freezes up the application. To get around this, I create a new thread (called copyingThread) that copies the files. When that thread runs though, it can't access the waitingList_lvw in main_frm. For example, in the function that the copyingThread executes, if I have this line of code: MsgBox(main_frm.waitingList_lvw.Items.Count.ToString()) the result is 0. Also, I don't get any errors and no files are copied. How do I access waitingList_lvw in main_frm from a thread called copyThread that was created in copying_frm?
Attached Files
File Type: zip File Randomizer.zip (28.0 KB, 3 views)
Reply With Quote
  #2  
Old 06-19-2011, 01:31 AM
xer0syk0 xer0syk0 is offline
Centurion
 
Join Date: Oct 2004
Posts: 145
Default

You should read this topic concerning accessing controls from different threads:
http://www.xtremevbtalk.com/showthread.php?t=302665
Reply With Quote
  #3  
Old 06-20-2011, 10:19 AM
Michael___ Michael___ is offline
Centurion
 
Join Date: Sep 2009
Posts: 111
Default

Thank you for the link. I now have the function running on a separate processor, I think. But now I have another problem...

The function triggers as soon as the copying_frm is shown. If I place it in the load declaration then it runs before copying_frm is even visible. The shown declaration is run when the form is shown, not the controls within it. What happens is, the form opens and there are white boxes instead of the actual controls. Then, when it finishes copying, a message box appears (as i should) and the controls then load. How do I make it so that the copying only starts after all the controls are visible?

Also, this is all my code in the copying form. Is this definitely running as another thread. I have my doubts and just want to confirm whether or not it does do what I think it should do.

Code:
Option Strict On

Public Class copying_frm

    Private Delegate Sub showCopyProgressDelegate()

    Dim totalProgress As Decimal
    Dim copying As Boolean

    Private Sub showCopyProgress()
        If totalProgress >= 1 Then
            copying = False
            MessageBox.Show("New files created successfully!", "Completed", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Process.Start("explorer.exe", main_frm.folderBrowserDialog1.SelectedPath)
            cancel_btn.Enabled = False
            copyingProgressBar_pbr.Value = Convert.ToInt32(totalProgress * 100)
        End If
    End Sub

    Private Sub copyFiles()
        For Each item As ListViewItem In main_frm.waitingList_lvw.Items
            totalProgress += My.Computer.FileSystem.GetFileInfo(Convert.ToString(item.Tag)).Length / main_frm.sizeOfFiles
            IO.File.Copy(Convert.ToString(item.Tag), main_frm.folderBrowserDialog1.SelectedPath + "\" + item.SubItems(1).Text + IO.Path.GetExtension(Convert.ToString(item.Tag)))
        Next
    End Sub

    Private Sub copying_frm_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        totalProgress = 0
        copying = True
        copyFiles()
        Dim del As New showCopyProgressDelegate(AddressOf showCopyProgress)
        main_frm.waitingList_lvw.Invoke(del)
    End Sub
End Class
Reply With Quote
  #4  
Old 06-20-2011, 03:11 PM
PlausiblyDamp's Avatar
PlausiblyDamp PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 1,908
Default

There doesn't seem to be anywhere in the code you posted that is executing anything on another thread, is there any other code in the form that might be launching a thread?
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #5  
Old 06-20-2011, 03:36 PM
Michael___ Michael___ is offline
Centurion
 
Join Date: Sep 2009
Posts: 111
Default

Oh... Thanks. How would I start the thread though? I know you can declare a new thread like Dim aThread As System.Threading.Thread and athread.Start() but after reading the article that xer0syk0 posted a link to I discovered that I had to do it slightly different. That is the only code.
I also realise that the code
Code:
Dim del As New showCopyProgressDelegate(AddressOf showCopyProgress)
        main_frm.waitingList_lvw.Invoke(del)
should be
Code:
Dim del As New showCopyProgressDelegate(AddressOf copyFiles)
        main_frm.waitingList_lvw.Invoke(del)
and that I shouldn't be using this line
Code:
copyFiles()
But after the line
Code:
main_frm.waitingList_lvw.Invoke(del)
what do I need to put to make it so that the sub copyFiles is ran in a separate thread?
Reply With Quote
  #6  
Old 06-21-2011, 02:20 AM
PlausiblyDamp's Avatar
PlausiblyDamp PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 1,908
Default

It might be worth looking at the BackgroundWorker class for something like this, this would allow you to execute the copy in the background but report progress correctly on the UI thread.

If you search these forums you should find a few examples of how it works.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #7  
Old 06-23-2011, 12:38 PM
Michael___ Michael___ is offline
Centurion
 
Join Date: Sep 2009
Posts: 111
Cool

Thanks for that on the background class. It all works now. Just a few more tweaks though and it'll be done. I've attached the final project if anyone's interested in finding a use for it. Thanks again!
Attached Files
File Type: zip File Randomizer ZIP.zip (23.1 KB, 3 views)
Reply With Quote
Reply

Tags
access, component, form, thread, threads


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->