Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

How can my background worker effect my WindowsForm control?

How can my background worker effect my WindowsForm control?

I have a WindowsForm and a background worker, I give the background worker a task and in the end of it needs to change a text of a control in my WindowsForm(I'v set the control to "public").

I get this error:
"Cross-thread operation not valid: Control 'Name1' accessed from a thread other than the thread it was created on."

You can't edit Forms controls from any other thread. Make a variable with the value you need to change, then use a Timer to update it every so often. That's the easiest way to do it. Otherwise you're messing around with threading a lot.
ヽ༼ຈل͜ຈ༽ノ
With backgroundworker you can put in Form_load:
checkforilligalcrossscripting = false; (something like that)
And with threading:
invoke((methodinvoker(delegate { change w/e here ; }));
(Dec 17, 2011 08:01 AM)iVision Wrote:  With backgroundworker you can put in Form_load:
checkforilligalcrossscripting = false; (something like that)
And with threading:
invoke((methodinvoker(delegate { change w/e here ; }));

Sorry for not being able to understand what you wrote, I usually need a well explained explanation to understand something wrote in English.
Thanks for helping anyway.

(Dec 17, 2011 07:54 AM)Silicon Wrote:  You can't edit Forms controls from any other thread. Make a variable with the value you need to change, then use a Timer to update it every so often. That's the easiest way to do it. Otherwise you're messing around with threading a lot.

Can you give me a simple example code please?
(Dec 17, 2011 08:01 AM)alonalon Wrote:  Sorry for not being able to understand what you wrote, I usually need a well explained explanation to understand something wrote in English.
Thanks for helping anyway.

Can you give me a simple example code please?

I gave a working code?
Code
invoke((methodinvoker(delegate { yourwhateveryouneedtochangeonyourformhereplease; }));
That will work for multithreading.
so do this:

Code
using System.Threading;

Void changeTextbox()
{
invoke((methodinvoker(delegate { textBox1.Text = "Changed out of a thread."; }));
}

void Form1_Load....
{
Thread ctextBox = new Thread(changeTextBox);
ctextBox.Start();
}
(Dec 17, 2011 08:29 AM)iVision Wrote:  I gave a working code?
Code
invoke((methodinvoker(delegate { yourwhateveryouneedtochangeonyourformhereplease; }));
That will work for multithreading.
so do this:

Code
using System.Threading;

Void changeTextbox()
{
invoke((methodinvoker(delegate { textBox1.Text = "Changed out of a thread."; }));
}

void Form1_Load....
{
Thread ctextBox = new Thread(changeTextBox);
ctextBox.Start();
}

I tells me "The name 'invoke' does not exist in the current context"

and

"I tells me "The name 'methodinvoker' does not exist in the current context""
this will run in main thread.
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);

this will run in diffrent thread
worker.DoWork += new DoWorkEventHandler(worker_DoWork);

So change what you want in main thread
only in RunWorkerCompleted & ProgressChanged


Possibly Related Threads...
Thread / Author Replies Views Last Post
by THOR TECHAug 18, 2024 06:37 PM 4 191 Aug 18, 2024 06:37 PM
Last Post: Lookdadon
[C#] by PabloEscarNarkJan 12, 2018 08:25 PM 3 178 Jan 12, 2018 08:25 PM
Last Post: 0xDEAD10CC
[.NET] by FhackJan 10, 2018 04:36 AM 4 158 Jan 10, 2018 04:36 AM
Last Post: Fhack