J
Jen C.
Hi
I wanted to make a control with a method that is called
asynchronously to adjust something displayed in the control. I used
this.Invoke() to do this, and it works great until I close the form.
Then I get an exception because the method is being called after the
control is disposed. I tried to avoid this by stopping the thread
when the Form1.Closing event happens, but it does not help. How can I
make this work properly?
Thanks in advance,
Jenny C.
// These methods are in my Form1 class
protected void myThreadProc()
{
while(!m_bThreadDone)
{
this.userControl11.asyncSetCircleRadius(m_tick++);
if (m_tick > m_maxtick) m_tick = 10;
if(!m_bThreadDone) Thread.Sleep(40);
}
}
private void Form1_Closing(object sender, CancelEventArgs e)
{
m_bThreadDone = true;
Thread.Sleep(40);
}
// These are in my control:
public int CircleRadius
{
get
{
return m_circleSize;
}
set
{
m_circleSize = value;
this.Invalidate();
}
}
public int m_newCircleSize=10;
// public delegate void circleSizeDelegate();
protected void localSetCircleRadius(object Sender, EventArgs
args)
{
CircleRadius = m_newCircleSize;
}
public void asyncSetCircleRadius(int newRadius)
{
m_newCircleSize = newRadius;
this.Invoke(new EventHandler(localSetCircleRadius));
}
I wanted to make a control with a method that is called
asynchronously to adjust something displayed in the control. I used
this.Invoke() to do this, and it works great until I close the form.
Then I get an exception because the method is being called after the
control is disposed. I tried to avoid this by stopping the thread
when the Form1.Closing event happens, but it does not help. How can I
make this work properly?
Thanks in advance,
Jenny C.
// These methods are in my Form1 class
protected void myThreadProc()
{
while(!m_bThreadDone)
{
this.userControl11.asyncSetCircleRadius(m_tick++);
if (m_tick > m_maxtick) m_tick = 10;
if(!m_bThreadDone) Thread.Sleep(40);
}
}
private void Form1_Closing(object sender, CancelEventArgs e)
{
m_bThreadDone = true;
Thread.Sleep(40);
}
// These are in my control:
public int CircleRadius
{
get
{
return m_circleSize;
}
set
{
m_circleSize = value;
this.Invalidate();
}
}
public int m_newCircleSize=10;
// public delegate void circleSizeDelegate();
protected void localSetCircleRadius(object Sender, EventArgs
args)
{
CircleRadius = m_newCircleSize;
}
public void asyncSetCircleRadius(int newRadius)
{
m_newCircleSize = newRadius;
this.Invoke(new EventHandler(localSetCircleRadius));
}