Mobile .Net CF 下的测试控制句柄
当我的线程完成时,我不断抛出异常。
我可以捕获它们,但我希望我的代码首先捕获它们。
此代码适用于完整的 .Net Framework,但 IsHandleCreated
在 Compact Framework 下不存在。
问题: 我应该使用什么来代替 IsHandleCreated
?
Control _parent; // set when custom BackgroundWorker like class is created
bool parentOk {
get {
if (_parent != null) {
if (!_parent.IsDisposed) {
return _parent.IsHandleCreated;
// Should I instead "return (_parent.Handle != IntPtr.Zero);"?
}
}
return false;
}
}
public void ReportProgress(int step, object data) {
lock (_thLock) {
if (parentOk && (ProgressChanged != null)) {
MethodInvoker methInvoker = delegate { ProgressChanged(step, data); };
try {
_parent.BeginInvoke(methInvoker); // recently changed from below
// _parent.Invoke(methInvoker); (old technique)
} catch (ObjectDisposedException) { // added for BeginInvoke
} catch (NullReferenceException err) {
Global.LogError(_CODEFILE + "ReportProgress", err);
} catch (InvalidOperationException err) {
Global.LogError(_CODEFILE + "ReportProgress", err);
}
}
}
}
When my threads complete, I keep getting exceptions thrown.
I can catch them, but I'd rather my code catch them first.
This code could work for the full .Net Framework, but IsHandleCreated
doesn't exist under the Compact Framework.
Question: What should I use instead of IsHandleCreated
?
Control _parent; // set when custom BackgroundWorker like class is created
bool parentOk {
get {
if (_parent != null) {
if (!_parent.IsDisposed) {
return _parent.IsHandleCreated;
// Should I instead "return (_parent.Handle != IntPtr.Zero);"?
}
}
return false;
}
}
public void ReportProgress(int step, object data) {
lock (_thLock) {
if (parentOk && (ProgressChanged != null)) {
MethodInvoker methInvoker = delegate { ProgressChanged(step, data); };
try {
_parent.BeginInvoke(methInvoker); // recently changed from below
// _parent.Invoke(methInvoker); (old technique)
} catch (ObjectDisposedException) { // added for BeginInvoke
} catch (NullReferenceException err) {
Global.LogError(_CODEFILE + "ReportProgress", err);
} catch (InvalidOperationException err) {
Global.LogError(_CODEFILE + "ReportProgress", err);
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个有趣的(并且我发现的没有很好记录的)是,控件的句柄在被查询之前实际上并没有被创建。似乎在大多数情况下,框架内部发生的情况都很好,但在您使用调用控件的情况下,有时它没有发生,并且您会收到
Invoke 或 BeginInvoke 无法调用一个控件,直到创建窗口句柄
异常。我的解决方案一直是尽早直接查询句柄。在你的情况下,你可能可以摆脱这样的事情(就像你的评论所暗示的那样):
An interesting (and not well documented that I've ever found) is that an Control's Handle isn't actually created until it is queried. It seems that in ost cases that happens internally to the framework just fine, but in cases where you are using a control for Invoking, there are occasions where it hasn't happened and you get an
Invoke or BeginInvoke cannot be called on a control until the window handle has been created
Exception.My solution has always been to query the handle directly early on. In your case you can probably get away with something like this (like your comment suggests):
实际上在.NET Compact Framework 2.0中是不可能检查创建的,因为Handle是直接在基类中创建的,所以当你调用
new Control()
时它已经创建了一个句柄。因此,我发现检查句柄是否仍然有效的唯一方法是访问Handle
属性。如果这抛出一个 ObjectDisposeException,那么您就知道它没有有效的句柄。
如果没有发生异常,句柄仍然有效。
.NET Compact Framework 2.0 中不存在
IsDispose
属性,因此不知道前面的答案是什么。Actually in .NET Compact Framework 2.0 is it not possible to check for create, because the Handle is direct in base class created, so when you call
new Control()
it has already a handle created. So the only way I found to check whether handle is still valid is to access theHandle
property.If this throws an ObjectDisposedException, then you know it has no valid handle.
If no exception occur handle is still valid.
The
IsDisposed
property is not existing in .NET Compact Framework 2.0, so no idea what the previous answer is about.