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

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.

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

"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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.