Hack Forums
[C#] How can my background worker effect my WindowsForm control? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] How can my background worker effect my WindowsForm control? (/showthread.php?tid=2019620)



How can my background worker effect my WindowsForm control? - alonalon - Dec 17, 2011

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."




* - Silicon - Dec 17, 2011

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.


* - iVision - Dec 17, 2011

With backgroundworker you can put in Form_load:
checkforilligalcrossscripting = false; (something like that)
And with threading:
invoke((methodinvoker(delegate { change w/e here ; }));


* - alonalon - Dec 17, 2011

(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)AntiHelix 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?


* - iVision - Dec 17, 2011

(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();
}


* - alonalon - Dec 17, 2011

(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""


* - DragQueen - Dec 17, 2011

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