473,382 Members | 1,726 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,382 software developers and data experts.

Progress bar without GUI code in the model?

bob
Hello,

I want to show progress to the user while some method is running so I
thought I'd use a progress bar.

But I don't see how I can do this without writing GUI code in the
model?

In Smalltalk I would fire of notification exceptions that a progress
bar would catch, increment progress, and then allow to continue (or no
one would catch and would perform their default behaviour which was to
continue). I gather that's not possible because Exceptions in C#
cannot be resumable?

Any ideas?

Thanks,
Bob
Nov 15 '05 #1
2 2737
How about an event? Then the GUI could subscribe to the "worker" class's
event, and do the neccessary GUI work.

If you need an "infintite" (time length unknown) progress bar, I just wrote
one:
http://www23.brinkster.com/peteprodu...teProgress.zip

-Pete
"bob" <bo**********@hotmail.com> wrote in message
news:b0**************************@posting.google.c om...
Hello,

I want to show progress to the user while some method is running so I
thought I'd use a progress bar.

But I don't see how I can do this without writing GUI code in the
model?

In Smalltalk I would fire of notification exceptions that a progress
bar would catch, increment progress, and then allow to continue (or no
one would catch and would perform their default behaviour which was to
continue). I gather that's not possible because Exceptions in C#
cannot be resumable?

Any ideas?

Thanks,
Bob

Nov 15 '05 #2
Hi Bob,

Using an exception for this is not recommended, in fact an exception should
only be used when an error occur, do never use exceptions to control the
flow of a program, the exceptions are VERY EXPENSIVES !!!.

You have to use a event for this, below I post a code snap that do
precisely that.
Basically I'm resizing images files, I do so in a worker thread and in the
main thread ( UI thread ) I use a progressbar as well as a couple of labels.

I post here the important part of the code, all the code will found below.

// This is the delegate I Will use to notify the interface a new file was
processed
public delegate void ProcessedFileHandler( );
// thre thread
Thread workingThread;
// This will "point" to the function to update the UI
ProcessedFileHandler procfilehandler;
private void ImportImages_Load(object sender, System.EventArgs e)
{
// The method I will call to update the UI
procfilehandler = new ProcessedFileHandler( this.UpdateProgressBar);
//Start the thread
this.workingThread = new Thread( new ThreadStart( this.ImportImage ));
this.workingThread.Start();
}

// This is the working method, I iterate in the file list and process each
file , at the end I call the method to update the interface
// the trick is to use a WinForm control and call Invoke on it, it can be
ANY UI control, all it does is MAKE SURE that the call
// is done in the main thread.
void ImportImage( )
{
foreach(string imageURL in files)
{
try
{

//Update the interface, this MAKE SURE the call is done in the main
thread
this.progressBar1.Invoke( procfilehandler, null);
}
catch(Exception e)
{
continue;
}
}
}
//The update UI method:
void UpdateProgressBar( )
{
this.progressBar1.Value++;
FileNameLBL.Text = currentfile;
currentLBL.Text = currentpos.ToString();
}
Here is the code completed, just for reference !!!.

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

//*********************** CODE START

using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Text;
using System.IO;
using System.Threading;

namespace WindowsApplication1
{
/// <summary>
/// Summary description for ImportImages.
/// </summary>
public class ImportImages : System.Windows.Forms.Form
{
public delegate void ProcessedFileHandler( );

Thread workingThread;
object syslock = new object();
int currentpos=0;
string currentfile="";
string[] files;
ProcessedFileHandler procfilehandler;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.Label FileNameLBL;
private System.Windows.Forms.Label currentLBL;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public ImportImages(string[] files)
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
this.files = files;
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.FileNameLBL = new System.Windows.Forms.Label();
this.currentLBL = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(16, 144);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(264, 23);
this.progressBar1.TabIndex = 1;
//
// FileNameLBL
//
this.FileNameLBL.Location = new System.Drawing.Point(16, 208);
this.FileNameLBL.Name = "FileNameLBL";
this.FileNameLBL.Size = new System.Drawing.Size(208, 16);
this.FileNameLBL.TabIndex = 2;
this.FileNameLBL.Text = "label1";
//
// currentLBL
//
this.currentLBL.Location = new System.Drawing.Point(176, 184);
this.currentLBL.Name = "currentLBL";
this.currentLBL.Size = new System.Drawing.Size(56, 16);
this.currentLBL.TabIndex = 3;
this.currentLBL.Text = "label2";
this.currentLBL.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// label3
//
this.label3.Location = new System.Drawing.Point(240, 184);
this.label3.Name = "label3";
this.label3.TabIndex = 4;
this.label3.Text = "label3";
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 176);
this.label1.Name = "label1";
this.label1.TabIndex = 5;
this.label1.Text = "Processing:";
this.label1.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
//
// ImportImages
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.label1);
this.Controls.Add(this.label3);
this.Controls.Add(this.currentLBL);
this.Controls.Add(this.FileNameLBL);
this.Controls.Add(this.progressBar1);
this.Name = "ImportImages";
this.Text = "ImportImages";
this.Load += new System.EventHandler(this.ImportImages_Load);
this.ResumeLayout(false);

}
#endregion

