Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Forms application 'blanking out' while busy

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
hardieca@hotmail.com - 16 Mar 2008 13:37 GMT
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
Gilles Kohl [MVP] - 16 Mar 2008 15:34 GMT
>Hi,
>
[quoted text clipped - 10 lines]
>How do I rein my app in a little so that the form is continually
>displaying useful information?

Check out the BackgroundWorker class:

http://msdn2.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

  Regards,
  Gilles.
Morten Wennevik [C# MVP] - 17 Mar 2008 08:10 GMT
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 BackgroundWorker 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 BackgroundWorker worker;
public Form2()
{
   InitializeComponent();

   worker = new BackgroundWorker();

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

   // Needed to be able to stop the processing
   worker.WorkerSupportsCancellation = true;

   // Any heavy processing should spawn from this event
   worker.DoWork += new DoWorkEventHandler(worker_DoWork);

   // This event fires when you want it to
   worker.ProgressChanged += new
ProgressChangedEventHandler(worker_ProgressChanged);

   // This event fires when the worker is done processing
   worker.RunWorkerCompleted += new
RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
}

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

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

private void button2_Click(object sender, EventArgs e)
{
   // Stop working
   if (worker.IsBusy)
       worker.CancelAsync();
}

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

   // This method is called when you use RunWorkerAsync

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

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

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

       // Report the number of files processed
       worker.ReportProgress(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).TotalSeconds + " minutes";
}

void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
   // If the worker calls ReportProgress this method will be called.
   progressBar1.Value = e.ProgressPercentage;
}

void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs 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.ToString());
}

Signature

Happy Coding!
Morten Wennevik [C# MVP]

> Hi,
>
[quoted text clipped - 14 lines]
>
> Chris

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.