开发者

Problem while Invoking delegate in a closed form

We have big problem: We have to update data on forms from a thread using Invoke. But when updating is too fast, user close the target form then 开发者_如何学编程we always get error message because update on closed from. We try many way to avoid that but no luck. Out code as following:

object[] paramList = ((IThreadUpdatableUI)_target).GetData();
if (_target.Handle != null)
  _target.Invoke(new UpdateUIDelegated(((IThreadUpdatableUI)_target).UpdateUI),
                                        new object[] { paramList });
if (!Started || ((IThreadUpdatableUI)_target).NeedToStop())
    break;
Thread.Sleep(_inteval);

Please Help. We are using C# in VS2005


You should check for your NeedToStop condition before invoking to the window, there is nothing to be updated if the form is closed. Just make sure that the NeedToStop condition returns true before the form is closed, e.g. in the FormClosing event handler.

By doing so, you make sure the thread terminates before the form is closed.


kill the updating thread on form.OnClosing, or unregister the delegate before closing.


Check for null reference exception before raising event.

If form is not there, event will be null so do not raise that event.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