void UpdateProgressBar( )
{
this.progressBar1.Value++;
FileNameLBL.Text = currentfile;
currentLBL.Text = currentpos.ToString();
}

void Done()
{
MessageBox.Show("Done");
this.Close();
}
void ImportImage( )
{
Image.GetThumbnailImageAbort myCallback =
new Image.GetThumbnailImageAbort(ThumbnailCallback);

//Images are in the form ID.extension
foreach(string imageURL in files)
{
try
{

DataRow prow = GetProductRow( Path.GetFileNameWithoutExtension(
imageURL) );
// if ( prow == null)
// continue; //product not found.
//See if the Image is correct in size, if not try to resize it
Image image = new Bitmap( imageURL);
if ( (image.Width> Config.MaxImageWidth) || (image.Height>
Config.MaxImageHeight) )
{
//Resize the image
Image newimage = image.GetThumbnailImage( Math.Min( image.Width,
Config.MaxImageWidth) ,
Math.Min( image.Height, Config.MaxImageHeight) ,
myCallback, IntPtr.Zero);
ExportImage( newimage,
Path.GetFileName(imageURL) );//prow["ProductID"].ToString() );
}
else
ExportImage( image,
Path.GetFileName(imageURL) );//prow["ProductID"].ToString() );

lock( this.syslock)
{
this.currentpos++;
this.currentfile = Path.GetFileName(imageURL);
}
//Update the interface
this.progressBar1.Invoke( procfilehandler, null);
}
catch(Exception e)
{
continue;
}
}
this.progressBar1.Invoke( new ProcessedFileHandler( this.Done), null);
}
//Just save the image to the new directory under the new name
void ExportImage( Image image, string newname)
{
image.Save( Config.ExportImageDirectory + @"\" + newname,
System.Drawing.Imaging.ImageFormat.Jpeg);
}
DataRow GetProductRow( string origID )
{
// foreach(DataRow prow in DataProvider.StaticData.Tables["Product"].Rows)
// if ( prow["OriginalID"].ToString() == origID)
// return prow;
return null;

}
public bool ThumbnailCallback()
{
return false;
}

private void ImportImages_Load(object sender, System.EventArgs e)
{
this.label3.Text = "of " + files.Length.ToString();
this.progressBar1.Minimum = 0;
this.progressBar1.Maximum = files.Length;
procfilehandler = new ProcessedFileHandler( this.UpdateProgressBar);
//Start the thread
this.workingThread = new Thread( new ThreadStart( this.ImportImage ));
this.workingThread.Start();
}
}
class Config
{
public static int MaxImageWidth = 200;
public static int MaxImageHeight = 200;
public static string ExportImageDirectory = @"c:\temp";
public static string ExportImageDirectoryPPC = @"c:\temp";
}
}
//********************** CODE END
"bob" <bo**********@hotmail.com> wrote in message
news:b0**************************@posting.google.c om...
Hello,

I want to show progress to the user while some method is running so I
thought I'd use a progress bar.

But I don't see how I can do this without writing GUI code in the
model?

In Smalltalk I would fire of notification exceptions that a progress
bar would catch, increment progress, and then allow to continue (or no
one would catch and would perform their default behaviour which was to
continue). I gather that's not possible because Exceptions in C#
cannot be resumable?

Any ideas?

Thanks,
Bob

Nov 15 '05 #3

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

Similar topics

10
by: Rob | last post by:
Hi all, Is it possible to do a progress bar of gauge bar in asp? If possible could someone show some sample code on it. Many thanks, Rob
1
by: Wavemaker | last post by:
I was wondering if there is a way to change the value of a control without triggering an event. The ComboBox control, for example, will trigger the SelectedIndexChanged event when its SelectedIndex...
5
by: James Morton | last post by:
I have a Static DataSet in a class file that I am using globally between a few forms. The main form populates the dataset through a menu option which invokes ReadXML in the class file to populate...
5
by: Tomaz Koritnik | last post by:
I have a worker thread doing some long calculation and I'd like to report progress (percent done,...). One easy way it to call Control.Invoke(). But is there another way of doing this without using...
4
by: Kenneth Keeley | last post by:
Hi, I have a page that uploads files to my server and I wish to display a "Please wait while uploading" page to the user while the file is uploading. I have been able to redirect the user once the...
3
by: Ritesh Raj Sarraf | last post by:
Hi, I have a small application, written in Python, that uses threads. The application uses function foo() to download files from the web. As it reads data from the web server, it runs a progress...
8
by: WhiteWizard | last post by:
I guess it's my turn to ASK a question ;) Briefly my problem: I am developing a Windows app that has several User Controls. On one of these controls, I am copying/processing some rather large...
25
by: David Sanders | last post by:
Hi, As part of a simulation program, I have several different model classes, ModelAA, ModelBB, etc., which are all derived from the class BasicModel by inheritance. model to use, for example...
1
by: spinoza1111 | last post by:
I want the GUI of the spinoza system to not piss me off with the usual type of progress reporting one sees: the flashy, colorful, and utterly uninformative gizmos that go back and forth and round...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.