473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
Mar 16 '08 #1
1 1713
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_RunWorkerCom pleted);
}

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());
}
--
Happy Coding!
Morten Wennevik [C# MVP]
"ha******@hotmail.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
Mar 17 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: David W. Fenton | last post by:
I'm blanking out on this one. In an old applicaiton, I am implementing a new feature to allow the user to open multiple instances of a main form. I've got that working, using the code for this...
14
by: Brian Keating EI9FXB | last post by:
I wonder can anyone reccomment a solution to this problem. Let me explain, I've services running on my system, my application receives diagnostic messages from these services, what i want to do...
4
by: waltborders | last post by:
Hi, Because the blind are unable to use a mouse, keyboard navigation is key. A major difficulty is that not all windows forms controls are keyboard 'tab-able' or 'arrow-able' or have "tab...
6
by: lphan | last post by:
Hi there, The project that I'm working on is a securty piece of n-tier applications which try to authenticate a user. The login page, either a web form or windows form, calls a web service (thin...
3
by: TylerB | last post by:
Below is the code I have a question about. It is a simple setting of tables with login information for my Web Form and response.writing out the information: crConnectionInfo = new...
1
by: Ryan Liu | last post by:
Hi, can someone tell me how to make an application/form blanking at the taskbar with FormWindowState.Minimized form state? I see MSN or Yahoo chat work this way. When a new message arrive, the...
4
by: dm1608 | last post by:
Hi -- I have a VB6 application that I plan on rewriting in C#.NET. The program today basically connects to a SQL Server and parses varies system stats from our mainframes and rolls the data up...
11
by: Kevin | last post by:
I've got a timer on my MDI parent form. If there's no mouse movement for a set number of minutes, the Visible property of all open forms is set to False and the Log On form is displayed. I could do...
2
by: LarryT | last post by:
I have a couple of .net applications (not written by me) which cause the entire screen to go "blank" (e.g. black) immediately after strating the applications. The screen remains blanked for 2-4...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.