8 Replies - 858 Views - Last Post: 23 October 2016 - 08:40 AM Rate Topic: -----

#1 phatom200   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-October 16

Cross-thread operation not valid

Posted 22 October 2016 - 07:58 PM

I'm using the below code to update a progress bar.

if (read_success == true && global_variables.csvtext == "not found")
               {
                    this.Unknown_Device.Text = "Writing CSV. Please wait.";
                    backgroundWorker1.RunWorkerAsync();
               }
 
rivate void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 0; i <= 100; i++ )
            {
                simulateHeavyWork();
                backgroundWorker1.ReportProgress(i);
            }
        }
        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.csvprogressbar.Value = e.ProgressPercentage;
        }
        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            writecsv(null, null);
        }
        private void simulateHeavyWork() 
        {
            Thread.Sleep(40);
        }

But when I debug the program on Visual Studio it breaks on where ever I have used text box controls by giving the below message.

Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on.

I read this issue on the Internet but I can't really figure how to change the code to get around this issue since I'm a total newbie to C# programming.

Appreciate if I can get some help with modifying the code to make the application run properly.

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Cross-thread operation not valid

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6290
  • View blog
  • Posts: 21,618
  • Joined: 05-May 12

Re: Cross-thread operation not valid

Posted 22 October 2016 - 08:07 PM

If you've read about this on the Internet, the most common advice you have have run across is to invoke the change back on the primary UI thread. Did you even attempt that?
Was This Post Helpful? 0
  • +
  • -

#3 phatom200   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-October 16

Re: Cross-thread operation not valid

Posted 22 October 2016 - 08:49 PM

Yes I read about that but I can't figure out how to change the code according to that. (Just started programming)

Would you be able to help?

This post has been edited by Skydiver: 22 October 2016 - 09:01 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6290
  • View blog
  • Posts: 21,618
  • Joined: 05-May 12

Re: Cross-thread operation not valid

Posted 22 October 2016 - 09:00 PM

There is no need to quote the post above yours. Just use the big Reply button or the Fast Reply area.

You could have simply looked at the documentation in an attempt to understand what peopel were telling you: BeginInvoke()

Anyway, if you are beginning programming, why waste time learning obsolete WinForms? You might as well learn how to use WPF or Xamarin.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • Bouncy!
  • member icon

Reputation: 6563
  • View blog
  • Posts: 26,615
  • Joined: 12-December 12

Re: Cross-thread operation not valid

Posted 22 October 2016 - 10:37 PM

As you are a total newbie to C# then don't start with the BackgroundWorker or anything asynchronous. Study the fundamentals of the language. Then, when you come to more complex topics, you will have a better understanding of what Google and MSDN are telling you.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14174
  • View blog
  • Posts: 56,774
  • Joined: 12-June 08

Re: Cross-thread operation not valid

Posted 23 October 2016 - 08:00 AM

Basically get rid of all the 'background worker' or other threading.
Was This Post Helpful? 0
  • +
  • -

#7 phatom200   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-October 16

Re: Cross-thread operation not valid

Posted 23 October 2016 - 08:05 AM

View Postmodi123_1, on 23 October 2016 - 08:00 AM, said:

Basically get rid of all the 'background worker' or other threading.


If I do, how I can get the progressbar to work?
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14174
  • View blog
  • Posts: 56,774
  • Joined: 12-June 08

Re: Cross-thread operation not valid

Posted 23 October 2016 - 08:40 AM

Okay.. so what is "this.Unknown_Device.Text"? All your GUI controls are on your main thread.. any background worker thread shouldn't be trying to jump back and modify the main thread, or it's components. I would think you need to revisit what the thread should, and is, doing.

http://www.codeproje...rogressBar-demo
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6290
  • View blog
  • Posts: 21,618
  • Joined: 05-May 12

Re: Cross-thread operation not valid

Posted 23 October 2016 - 08:40 AM

Notice how you periodically called a function to report progress? You would do the same thing.

The problem arises in making your UI seem responsive even though you are busy doing something.

The first good long term choice is to use threading, but as previously mentioned, you should master the basics before you go down this path.

In the interim, you need to partition you long running job into smaller bits that take up less then a third of second to run. You then incrementally do your work either in a timer event or on an idle state event.

Think of it this way. All those classic video games in 70's, 80ís and early 90ís basically ran single threaded. Looks amazing back then, and even more amazing if you think about the engineering that went into them.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1