Houlahan
Houlahan

Reputation: 783

running a form on another thread

what i currently have is the main form that then when a user presses a button it runs some functions that loops through some files and changes them and then copys them to another location the functions can take quite some time. what i want to is when the button is pressed it opens another form with a textbox on it and when it has finished 1 cycle of the loop is outputs a line to the textbox and tells the user weather or not it has been sucsessfull.

at the moment i have:

    Dim t As Threading.Thread
    t = New Threading.Thread(AddressOf Form3.Show)
    t.Start()

and when the end of that cycle finishes just append some text to that textbox:

for each x in list
    'copy some files
    Form3.RichTextBox1.AppendText("Cycle 1 compleate")
loop

while it runs the form just flashes a coupple of times?

Thanks

Upvotes: 0

Views: 1425

Answers (1)

Nick Butler
Nick Butler

Reputation: 24383

You should keep all UI controls on the UI thread - that includes whole Forms.

You can run the file operations in the background, and only marshal back to the UI thread when you want to append a message.

The BackgroundWorker class would be a good fit here.

There is an excellent and free ebook here: Albahari.

Upvotes: 2

Related Questions