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

LongRunning method in MainForm and showing modal dialog box with progressbar ( C# )

Hello i'm just curious what you think about this: i want to start a longrunning method in a MainForm then show a modal dialog box with progressbar on in (which show operation progress, as you can guess). I know that this should be a multithreaded app (asynchronously) and i've seen plenty examples, but none of them works as fine as i want it to. What i really want to do is:

- run a longrunning method in background thread (whatever it does, say encryption, copying files, or even call sleep method - just do some calculations which takes time)

- show a modal dialog box with progressbar and Cancel button

- if the operation completed successfully - form with progressbar should get lost itself (i mean Close() but without user interaction)

- if users clicked the cancel button - operation should be canceled

I'll be really grateful for code example as simply as it can be - just to help me realize how it can be done.
Mar 30 '07 #1
3 4145
RedSon
5,000 Expert 4TB
What was wrong with the examples that you read? Do you have a prototype created from those examples?
Mar 30 '07 #2
What was wrong with examples was (generally speaking) not to cancel operation in background thread - especially when i use BackgroundWorker to run an encryption algorithm. But it's not worth to talking about.

In my previous project i used Thread object and ThreadStart delegate, like this:

Expand|Select|Wrap|Line Numbers
  1. Thread OperationThread = new Thread(new ThreadStart(DoWorkMethod));
  2.  
And it works as i needed but i had to display form with progressbar (that's ok) and in that form was code responsible to start new thread (this succks). What if i have, say 5 operations which have to be done in new (separate) thread? Now, in such situation should i have 5 forms with progressbar?

Obviously not. So i need a solution to run background thread in mainform, show progressbar form and if operation succeed - close progressbar form (in code - without user interaction), otherwise (if operation failed) - cancel and rollback operation.

Is it possible at all? If so, plese give me some advise and code.
Anyway thanks for replys.
Mar 31 '07 #3
Hi,

Check out this link, it has a solution exactly for the situation you are asking for.

http://www.mycsharpcorner.com/Post.aspx?postID=21

Basically, you can use a volatile bool to indicate when the long process has finished processing, and have a timer on your Please-Wait dialog box that checks the volatile value every interval to see if the thread has finished, so it can close itself or not.

You should make sure you control the GUI Only from the main thread... this is a .NET restriction, hence, the use of the volatile variable.

Hope this helps.

Source: http://www.mycsharpcorner.com
Mar 31 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Patrick Lim | last post by:
Here is the problem: I have written a non-modal frame class in Java for displaying help text when a user is using an application. It works as desired in that if the user selects "help" again,...
2
by: martin de vroom | last post by:
Hi, I have a web page that opens a modal dialog (client side) in the following manner onclick="window.showModalDialog('/dialog.asp',null,'dialogHeight: 200px; dialogWidth: 400px; dialogTop:...
2
by: Gilles T. | last post by:
Hi, How I can refresh a modal dialog in asp.net? I open a modal dialog in first with a dropdownlist. To add a element in my dropdownlist (table), I open a second modal dialog to ask element and...
2
by: cassidyc | last post by:
Hi, I was wondering if anyone has come accross this issue? And if they have any solutions I have that can create new copies of itself Form1 as = new form1(); af.show(); This form can also...
2
by: sthrudel | last post by:
Hi! I'm working on a web application in Asp.net and what I would like to have is a cross borwser modal dialog which accepts user's input. I would like to catch what the user clicked on the...
5
by: Earl | last post by:
I need to call a method on an owned child form, and am wondering if the best way of doing this is to capture the Closing event of the form that passes control back to the form where I have the...
0
by: =?Utf-8?B?a20=?= | last post by:
I've got a simple C# app built in VS2005. When I click a menu item a modal form opens and performs a lengthy operation in the Load event handler to populate a text control, so it takes a few...
2
by: diogenes | last post by:
I have created many shortcut/popup (aka context, or right-click) menus for my application - instead of toolbars or standard drop-down menus. Within my custom menu, I am using...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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...
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
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...

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.