查找需要 InvokeRequired 的方法
我来找您是想看看是否有人对如何解决我在迁移到 ActiveMQ 时遇到的问题有想法。 我在这个项目中使用 ActiveMQ 发送通知(在 C# 中),在完成实现后,我发现了一些有关线程问题的错误。 ( 我知道该异常的解决方案是使用“if this.InvokeRequired ....等”,但我的问题是:
有没有办法找到需要此调用的所有方法?
当然,我可以逐步检查通知触发的所有事件,但是,除了这会花费我太多时间之外,它也无法解决我未来的编程错误。
我想有更好的方法来解决这个问题,但我现在想不到。 您以前遇到过这个问题吗?
非常感谢您的帮助
I come to you to see if someone has an idea on how to solve a problem I've come across while doing a migration to ActiveMQ.
I'm using ActiveMQ to send notifications within this project (in C#), and after finishing the implementation i found some errors concerning threading problems. (
I know that the solution for that exception is to use the "if this.InvokeRequired.... etc", but my question is:
Is there any way of finding all the methods that require this invoke?
Of course i could check step by step all the events triggered with the notifications, but, apart from the fact that it would take me too much time, it wouldn't solve me future programming errors.
I guess there is a better way to figure out this, but i cannot think of it right now. Have you encountered the problem before?
Thank you very much for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不。没有自动化的方法可以做到这一点,除非您从一开始就设置了一个测试驱动的项目。 在这种情况下,您可以添加一些条件来测试线程的正确性。
软件无法推断出您的意图,除非以非常具体的方式(例如 FxCop,以及 IDE 关于某些事情的警告)。 你写的不一定是你想写的。 您实际上是在寻求能够弄清楚您想要做什么的软件。
了解是否需要调用的唯一方法是了解任何给定函数运行的上下文。 如果它在后台线程上运行,并且您正在调用需要在主线程上运行的代码(例如 GUI 代码),则需要调用。
你必须自己弄清楚这一点。
No. There is no automated way to do this, unless of course you've setup a test driven project from the beginning. In which case, you could add some conditions to test for thread correctness.
Software cannot deduce what you intended, except in very specific ways (FxCop for instance, and the IDE's warnings about certain things). What you wrote is not necessarily what you meant to write. You're effectively asking for software that can figure out what you meant to do.
The only way to know if an invoke is required is to know the context in which any given function operates. If it operates on a background thread, and you are calling code that needs to run on the main thread (such as GUI code) then an invoke is required.
You have to figure that out yourself.
这并不是说您调用的某些方法需要调用。 这取决于您从哪个线程调用这些方法。
如果您在 Winforms 应用程序中调用 UI 线程以外的线程上的方法,则需要 Invoke.
根据代码,应该很容易分析哪些线程进行了哪些调用,特别是如果您正在命名后台线程(这总是很方便)。 但可能没有自动的方法来做到这一点 - 只需退一步看看你的代码。
Its not that certain methods you are calling require the invoke. It depends on what thread you are calling those methods from.
If you're calling a method in a Winforms app, on a thread other the UI thread, it's going to require the Invoke.
Depending on the code, it should be easy to analyse what calls are made from which threads, especially if you are naming background threads (which always comes in handy). But there's probably not an automatic way to do this - just step back and look at your code.