473,583 Members | 3,437 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 BackupCompleted EventHandler();
private event BackupCompleted EventHandler backupCompleted ;
private void btn_run_click(o bject sender, System.EventArg s e)
{
if (myMsgs.createB ackup()) //File Copy will be made
{
this.backupComp leted += new
BackupCompleted EventHandler(on BackupCompleted );
Thread t = new Thread(new ThreadStart(mak eBackup));
t.Start();
}
}
private void onBackupComplet ed()
{
statusBar_audit .Panels[0].Text = "Done with backup"; /* I GET THE
ERROR HERE */
dataGrid_auditA ddress.DataSour ce = null;

}
private void makeBackup()
{
File.Copy(_sFil eName, _sFileName + ".bak");
onBackupComplet ed();
}
Thanks for the help.
Nov 16 '05 #1
1 14238
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 SetStatusBarPan elDelegate(Stri ng newText);
private void SetStatusBarPan el(String newText)
{
statusBar_audit .Panels[0].Text = newText;
}

and in your onBackupComplet ed:

Invoke(new SetStatusBarPan elDelegate(SetS tatusBarPanel),
new Object[] { "Done with backup" });

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vk arlsen.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
7872
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, None, None, None, (conn)) def run(self,conn): while 1: data = conn.recv(1024)
0
1013
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
3226
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 behaviour. Inside the join() method of threading.Thread objects, a Condition named self.__block is acquired, and then the wait logic is executed. ...
2
2238
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 Me.bytesDownloadedTextBox.Text = "" Me.totalBytesTextBox.Text = ""
2
1905
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":} cpu.CpuMonitor(debug,stats_d).start() db.UpdateDatabase(debug,stats_d).start() And CpuMonitor looks like this:
3
6041
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 different thread other than the treadToDump the threadToDump must be suspended otherwise a ThreadStateException gets thrown. However under .NET 2.0...
0
2045
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 Exception: System.TypeInitializationException: The type initializer for "System.Threading.Thread" threw an exception. --->...
2
2535
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 TestClass { public: void ThreadProc()
7
6129
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, it should be noticed that lots of threading module methods have no docstrings (in my Python 2.5), for example _Thread__bootstrap, _Thread__stop.
0
7893
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7821
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8172
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8317
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7928
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5369
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3839
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2326
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1151
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.