如何调用带有空参数的委托?
如果我尝试在调用期间将 null 参数传递给委托,则会收到 null 异常。代码如下所示:
public void RequestPhoto() { WCF.Service.BeginGetUserPhoto(Contact.UserID, new AsyncCallback(RequestPhotoCB), null); } public void RequestPhotoCB(IAsyncResult result) { var photo = WCF.Service.EndGetUserPhoto(result); UpdatePhoto(photo); } public delegate void UpdatePhotoDelegate(Binary photo); public void UpdatePhoto(Binary photo) { if (InvokeRequired) { var d = new UpdatePhotoDelegate(UpdatePhoto); Invoke(d, new object[] { photo }); } else { var ms = new MemoryStream(photo.ToArray()); var bmp = new Bitmap(ms); pbPhoto.BackgroundImage = bmp; } }
问题出在以下行:
Invoke(d, new object[] { photo });
如果变量“photo”为空。在调用期间传递空参数的正确方法是什么? 谢谢!
I get a null exception if I try to pass a null parameter to a delegate during an invoke. Here's what the code looks like:
public void RequestPhoto() { WCF.Service.BeginGetUserPhoto(Contact.UserID, new AsyncCallback(RequestPhotoCB), null); } public void RequestPhotoCB(IAsyncResult result) { var photo = WCF.Service.EndGetUserPhoto(result); UpdatePhoto(photo); } public delegate void UpdatePhotoDelegate(Binary photo); public void UpdatePhoto(Binary photo) { if (InvokeRequired) { var d = new UpdatePhotoDelegate(UpdatePhoto); Invoke(d, new object[] { photo }); } else { var ms = new MemoryStream(photo.ToArray()); var bmp = new Bitmap(ms); pbPhoto.BackgroundImage = bmp; } }
The problem is with the line:
Invoke(d, new object[] { photo });
If the variable "photo" is null. What is the correct way to pass a null parameter during an invoke?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只是为了其他人的利益,您可以将空参数传递给委托(如果类型允许?此处需要澄清)。在您的情况下,IAsyncResult 将允许它。
至于调试,异常发生在Invoke上,因为你在给定的线程A上调试,异常发生在线程B上。你可以在多个线程上断点。在线程 B 代码上设置断点,您将看到异常接近源或位于源上。
请注意,如果多个线程同时运行代码,您的调试器将会跳转。在多线程中进行调试总是至少有点棘手,但是当您解决问题时会感到满意。
您还可以进一步改进您的答案代码,在检查 InvokeRequired 之前检查 null,因为这与您的逻辑无关(您的代码在使用之前、调用之后检查它)。这将节省将 Invoke 推送到消息泵上的时间(假设为 WinForms)。
Just for the benefit of others, you can pass null arguments to delegates (if the type allows it? Clarification needed here). In your case, IAsyncResult will allow it.
As for the debugging, the exception occurs on Invoke because you are debugging on a given Thread A, the exception occurs on Thread B. You can breakpoint multiple threads. Breakpoint the Thread B code and you will see the exception closer to or on the source.
Notice though that your debugger will jump around if multiple threads are running code at the same time. Debugging in multiple threads is always at least a little tricky, but satisfying when you solve the problems.
You could also further improve your answer code to check the null before it checks the InvokeRequired, as this is thread-independent to your logic (your code checks it just prior to use, after Invoking). This will save pushing the Invoke onto the message pump (assuming WinForms).
好吧,我想通了。问题并不像我想象的那样将 null 参数传递给委托。问题在于委托执行它导致线路上出现空异常:
我没有意识到问题在那里,因为它在调用线路上崩溃了。
所以我将 UpdatePhoto 方法更改如下:
一切都很好!
OK I figured it out. The problem was NOT with passing the null parameter to the delegate like I thought. The problem was with the delegate executing it was causing a null exception on the line:
I didn't realize the problem was there because it was crashing on the Invoke line.
So I changed the UpdatePhoto method as follows:
And all is well!