views:

83

answers:

2

Hi everyone,

My application is a C# Windows form application. When the user presses the button "Generate" my application creates a new thread and run the new process the process goes something like this pseudo code below

Loop counter = 1 to N 
    progressbar = counter/N
    Display msg on label1
    Do some tasks
Loop
progressbar = 100%
Display msg on lable1

I've delegate my processbar and label msgs, the messages shows up when it's within the loop with no problem I've even added an event to run Application.DoEvents() whenever the label text changes , however my last msg NEVER shows up, I've tried placing Application.DoEvents() right after the last msg label, however no solution. Anyone else encounter this before?

Thanks for the help everyone!

Edit

this is how I coded my last msg (The one after the loop)

        this.BeginInvoke((MethodInvoker)delegate()
        {
            toolStripProgressBar1.Value = 100;
            btnCancel.Enabled = false;
            btnBrowse.Enabled = true;
            btnSaveTo.Enabled = true;
            btnGenerate.Enabled = false;
            // Performance testing
            lblGeneratingInfo.Text = "Generation Complete! Total Run time: " + DateTime.Now.Subtract(startDate).ToString();
        });

FIXED: I'm assuming the text I tried to display was too long... when I replaced "Generating Complete: Total run time " with just "Run time: " it displayed o_o

+2  A: 

You might want to use a BackgroundWorker which uses ReportProgress() to tell how much progress it made.

Take a look at this backgroundWorker Tutorial

Updating the UI should only happen in the method called by ReportProgress().

dbemerlin
Thanks for the link, I'll try out background worker if I cannot get my current one to work
MikeAbyss
+1  A: 

You mention that this all happens on a new thread. However, you also mention DoEvents. To me, that suggests that you are updating the UI from the worker thread, which would be a problem (the other possibility is that you are running on a worker thread, but blocking the UI thread, which is also a problem).

You should be asking the UI to update itself on the UI thread - then you don't need DoEvents:

/* on worker thread */
string text = ...
int progress = ...
this.Invoke((MethodInvoker)delegate { /* on UI thread */
    label1.Text = text;
    progressBar.Value = progress;
});
/* on worker thread */
Marc Gravell
Hey thanks for the advice, I've taken out the Application.DoEvents() for changed text, everything still runs fine on the new thread, however the last msgs still doesn't show up, all other msgs shwos up perfectly
MikeAbyss
@MikeAbyss - are you sure the label is visible? It isn't clear what *does* appear there...
Marc Gravell
@Marc - Yes the label is always visible, my code does not change its visibility either, more or less the last msg should appear as "Generation Complete! Total run time [The time it took to finish the process]". Originally I had a status strip which contained just a label, this label had no problem when updating its text and being displayed, however when I added in the progress bar into the status strip, this problem started occurring. Thanks!
MikeAbyss
Thanks for all your help, found the issue!
MikeAbyss