Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Cancel Closing Form in Form Closing event

Cancel Closing Form in Form Closing event

Scheduled Pinned Locked Moved C#
question
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nadia Monalisa
    wrote on last edited by
    #1

    Hi, Is it possible to cancel Form Colosing in the Form Closing Event ? I have background worker in my form and when user try to close a form where a background worker is still busy, I want to let the user know that, "A background process is still busy, Try closing Later" -- And then Return the method without closing the form.

    W 1 Reply Last reply
    0
    • N Nadia Monalisa

      Hi, Is it possible to cancel Form Colosing in the Form Closing Event ? I have background worker in my form and when user try to close a form where a background worker is still busy, I want to let the user know that, "A background process is still busy, Try closing Later" -- And then Return the method without closing the form.

      W Offline
      W Offline
      wheelerbarry
      wrote on last edited by
      #2

      The onclose event has a CancelEventArgs prameter e. set e.Cancel to true.

      N 1 Reply Last reply
      0
      • W wheelerbarry

        The onclose event has a CancelEventArgs prameter e. set e.Cancel to true.

        N Offline
        N Offline
        Nadia Monalisa
        wrote on last edited by
        #3

        Thanks wheelerbarry, As far I know, these onClose, onLoad type events are protected members and I will have to override this methods right ? Is not it possible simply using e.Cancel within FormClosing event handler ?

        N 1 Reply Last reply
        0
        • N Nadia Monalisa

          Thanks wheelerbarry, As far I know, these onClose, onLoad type events are protected members and I will have to override this methods right ? Is not it possible simply using e.Cancel within FormClosing event handler ?

          N Offline
          N Offline
          Nader Elshehabi
          wrote on last edited by
          #4

          Hello

          bashiwala wrote:

          Is not it possible simply using e.Cancel within FormClosing event handler ?

          Yes it is! It's even the standard practice. Yet better make it the other way around. You shouldn't make the user wait for the thread. The thread should wait the user. Make your background worker a Background thread that will b terminated once the application is closed so that the use won't get frustrated waiting for something they don't even see or know when will it finish.

          Regards:rose:

          N 1 Reply Last reply
          0
          • N Nader Elshehabi

            Hello

            bashiwala wrote:

            Is not it possible simply using e.Cancel within FormClosing event handler ?

            Yes it is! It's even the standard practice. Yet better make it the other way around. You shouldn't make the user wait for the thread. The thread should wait the user. Make your background worker a Background thread that will b terminated once the application is closed so that the use won't get frustrated waiting for something they don't even see or know when will it finish.

            Regards:rose:

            N Offline
            N Offline
            Nadia Monalisa
            wrote on last edited by
            #5

            What happens when I execute Application.Exit() if a background worker is still busy in that application ? I assume that, all threads and background workers are immediately stopped and the application isexited. Is not that right ? The same question is for Application's main parent form closing. If the user simply click the cross button of top right corner of the Main Parent Window to close, will the threads and background workers are terminated immediately or they keep on running invisible ?

            N 1 Reply Last reply
            0
            • N Nadia Monalisa

              What happens when I execute Application.Exit() if a background worker is still busy in that application ? I assume that, all threads and background workers are immediately stopped and the application isexited. Is not that right ? The same question is for Application's main parent form closing. If the user simply click the cross button of top right corner of the Main Parent Window to close, will the threads and background workers are terminated immediately or they keep on running invisible ?

              N Offline
              N Offline
              Nader Elshehabi
              wrote on last edited by
              #6

              You'd be surprised if I tell you that i've never used those BackgroundWorkers!! I always use good old fashioned multithreading. I even don't know why they made this clas?!! Anyway, if a thread is made Foreground then it will keep running invisible and your applicatoin won't really exit. You can check that out using the task manager. But if you make that thread background by setting Thread.IsBackground = true, then it will close once all foreground threads are close and the process won't wait for that thread to finish. I don't know about BackgroundWorker class if it's really background, but it would take a small test to find out -I'm currently too lazy to lookup MSDN;P-

              Regards:rose:

              N 1 Reply Last reply
              0
              • N Nader Elshehabi

                You'd be surprised if I tell you that i've never used those BackgroundWorkers!! I always use good old fashioned multithreading. I even don't know why they made this clas?!! Anyway, if a thread is made Foreground then it will keep running invisible and your applicatoin won't really exit. You can check that out using the task manager. But if you make that thread background by setting Thread.IsBackground = true, then it will close once all foreground threads are close and the process won't wait for that thread to finish. I don't know about BackgroundWorker class if it's really background, but it would take a small test to find out -I'm currently too lazy to lookup MSDN;P-

                Regards:rose:

                N Offline
                N Offline
                Nadia Monalisa
                wrote on last edited by
                #7

                Thanks :) I feel more comfortable using Google than MSDN to search for a C# solution.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups