473,322 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Threading question "Controls created on one thread cannot be parented to a control on a different thread."

VMI
Im using a separate thread in my code to copy a huge file, but I get the
"Controls created on one thread cannot be parented to a control on a
different thread" when I want to update the Form controls after makeBackup
is complete. I know what the problem is (I'm trying to update controls from
a thread that did not create them), but how can I fix it? This my code:

private delegate void BackupCompletedEventHandler();
private event BackupCompletedEventHandler backupCompleted;
private void btn_run_click(object sender, System.EventArgs e)
{
if (myMsgs.createBackup()) //File Copy will be made
{
this.backupCompleted += new
BackupCompletedEventHandler(onBackupCompleted);
Thread t = new Thread(new ThreadStart(makeBackup));
t.Start();
}
}
private void onBackupCompleted()
{
statusBar_audit.Panels[0].Text = "Done with backup"; /* I GET THE
ERROR HERE */
dataGrid_auditAddress.DataSource = null;

}
private void makeBackup()
{
File.Copy(_sFileName, _sFileName + ".bak");
onBackupCompleted();
}
Thanks for the help.
Nov 16 '05 #1
1 14191
VMI wrote:
Im using a separate thread in my code to copy a huge file, but I get the
"Controls created on one thread cannot be parented to a control on a

<snip>

Don't mess with visual controls from your threads, they're not thread-safe.

Make a separate method to set your status panel text, and use the
Form.Invoke method to call it, Invoke will call the method on the main
GUI thread, ensuring that you won't have problems with threading in this
context.

ie. something like:

private delegate void SetStatusBarPanelDelegate(String newText);
private void SetStatusBarPanel(String newText)
{
statusBar_audit.Panels[0].Text = newText;
}

and in your onBackupCompleted:

Invoke(new SetStatusBarPanelDelegate(SetStatusBarPanel),
new Object[] { "Done with backup" });

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x0270466B
Nov 16 '05 #2

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

Similar topics

4
by: sashan | last post by:
I'm trying to pass arguments to my the run method of my class that inherits from threading.Thread. class Reader(threading.Thread): def __init__(self, conn): threading.Thread.__init__(self,...
0
by: Christopher J. Bottaro | last post by:
Hello, I'm new to Python programming, so please excuse me. thread = threading.Thread(self.somefunc()) thread.start() print "Thread started" def somefunc(self): while (1)
3
by: Peter Hansen | last post by:
I'm still trying to understand the behaviour that I'm seeing but I'm already pretty sure that it's either a bug, or something that would be considered a bug if it didn't perhaps avoid even worse...
2
by: Vjay77 | last post by:
In this code: Private Sub downloadBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) If Not (Me.downloadUrlTextBox.Text = "") Then Me.outputGroupBox.Enabled = True...
2
by: mattbennett | last post by:
Hi, I'm new to python, and having problems instantiating classes. My main method has a couple of lines like this: debug = False stats_d = {"cpu":,"foo":,"bar":,"baz":} ...
3
by: =?Utf-8?B?TWFyayBDaGFubmluZw==?= | last post by:
I have a code which registers all threads with a thread dump class. At intervals this thread dump class will dump the stack trace of all threads. As calling StackTrace(threadtoDump) from a...
0
by: Schadrach | last post by:
I'm having a strange problem, I have a small executable that runs a backup for a some data nightly, and as of September 22, 2007 it has ceased to function with the following error: Unhandled...
2
by: PimpDaddy | last post by:
Here is the source code of my C++/ME app. When I run it I can use "Windows Task Manager" to see that thread HANDLEs are leaking. #include "stdafx.h" #include <omp.h> public __gc class...
7
by: dmitrey | last post by:
hi all, Is there a better way to kill threading.Thread (running) instance than this one http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496960 (it's all I have found via google). BTW,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.