0

I have a C# windows form Menu based application in .Net framework 4.0. in my option there are too many controls are added in form. a DataGridView is my first control on form. I handled a event on DataGridViewCellDoubleClick, in this event I am set values of all controls, and too many events are fire from this event and too many method is also called from this event.

My question is i double click on DataGridViewCell then immediately i click on my form exit button. my form is immediately close but my CellDoubleClick event is fired and it is executing and i have too many problems and exceptions occur because form is closed but my code is executing.

How can I resolve this problem that either form not close when my event is completely execute, or my event is should not execute or i can do some code in Closing event of form that prevent me from this problem.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Husain Sanwerwala
  • 479
  • 2
  • 7
  • 13
  • `bool canClose`.. Your question is a little unclear but what are the cell clicks doing? work on another thread? background/foreground? – Sayse Aug 22 '14 at 06:43
  • 2
    If you're not using worker thread, this cannot happen. Post some code which reproduces the problem. Prepare a small sample program and post it. So that it will be easy for us to help. – Sriram Sakthivel Aug 22 '14 at 06:51
  • You could add a WinForms Timer (not a thread timer), and when FormClosing fires you could cancel the closing for now, and start the timer up with a 1 second cycle time, for example. Then when the timer fires you could close the form. Would that be good enough? – RenniePet Aug 22 '14 at 06:51
  • @RenniePet - That sounds horribly inefficient – Sayse Aug 22 '14 at 06:54
  • I have not any background thread or background worker. – Husain Sanwerwala Aug 22 '14 at 06:55
  • Post the code which reproduces the problem. We don't care about your production code, just post a sample code which shows the same problem. Otherwise you're unlikely to get help. – Sriram Sakthivel Aug 22 '14 at 06:56

4 Answers4

0

Listen to the FormClosing event and then set e.Cancel = true if your background process is busy.

Kurubaran
  • 8,696
  • 5
  • 43
  • 65
Loathing
  • 5,109
  • 3
  • 24
  • 35
  • I am not working on multi thread or background worker. i have just one thread. I do not want that form should not close, I want that form should be close and no problems and exceptions occur. – Husain Sanwerwala Aug 22 '14 at 06:47
  • All cell click and form events run off the same main UI thread. So it's not possible to close a form while the UI thread is busy. As others have mentioned, post a simple example. – Loathing Aug 22 '14 at 06:58
0

Add this login in your code... I hop this will work.

bool inProcess = false;

private void DataGridViewCellDoubleClick(...)
{
   inProcess = true;
   //Write all ur code
   inProcess = false;
}

private void Form_Closing(...)
{
   e.Cancel = inProcess;
}
Shell
  • 6,818
  • 11
  • 39
  • 70
-1

@Shell is on the right track, but to close the form when the code is finished instead of cancelling the Form_Close try this:

bool inProcess = false;

private void DataGridViewCellDoubleClick(...)
{
    inProcess = true;
    //Write all your code
   inProcess = false;
}

private void Form_Closing(...)
{
    while(inProcess)
    {
    Application.DoEvents();
    }
}

After some research based on the commentor's hint I have to add that your form would not be disabled during the while-loop, so that there is plenty of opportunity for the user to mess up your execution order. If you would hide the form or disable all controls before entering the while-loop this would be less severe, but using a seperate thread is the proper way to go.

Community
  • 1
  • 1
  • Gag...will everyone _please_ stop suggesting `Application.DoEvents()` as the fix to everything...it's *LAZY*: http://stackoverflow.com/questions/5181777/use-of-application-doevents – DonBoitnott Aug 23 '14 at 16:04
-1

If you want your application to exit(in case you have the datagrid on the main form) and not just a window of your application then you may use Application.ExitThread Method - ExitThread() or Application.Exit() method if your IsBackground property set to true this will kill the process thread and should stop the fired event.

Ryu
  • 191
  • 10