954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C# form crash on exit

Hi,

I have a problem with my form application that when I close it (using cross in top right), the whole program crashes indefinately.

This has only recently become an issue when I introduced some invoke code for updating a label and picture box on my form

I have an event handler within the form class that fires every 10th of a second that I use to set a label and a picturebox on the form, using the code below.

public delegate void setlblCallback(string text);

class {

   event handler {

   lblFaces.Invoke(new setlblCallback(this.setlbl), new object[] { faces.Count.ToString()        });

   }
 
   public void setlbl(String text)
        {
                lblFaces.Text = text;
        }
}


I initially didn't use invoke but received a threading error when setting the label.

Now the problem is the form is crashing on exit after implementing the invokes. (if i set the picture box directly the form end correctly but then the label errors, hence catch 22)

This code below is run on form close, which stops a webcam stream in another class (using Aforge.Net framework) This stream is run on another thread hence why I think this may also be involved.

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            
            inputCam.stop();
        }


Any ideas as to why this is happening would be appreciated or even how I could try and catch the exception when it happens ?

Thanks in advance

Tom

tomprice0189
Newbie Poster
5 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

The problem is the other thread is still running when you are exiting the GUI thread. Try using the "formclosing" event instead of the "formclosed" event to stop the other thread.

Although the invoke code you are using appears odd out of context. Its hard to tell exactly why its hanging.

Diamonddrake
Master Poster
724 posts since Mar 2008
Reputation Points: 442
Solved Threads: 89
 

Hi Diamond,

Thanks for the advice, I seem to have fixed the problem by using .BeginInvoke instead of just .Invoke. I gather the begin method uses a thread from the thread pool so it doesn't interfere with my other one.

Any reason why this would be a bad thing to do?

Cheers
Tom

tomprice0189
Newbie Poster
5 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

"BeginInvoke" is used to run a method on another thread using the thread pool, just "Invoke" is used to call a method to run on the thread the control is on.(Control.Invoke lol get it.)

Like I said your code looks odd, but if the problem is gone then best of luck.

Diamonddrake
Master Poster
724 posts since Mar 2008
Reputation Points: 442
Solved Threads: 89
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: