This article explains the basics of Dot Net Threading, provides a simplistic diagram of how to implement a Background Worker into code, AND I provide VB .NET code that demonstrates one application with three Threads showing different ways to implement Background Worker Thread so as to update controls, objects and variable from a form. Hopefully I have done this in a constructive way so that you, the novice yet to learn about threading, or the ‘I am about to pull my hair out’ coder can take it and see how easy it is to implement a thread. BE WARNED. There is absolutely no error coding in this application as the point was to show you Threading, not Error Control. Perhaps I could make that another topic?
Threading. You are either a programmer who understand what it is, or you probably are not a programmer.
When I say the word ‘Multitasking’ most people have an understanding that computers can multitask, and females love to say they can multitask, and employees try to say they are great at their job because they multitask, yet they would have no idea about CPU Time Slicing or what effort it takes to make YOUR program Thread Safe.
I am somewhat bemused when I tell people that I’ve been doing some programming and how I needed to do some threading, and their response implies that I have a Microsoft Excel Spreadsheet open and am having difficulty with a formula. Um. No.
How easy is threading? On a scale of 1 : 10, I’d rate it about a 7. or an 8. If you have a framework, it still takes care and consideration. Compared with anything I do at work, Threading is FAR more complicated.
crapping.. bollocking on! Let’s do this.
- Pictures tell a thousand words, and here is my Play School Style Picture (oops, I meant to say the Parallel System Support Program).
- And below I illustrate the classes in my application. Note that I have three threads, depicted as Thread_A, Thread_B, Thread_C. Each Thread requires three classes.
I personally like to name classes so that I can quickly and easily identify what they do. If you prefer to use ambiguous names feel free to call them what you like.
I use one class to store a ‘database table’ of types (clsTableStorage, which creates a Data Table consisting of four columns), and then there is the main form, which has the controls on it for testing. Don’t worry, you don’t have to DO anything – this is currently working in Visual Studio 2010 with Dot Net 4.0
- On first Load, I have Three lines of Controls (two labels, a Progress Bar, one label) Then two Command buttons followed by a Data Grid View. Your mission, should you accept it, is to press the Run button.
- After you pressed the run button, you probably saw all three lines of controls, display different things. Each Line represents a different thread. They should run fairly fast as the thread is nothing more than looping a For Loop from 0 to 500, and updating the controls as it goes. In the case of the Data Grid View, I only update the grid every 50th Record.
- Ah, so what?! I hear you exclaim. Well., there are three different models being used to update these controls:
- The first Thread updates the Controls directly. I pass the name of each and every control to the Thread, and it updates those controls. If you have not had threading work before, this seems like a miracle cure… but if you have more than five controls to update, it is going to be a pain in the neck! Still . it works.
- This thread passes in a Data Table in the form of a variable. The thread only updates the Data Table. The way that the controls get updated is that before calling the thread, I bind the controls to the Data Table as if it were a Data Source (which it is in fact). There are downsides to this method. Downside 1. As you will see when you run the code, the Progress Bar will not update during the thread. Downside 2: the Data Table can only store ONE RECORD for this to work. If you can make it work with a Data Table that stores two or more, please let me know as I did try to include that as the next example.
- This thread uses a Data Table (obviously) but I pass in the Progress bar AND I prove to you that you can modify the form side variable (the Data Table) while the thread is in play. After you have clicked ‘Run’ (and don’t wait for the code to end) Simply click ‘Force Record into Data Table’, and a dummy record will be ‘injected’ into the Data Table. If you look at the code, you will see there are no tricks here. The Data Grid View is bound to the Data Table. I think this is pretty cool!
- Go ahead and check out the code! I suggest commenting out one section of thread while you identify how the other ones work… but you will find that they work the way I describe it in my Play School Style hand out.
- If your computer is not very powerful, you may need to extend the
Const THREADSLEEPTIME As Integer =1,
Const THREADSLEEPTIME As Integer = 3
Since creating this post, I modified the Thread Example to include a fourth thread. This fourth thread runs from within the for loop, presented below. This is imitating a section (or sections) of code where the guts of the code can take a while to calculate. In my case I send it off to calculate a Fibonacci Number, and I am happy to report that doing so slowed down the application… slightly.
I also took the time to improve some small issues… but nothing major.
I also noticed, that at home on my 6 year old laptop, that the threads required a longer THREADSLEEPTIME than on my good computer, which was actually a surprise. What was interesting was that as the night went on, the laptop became more responsive towards the program.