6 Replies - 838 Views - Last Post: 02 September 2013 - 09:55 AM Rate Topic: -----

#1 kyle_denney  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 132
  • Joined: 10-August 12

Background worker and loading...

Posted 01 September 2013 - 01:28 PM

Hello,

I would like to use a background worker to update the end user for a long running process. Unfortunately I have updates to datagridviews on long running processes so I keep getting the cross thread errors. Is there a way to avoid this with a background worker and how does one do it? Here is some code snippets but if I use the backgroundworker in this manner it doesn't update the textbox until after the long running process is finished which is obviously useless in the way I am trying to do it. Any help would be appreciated. Thanks!

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        BackgroundWorker1.ReportProgress(5, "Running some long process...")
    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        Label5.Text = CType(e.UserState, String) & " " & e.ProgressPercentage.ToString
    End Sub




I need a better way to update the UI. I would eventually like to use a progress bar but unfortunately everywhere on the internet has the long running process just running some loop with a cheap example that doesn't actually help anyone that has UI calls on the long running process. For instance, I am loading gridviews and textboxes on a separate form and would like to update the end user such that each time a new gridview is filled to say "... gridview1 is filled..." "gridview2 is filled..." etc. Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Background worker and loading...

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5417
  • View blog
  • Posts: 11,609
  • Joined: 02-June 10

Re: Background worker and loading...

Posted 01 September 2013 - 02:03 PM

FAQs: 16, 17, 18


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.

Did you even read about the command you're trying to use, and look at the examples?
C# Language Specification


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated July 2013
Spoiler



Was This Post Helpful? 0
  • +
  • -

#3 kyle_denney  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 132
  • Joined: 10-August 12

Re: Background worker and loading...

Posted 02 September 2013 - 07:15 AM

How does putting a breakpoint in the code and stepping through it actually help. Having code and it being broke is not the problem. I just need a great tutorial on backgroundworkers and actually changing UI elements on those workers because of it running on a separate thread. I know what I need I just don't know where to find any decent examples or code snippets. Your information was not helpful at all.

Didn't you read where I said that the examples I found only showed generic loops (including MSDN) that did not show how to update the UI.

This post has been edited by kyle_denney: 02 September 2013 - 07:17 AM

Was This Post Helpful? 0
  • +
  • -

#4 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 964
  • Joined: 09-May 09

Re: Background worker and loading...

Posted 02 September 2013 - 07:48 AM

backgroundWorker.reportProgress is what you need

if you dont do this then you can create a function to update the datagrid, use the control.invoke method
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5417
  • View blog
  • Posts: 11,609
  • Joined: 02-June 10

Re: Background worker and loading...

Posted 02 September 2013 - 08:25 AM

View Postkyle_denney, on 02 September 2013 - 08:15 AM, said:

How does putting a breakpoint in the code and stepping through it actually help. Having code and it being broke is not the problem. I just need a great tutorial on backgroundworkers and actually changing UI elements on those workers because of it running on a separate thread. I know what I need I just don't know where to find any decent examples or code snippets. Your information was not helpful at all.

Didn't you read where I said that the examples I found only showed generic loops (including MSDN) that did not show how to update the UI.


I think my information would have been more helpful if you had actually taken the tme to read it:

Quote

FAQ's 16, 17, 18


Attached Image


FAQ 16 points you right at a tutorial on using the backgroundworker thread:
http://www.dreaminco...worker-in-c%23/

FAQ 17 & 18 are specifially for the cross-thread issue you mentioned so you can do it right instead of the .Invoke hack.

You skimmed my response, thought you knew what it was saying and abandoned the rest. That is not a good technique when reading documentation and might explain why you didn't get what you wanted out of the rest of the help and numerous tutorials you've read until this point, leading you to asking again.

Slow down. Read articles and responses fully and you might get a lot more out of them.
Was This Post Helpful? 0
  • +
  • -

#6 kyle_denney  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 132
  • Joined: 10-August 12

Re: Background worker and loading...

Posted 02 September 2013 - 08:31 AM

Thanks everyone - I did not realize the links had clickable icons. I wondered why they were posted and then realized that you can actually expand them. Thanks!

This post has been edited by kyle_denney: 02 September 2013 - 08:34 AM

Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5417
  • View blog
  • Posts: 11,609
  • Joined: 02-June 10

Re: Background worker and loading...

Posted 02 September 2013 - 09:55 AM

You might need to take an introduction to computers course before you try developing software.
I don't mean to be mean, but blue underscored text, where your cursor changes from an arrow to a hand icon when you hover over them - has been the standard text-link style for two decades. We simply can't go around putting big "Click here" arrows on everything everywhere in the site/internet.

If you aren't even seeing the FAQ's, you need to click the "Show" button. Which would also indicate you need to slow down and read everything presented, and probably become more familiar with just using a computer and the internet before you try to develop for it. Essentially I'm saying it would be silly to try to design better automobiles if you can't drive because you don't understand the underlying concepts of the field or its users or existing paradigms... so how could you develop for them?
Attached Image
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1