Forms application 'blanking out' while busy

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hardieca@hotmail.com

    Forms application 'blanking out' while busy

    Hi,

    I'm building a forms app that parses thousands of text files. My boss
    insisted it have a GUI, to which I added an information console and a
    progress bar tracking the number of files parsed.

    Everything is working fine, except that when the application is run,
    it devotes itself entirely to the parsing, and the form with the
    console and progress bar just blanks out until the application
    finishes running.

    I've done mostly web development, so I'm kinda flummoxed about this.
    How do I rein my app in a little so that the form is continually
    displaying useful information?

    Regards,

    Chris
  • =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?=

    #2
    RE: Forms application 'blanking out' while busy

    Hi Chris

    I apologize if this answer appears twice, but I got an error trying to post
    the previous answer, so here goes.

    When you do heavy processing inside the GUI thread (main thread in a windows
    application) you won't have any processing time to perform GUI stuff. This
    will cause a gray and/or non responsive interface.

    You should move any heavy processing to a separate thread. The easiest way
    to do this is by using a BackgroundWorke r object like Gilles points out.

    Below is an example of how this can be done using a background worker that
    reports progress as well as supports aborting. Add a progressbar control as
    well as two buttons with their Click event attached to button1_Click and
    button2_Click

    private BackgroundWorke r worker;
    public Form2()
    {
    InitializeCompo nent();

    worker = new BackgroundWorke r();

    // Needed to fire the progresschanged event
    worker.WorkerRe portsProgress = true;

    // Needed to be able to stop the processing
    worker.WorkerSu pportsCancellat ion = true;

    // Any heavy processing should spawn from this event
    worker.DoWork += new DoWorkEventHand ler(worker_DoWo rk);

    // This event fires when you want it to
    worker.Progress Changed += new
    ProgressChanged EventHandler(wo rker_ProgressCh anged);

    // This event fires when the worker is done processing
    worker.RunWorke rCompleted += new
    RunWorkerComple tedEventHandler (worker_RunWork erCompleted);
    }

    private void button1_Click_1 (object sender, EventArgs e)
    {
    // Resett progressbar
    progressBar1.Va lue = 0;

    // Notify the worker object to start working
    worker.RunWorke rAsync(@"C:\tem p");
    // You can put anything as an argument to this method
    }

    private void button2_Click(o bject sender, EventArgs e)
    {
    // Stop working
    if (worker.IsBusy)
    worker.CancelAs ync();
    }

    void worker_DoWork(o bject sender, DoWorkEventArgs e)
    {
    DateTime start = DateTime.Now;

    // This method is called when you use RunWorkerAsync

    string path = e.Argument.ToSt ring();
    // e.Argument can be anything, including a complex object

    for (int i = 0; i < 100; i++)
    {
    // Did someone cancel the work?
    if (worker.Cancell ationPending)
    {
    // if so, then simply return
    return;
    }

    // Simulate processing of a single file
    // which in this case would take 1 seconds
    // to process.
    System.Threadin g.Thread.Sleep( 1000);

    // Report the number of files processed
    worker.ReportPr ogress(i + 1);
    // The parameter says intPercentage,
    // but it can be any number you like
    }

    // the Result property can be used to provide information
    e.Result = "100 files processed in " + (DateTime.Now -
    start).TotalSec onds + " minutes";
    }

    void worker_Progress Changed(object sender, ProgressChanged EventArgs e)
    {
    // If the worker calls ReportProgress this method will be called.
    progressBar1.Va lue = e.ProgressPerce ntage;
    }

    void worker_RunWorke rCompleted(obje ct sender, RunWorkerComple tedEventArgs e)
    {
    // This method will be called when the worker is done processing or
    cancelled

    if (e.Result == null)
    MessageBox.Show ("Processing aborted");
    else
    MessageBox.Show (e.Result.ToStr ing());
    }


    --
    Happy Coding!
    Morten Wennevik [C# MVP]


    "hardieca@hotma il.com" wrote:
    Hi,
    >
    I'm building a forms app that parses thousands of text files. My boss
    insisted it have a GUI, to which I added an information console and a
    progress bar tracking the number of files parsed.
    >
    Everything is working fine, except that when the application is run,
    it devotes itself entirely to the parsing, and the form with the
    console and progress bar just blanks out until the application
    finishes running.
    >
    I've done mostly web development, so I'm kinda flummoxed about this.
    How do I rein my app in a little so that the form is continually
    displaying useful information?
    >
    Regards,
    >
    Chris
    >

    Comment

    Working...