 |
 |

06-18-2011, 10:39 AM
|
|
Centurion
|
|
Join Date: Sep 2009
Posts: 111
|
|
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?
|
|

06-19-2011, 01:31 AM
|
|
Centurion
|
|
Join Date: Oct 2004
Posts: 145
|
|
|

06-20-2011, 10:19 AM
|
|
Centurion
|
|
Join Date: Sep 2009
Posts: 111
|
|
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
|
|

06-20-2011, 03:11 PM
|
 |
Ultimate Contributor
Forum Leader * Expert *
|
|
Join Date: Nov 2003
Location: Wigan, UK
Posts: 1,676
|
|
|
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?
|
|

06-20-2011, 03:36 PM
|
|
Centurion
|
|
Join Date: Sep 2009
Posts: 111
|
|
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 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?
|
|

06-21-2011, 02:20 AM
|
 |
Ultimate Contributor
Forum Leader * Expert *
|
|
Join Date: Nov 2003
Location: Wigan, UK
Posts: 1,676
|
|
|
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.
|
|

06-23-2011, 12:38 PM
|
|
Centurion
|
|
Join Date: Sep 2009
Posts: 111
|
|
|
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!
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|