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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jamesjames
    New Member
    • Mar 2007
    • 3

    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.
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    What was wrong with the examples that you read? Do you have a prototype created from those examples?

    Comment

    • jamesjames
      New Member
      • Mar 2007
      • 3

      #3
      What was wrong with examples was (generally speaking) not to cancel operation in background thread - especially when i use BackgroundWorke r 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:

      Code:
      Thread OperationThread = new Thread(new ThreadStart(DoWorkMethod));
      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.

      Comment

      • yousefla
        New Member
        • Mar 2007
        • 10

        #4
        Hi,

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



        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
        Last edited by yousefla; Mar 31 '07, 09:03 PM. Reason: Link correction

        Comment

        Working...