• (2 Pages)
  • +
  • 1
  • 2

The Background Worker How to use a background worker. Rate Topic: ***** 3 Votes

#16 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

Reputation: 2294
  • View blog
  • Posts: 9,531
  • Joined: 29-May 08

Posted 31 May 2013 - 12:24 AM

macosxnerd101: Let me have swing at it or them first.




Quote

Why does this article contain countless sentence fragments that make no sense?
Is the English?


Big_Android_Doris:
U sum sorta gramma nazi?
or
Are you prejudiced against Dyslexic content creators?

Further more which version are you comparing to? The Queen's English, American English, Estuary English, Ye Olde World English, Shakespearean or an accented English? like Yorkshire. which even has sub dialects for example the Barnsley (from where I originate) accent. I think in Barnsley and have to first translate the to whatever this "standard" English.

Combine that with other problems I suffer from like Dyslexia, Audio Processing Disorder (difficulty differentiating the phonemes of language) and Irlen syndrome. It can be physical and mentally exhausting, as well as frustrating. Since there a big disjuncture in my mental and intelligence capabilities and what I physical produce. Compared with to other I can seem like I'm lazy because it take me a disproportionate amount of time to produce comparable content.

Spell check can only check the spelling of the words. Correct contextual usage is harder to see, especially for me. The tutorial was created in 2009 and has had a minimum of 105K of views. I think it is actually a lot more since the we (DIC) has a forum upgrade that reset them, and the tutorial existed before that. If you were to take the time to investigate (which I suspect you wouldn't) and review the progression of the tutorials. I believe you find that they improve, since only generating and writing content will improve your quality of them.

Do you speak from a voice of authority? By which I mean a content producer.
What content have you produced?
Can we see it?
Are you willing to place yourself and the content you produce on to the sacrificial alter of the internet.
To be under the knife of a high priest of the church of the flying spaghetti monster for your linguistic fails.



Quote

Why is the huge, complex mess of code needed?

A lot of the complexity arises from the threading model used in Windows and the cross thread communications and the marshaling of data across them. The thread on which GUI control is created on, is the owner of it. Since the background worker uses internally a separate thread to enable simultaneous operation with the GUI thread, delegates are need to access controls on the form(s).

You can always create a new thread yourself. Or if you are use a new version of .net (.net 4.0 or later) the use tasks, but this still involve cross thread communications and the use of delegates.

Quote

If MS wanted us to use this extremely common feature: Run This Code In The Background

Common feature where?

Quote

Why can't the entire mess just be one line of code:
Call RunInTheBackground(mySubName)



Done.


First point, use the code tags for code or use the il tag for small single line code example.
Or didn't you read the background text of, or that big yellow box above the reply box?

Are you a vb6 coder? Call {method name} isn't required in vb.net.


Quote

Sheezess. I can't invest all this time, coding, and debugging...

Why are you even bothering then?
Debugging Skills are essential to learn, your code isn't going to be perfect.

Quote

... just to do something that should ALREADY
be a built-in call.

Your complaining to me, because your frustrated that you can't understand or use it.

It is already a built in call, you just don't know how to use it because you haven't invested the time to read the publicly available documentation (MSDN), or even do a little experimentation.

There are multiple ways to do it. All of which boil down to Threads and Tasks
Do you want synchronous or asynchronous results?

Which way is best for your situation is done to you to decide, which one meets you requirements.
You are the one doing the engineering for your project.

Microsoft ain't psychics who can predict the intent of you code.

vb.net is a descended from vb6 which is single threaded, if you wanted it you write the multi-thread usage yourself. It was even more complex.

Also multi-threaded code incurs a cost (it could even be slower), and is inherently non-deterministic.
So is hard to get get use to, thus hard do correctly.

X = X + 1 or X += 1 isn't thread safe.

You need to use Interlocked.Increment(X).

Multiple access to shared resource need coordination and control, is that via Locks or mutexes or some other synchronization mechanism?

For a large majority of the use cases it isn't even needed.

This post has been edited by AdamSpeight2008: 31 May 2013 - 12:40 AM

Was This Post Helpful? 1
  • +
  • -

#17 ianbhenderson73  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-May 15

Posted 11 May 2015 - 03:42 PM

This is a great tutorial, and I can already see it being very useful.

I could really use your help on extending this a bit: I have a VB.net winform which contains a combo box and a button - the code behind the button calls a method within a module which itself calls other methods within that same module.

Essentially what I've built is an application which takes an incoming spreadsheet file, imports the data into a database and performs a number of operations on that date. So the method which is initially being called in the module is then running a series of sequential sub-processes. I've coded the whole thing so that it updates a text box on the UI which lets the user know exactly what's going on. The difficulty that I face is that I've built a function which updates the UI textbox. So in essence, I have three levels of code - the code directly behind the form, the code being called by the form code (in a separate module file) and the code being called by the module code in a separate function file.

The main reason for giving so much feedback is that sometimes these incoming spreadsheets can be quite large and it means that the user isn't sitting staring at a blank application that doesn't appear to be doing anything. I'm also expecting to have to build functionality for handling more than one type of file, so the function approach saves me having to write the same code over and over. My difficulty is this: I can wrap up the code behind the form button in a Background Worker and it works a treat. It does not however update the status window and therefore the user is effectively running blind. It would be good to know if it's possible to wrap a Background Worker in a Background Worker, but I just can't get my head around what I need to do.
Was This Post Helpful? 0
  • +
  • -

#18 JohnESP  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 09-February 15

Posted 30 May 2015 - 10:19 AM

I've attached the completed working tutorial to play with

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#19 ZeeSam  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 34
  • Joined: 01-September 15

Posted 09 March 2016 - 02:53 AM

Okay .. So this might be creepy posting this after about 10 months, but I find this tutorial very useful. Only thing is that I am new to this and I have a problem.

I did what you did and I understand pretty much all of it. The only thing that is giving me a problem is : How to put this in my project?

I have a huge project that has a lot of controls that take a lot of time loading. I want to put a form like this in my project so it would appear when the control takes too long to load. I know I'm going to have to run over all my project and put some code in the areas that are taking too much time. But how do I make everything work in the background? were do I put my code? And is it possible to do this with one form like in your tutorial, or do I have to make a form for every thing that is taking too long to load?

Any help appreciated :)
Was This Post Helpful? 0
  • +
  • -

#20 andrewsw  Icon User is offline

  • Beach ready
  • member icon

Reputation: 5464
  • View blog
  • Posts: 21,482
  • Joined: 12-December 12

Posted 09 March 2016 - 05:59 AM

Quote

How to put this in my project?

Your questions aren't about this tutorial and should be in a new question.

However,

Quote

But how do I make everything work in the background? were do I put my code?

this is too broad, too vague. You'll need to ask a more specific question.

There isn't a switch that you can use to make "everything" run in the background. You have to examine your application, to identify bottlenecks and inefficient, and perhaps unnecessary, processes. That is, a thorough review of your application is required to identify why it, or parts of it, run slowly, and whether this can be improved or corrected by application design.

Then, once you've identified some bottlenecks, you can consider whether these could be improved by running some aspects in the background.

Other things to consider first though, are whether forms can be simplified, loaded only when necessary, and deferring the loading of data and controls. Also ensure that you are only loading the data, the columns and rows, that are needed.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2