473,804 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get an object to inform progress via a form's status bar?

Hi,

I've got main() running in on a form. When a button on the form is
clicked, an object from a class I made is instantiated, and does some
stuff. I would like the stuff it does to be reflected in the form's
status bar. For example, "Now deleting file 35 of 234." if the object
was deleting a bunch of files.

I have no idea how to do this, but I understand there's some
thread-violation trickiness involved with having objects calling
controls on forms they don't own.

How can I achieve this effect easily/elegantly?

thanks for any ideas,

cdj

Oct 20 '06 #1
5 1334
class Form1:Form
{

public delegate void StringDelegate( string message);

public void ThreadSafeUpdat eText(string message)
{
this.BeginInvok e(new StringDelegate( UpdateText), new
object[]{message});
}

private void UpdateText(stri ng message)
{
this.Text = message;
}
}

The above code will update the title of a given form without locking up in
thread hanging badness.

The Key is the BeginInvoke call. It basically queues the call onto the
WindowsMessageP ump for the UI thread to handle (although that might be a
gross simplification, i'm not 100% sure).
A direct call to this.Text = "Whatever" would push ahead of the queue and
cause the threading issue by accessing the control on a different thread.

HTH

Simon Tamman

"sherifffruitfl y" <sh************ *@gmail.comwrot e in message
news:11******** **************@ i42g2000cwa.goo glegroups.com.. .
Hi,

I've got main() running in on a form. When a button on the form is
clicked, an object from a class I made is instantiated, and does some
stuff. I would like the stuff it does to be reflected in the form's
status bar. For example, "Now deleting file 35 of 234." if the object
was deleting a bunch of files.

I have no idea how to do this, but I understand there's some
thread-violation trickiness involved with having objects calling
controls on forms they don't own.

How can I achieve this effect easily/elegantly?

thanks for any ideas,

cdj

Oct 20 '06 #2
I like creating 2 sets of interfaces IProgressClient and IProgressServer .
The UI or its controller will implement the client whilst any classes
wishing to have the ability to write information to the progressbar
implement IProgressClient . I like this solution as it gives maximum
flexibility as to how you configure progress bars in your app and if done
right will allow the classes implementing the clients to still operate even
when there are no servers attached to them.

This is quite a woolly answer but should at least highlight the direction I
took. If needed I could probably dig up some source code...

--
RobP
'There are only 10 types of people in this world - Those that understand
binary and those that don't'

"sherifffruitfl y" <sh************ *@gmail.comwrot e in message
news:11******** **************@ i42g2000cwa.goo glegroups.com.. .
Hi,

I've got main() running in on a form. When a button on the form is
clicked, an object from a class I made is instantiated, and does some
stuff. I would like the stuff it does to be reflected in the form's
status bar. For example, "Now deleting file 35 of 234." if the object
was deleting a bunch of files.

I have no idea how to do this, but I understand there's some
thread-violation trickiness involved with having objects calling
controls on forms they don't own.

How can I achieve this effect easily/elegantly?

thanks for any ideas,

cdj

Oct 20 '06 #3
I have a farily basic example of modifying GUI objects from a different
thread on my website:
http://devdistrict.com/codedetails.aspx?A=366
Kelly S. Elias
Webmaster
DevDistrict - C# Code Library
http://devdistrict.com

sherifffruitfly wrote:
Hi,

I've got main() running in on a form. When a button on the form is
clicked, an object from a class I made is instantiated, and does some
stuff. I would like the stuff it does to be reflected in the form's
status bar. For example, "Now deleting file 35 of 234." if the object
was deleting a bunch of files.

I have no idea how to do this, but I understand there's some
thread-violation trickiness involved with having objects calling
controls on forms they don't own.

How can I achieve this effect easily/elegantly?

thanks for any ideas,

cdj
Oct 20 '06 #4

Simon Tamman wrote:

(code snipped)
The above code will update the title of a given form without locking up in
thread hanging badness.
I guess I just don't understand - it looks to my eye as though the
class (of the object) that's doing the stuff doesn't have any
"communicat ion code" in your (and everyone else's) examples? How on
earth does the form know what's going on with the object, if the object
doesn't have any code "telling" the form what's going on?

I was probably unclear about what I'm dealing with. Here's a minimal
"template" example:

public class myform : system....form
{
..
..
..

private void btn_awesomeButt on_Click(object sender, System.EventArg s e)
{
killerObject ko = new killerObject;
ko.doSomethingR ockin();
}

}

public class killerObject
{
..
..
..

public doSomethingRock in()
{
(loop to delete 1000 hardcoded files here)
}

}

What I want to do is have the status bar (or any control) on the form
be updated every pass thru the loop in the object created by the button
click event (say with the name of the file being deleted).

Does that make sense even? Does your suggestion suffice for what I'm
after? I would've thought that some code would have had to have been
added to killerObject *as well as* to the form.

thanks for the replies all! sorry if I was unclear...

Oct 20 '06 #5
Well the code I presented displayed how the communication should operate
without encountering thready badness.
A more complete example would be:

class Form1:Form
{

public delegate void StringDelegate( string message);

public void ThreadSafeUpdat eText(string message)
{
this.BeginInvok e(new StringDelegate( UpdateText), new
object[]{message});
}

private void UpdateText(stri ng message)
{
this.Text = message;
}

private void btn_Click(objec t sender, EventArgs e)
{
KillerObject killer = new KillerObject(th is);
killer.DoSometh ingRockin();
}
}

public class KillerObject
{
Form1 form;
public KillerObject(Fo rm1 form)
{
this.form = form;
}

public void DoSomethingRock in()
{
for(int i = 0; i< 100; i++)
{
form.ThreadSafe UpdateText("Cur rently looping: " + i.ToString());
}
}
}
Obviously you'd need to change the code to use a progress bar and it might
be better for the killer object to use events instead of taking a reference
to the form.
Additionally if you wanted to keep the UI active it would be best to run
"DoSomethingRoc kin" in a seperate thread.

HTH.

Simon Tamman
"sherifffruitfl y" <sh************ *@gmail.comwrot e in message
news:11******** **************@ i42g2000cwa.goo glegroups.com.. .
>
Simon Tamman wrote:

(code snipped)
The above code will update the title of a given form without locking up
in
thread hanging badness.

I guess I just don't understand - it looks to my eye as though the
class (of the object) that's doing the stuff doesn't have any
"communicat ion code" in your (and everyone else's) examples? How on
earth does the form know what's going on with the object, if the object
doesn't have any code "telling" the form what's going on?

I was probably unclear about what I'm dealing with. Here's a minimal
"template" example:

public class myform : system....form
{
.
.
.

private void btn_awesomeButt on_Click(object sender, System.EventArg s e)
{
killerObject ko = new killerObject;
ko.doSomethingR ockin();
}

}

public class killerObject
{
.
.
.

public doSomethingRock in()
{
(loop to delete 1000 hardcoded files here)
}

}

What I want to do is have the status bar (or any control) on the form
be updated every pass thru the loop in the object created by the button
click event (say with the name of the file being deleted).

Does that make sense even? Does your suggestion suffice for what I'm
after? I would've thought that some code would have had to have been
added to killerObject *as well as* to the form.

thanks for the replies all! sorry if I was unclear...

Oct 23 '06 #6

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

Similar topics

0
1407
by: mirandacascade | last post by:
O/S - Windows XP Home with Service Pack 2 Vsn of Python: 2.4 (from ActiveState) This question is with regard to the progress bar controls that are in the file status.py. On my workstation, status.py is located in the: c:\python24\lib\site-packages\pythonwin\pywin\dialogs\ folder.
4
11622
by: Alexander | last post by:
Hi, I have written a program that takes on some operations much more time than I expected. As I have seen users clicking wildly on the screen to make something happen, I want to follow the microsoft principles: always show them something ;) First I made up a little status dialog containing a gif image to show a little animation and a TextBox for a changing status message during the work of the main program. Just showing the dialog box...
4
3278
by: aj | last post by:
Alan, you can achieve this by using XMLHttp Async functionality in a javascript file. Create an aspx page in which execute long running SQL query, then do an XMLhttp post to that page from a js function. In the javascript create 2 funtion Call LoadData in the window onload event.
7
5498
by: Oleg | last post by:
I have a web form let's say 'YYZ.aspx'. It has an iframe in it. When it loads it shows progress bar in IE this way: loading for page then again loading for page in iframe. This part is fine. Problem is when I put page 'YYZ.aspx' in iframe of another page, let's say 'Home.aspx'. In short: (page (page in iframe(page in iframe) ) ) Progress bar shows ones (fast), then second time(kind of fast) and then third time. And this time it never...
3
2513
by: John Salerno | last post by:
I'd be curious to know if this works any differently on other computers/platforms or while other things are happening in the background. I can't tell if it's the Timer object that isn't keep accurate time (although a test with time.time() seems to show that it is), or if I'm just messing up my algorithm to fill the progress bar. If I put in 10 seconds, the progress bar will be fully filled at the end, but if you put 30, it doesn't. As the...
9
3274
by: esakal | last post by:
Hello, I'm programming an application based on CAB infrastructure in the client side (c# .net 2005) Since my application must be sequencally, i wrote all the code in the UI thread. my problem occurs when i try to show a progress bar. The screen freezes. I know i'm not the first one to ask about it. but i'm looking
5
10911
by: Miro | last post by:
I will try my best to ask this question correctly. I think in the end the code will make more sence of what I am trying to accomplish. I am just not sure of what to search for on the net. I have a form that has a button. ( this form is a child form of a parent form ( main form ). Anway...in this child form I have a button, and if clicked a bunch of code will get executed. I would like to show a Progress Bar / form in modal/ShowDialog...
2
2186
by: giddy | last post by:
hi , I'm in a slight dilemma. I have an updater program that checks for pending updates and downloads them if the users chooses to. Since i dont need a windows form to check if there is an update , I dont use a form unless i have two. This , creates either one of two problems. When i'm downloading asynchonously , the main thread exits!
4
2751
by: frozenade | last post by:
Hello all.. merry christmas.. :) I need to show progress status when a button is clicked. I use this script. but I know that this code is still very bad. <html>
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10604
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10359
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10101
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9177
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6870
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3837
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.