progress bar to reflect status of program

hshawa

Member
Joined
Aug 2, 2012
Messages
8
Programming Experience
1-3
Hi guys, I am fairly new to VB.net. Trying to write an application that is supposed to test some issues with a POA. long story short, I have an array of type long that is initialized, and values are set as the program executes. I want a progress bar in a form to be able to continuously read this array (which is globally declared) and make the changes on the progress. The problem seems that form does not show at all, the updating code is in the form_new event handler in the form.

This form is created and loaded by a function that runs on a new thread while the program executes.

I have read about background worker, but the thing is that the main time consuming program is the one who invokes the form and not vice versa, this is why i chose threading.

Any tips?

Thanks
 
The BackgroundWorker simplifies for you the task of reporting progress in UI thread, just set WorkerReportsProgress to True and handle ProgressChanged where you do any UI reporting.
 
The BackgroundWorker simplifies for you the task of reporting progress in UI thread, just set WorkerReportsProgress to True and handle ProgressChanged where you do any UI reporting.

Thanks for the reply,

I am aware of using background worker. the thing is that my whole main program needs to do complicated work that depends on alot of user input. So i cannot simply create a background worker for that. This is why I want to use threading. when the main program starts executing (when an OK button is clicked), i want to invoke a separate thread that can read an array and depending on the progress there, update the progress bar.

this is the code for the function that runs on a separate thread to load the progress form:
VB.NET:
  [COLOR=#0000ff]Sub[/COLOR] progressForm()

        [COLOR=#0000ff]Dim[/COLOR] frm3 [COLOR=#0000ff]As[/COLOR] [COLOR=#8515ea]New[/COLOR] Form3
        frm3.Showdialog()


    [COLOR=#0000ff]End[/COLOR] [COLOR=#0000ff]Sub[/COLOR]


this is the form code that has a progress bar, the problem is that it is not shown. could it be because it is in the form_load event? If not, then where should i put the code in the form?

VB.NET:
[COLOR=#0000ff]Public[/COLOR] [COLOR=#0000ff]Partial[/COLOR] [COLOR=#0000ff]Class[/COLOR] Form3
    [COLOR=#0000ff]Public[/COLOR] [COLOR=#0000ff]Sub[/COLOR] [COLOR=#8515ea]New[/COLOR]()
        [COLOR=#008000]' The Me.InitializeComponent call is required for Windows Forms designer support.[/COLOR]
        [COLOR=#0000ff]Me[/COLOR].InitializeComponent()
        
        [COLOR=#008000]'[/COLOR]
        [COLOR=#008000]' TODO : Add constructor code after InitializeComponents[/COLOR]
        [COLOR=#008000]'[/COLOR]
        
        [COLOR=#0000ff]Me[/COLOR].progressBar1= [COLOR=#8515ea]New[/COLOR] System.Windows.Forms.ProgressBar 
        
        [COLOR=#0000ff]Dim[/COLOR] x [COLOR=#0000ff]As[/COLOR] [COLOR=#6f002f]Integer[/COLOR] 
             [COLOR=#0000ff]While[/COLOR] intervals(testingCases -1 ) = 0
   
            [COLOR=#0000ff]If[/COLOR] intervals(x) <> 0 [COLOR=#0000ff]Then[/COLOR]
                x = x +1
            
            [COLOR=#0000ff]End[/COLOR] [COLOR=#0000ff]If[/COLOR]
            progressBar1.Value = x            
            
        [COLOR=#0000ff]End[/COLOR] [COLOR=#0000ff]While[/COLOR]
        
        [COLOR=#0000ff]Me[/COLOR].label1.[COLOR=#0000ff]Text[/COLOR] = "done!"    
      
    [COLOR=#0000ff]End[/COLOR] [COLOR=#0000ff]Sub[/COLOR]
    [COLOR=#0000ff]Private[/COLOR] [COLOR=#0000ff]Sub[/COLOR] form3_load()
    
    [COLOR=#0000ff]End[/COLOR] [COLOR=#0000ff]Sub[/COLOR]
         
[COLOR=#0000ff]End[/COLOR] [COLOR=#0000ff]Class[/COLOR]
 
program needs to do complicated work that depends on alot of user input. So i cannot simply create a background worker for that
Why not ???
 
Why not ???

As I said, It is not a single function that does some work, but the whole main program executes this code, and trying to wrap it in a single function (backgroundWorker_doWork) is gonna be a pain. I mean I can try that, but I think a simpler solution that just allows me to have a separate UI thread that shows a progress bar is better.
Anyways, I seem to have some progress and now the progress form is showing. But I need a form event that allows me to run code while the form is shown. this is responsible for updating the progressbar. I tried the Form_Shown() event but it is not compiling. Here is the code for the Form_Shown event:
VB.NET:
[FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff] Private[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000] Form3_Shown([/COLOR][/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000] sender [/COLOR][/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000] System.[/COLOR][/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]Object[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000], [/COLOR][/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000] e [/COLOR][/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000] System.[/COLOR][/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]EventArgs[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000]) _[/COLOR]
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] Form3.Shown
[/SIZE][/FONT][/SIZE][/FONT]

It does not compile with error of: Handles clause requires a WithEvents variable defined in the containing type or one of its base types. What does that mean?
 
You already have a UI thread and don't need another. Use Control.Invoke to call a method on UI thread from any secondary thread.
 
What you're trying to do is bad, bad, bad. The proper way to do this is the way JohnH has already suggested. No one has said that you have to put everything into a single function. You just need to start a single function on a background thread and then call all your other functions from that. You may need to tweak your design a bit to get it to work but it shouldn't be anything major. The one thing you must change is make sure that you're not accessing any controls directly in a method executed on that background thread. That's not difficult to do. Just follow the steps outlined here:

Accessing Controls from Worker Threads
 
Hey Guys!

Thanks for your input, although it was a little tedious, but I got it to work. Apparently using using backgroundworker is the only solution to this case.

Thanks!
 
BackgroundWorker is by far not the only solution to multi-threading, it is just a very convenient component to use in winforms environment, especially for inexperienced developers.
 
Back
Top