C#:如何强制“调用"通过以某种方式从另一个线程发出信号来从主线程获取一个方法 [英] C#: How to force "calling" a method from the main thread by signaling in some way from another thread
问题描述
对不起,标题太长,我什至不知道如何表达问题
Sorry for long title, I don't know even the way on how to express the question
我使用的库从与主线程不同的上下文运行回调(是一个 C 库),我在 C# 中创建了回调,当被调用时,我只想引发一个事件.
I'm using a library which run a callback from a different context from the main thread (is a C Library), I created the callback in C# and when gets called I would like to just raise an event.
但是因为我不知道事件里面会发生什么,所以我想找到一种方法来调用该方法而不会出现锁等问题(否则第三方用户将不得不在事件内部处理这个,很丑)
However because I don't know what will be inside the event, I would like to find a way to invoke the method without the problem of locks and so on (otherwise the third party user will have to handle this inside the event, very ugly)
有没有办法做到这一点?我可能完全走错了路,但我正在考虑用 winforms 的方式来处理不同的线程(.Invoke 事情)
Are there any way to do this? I can be totally on the wrong way but I'm thinking about winforms way to handle different threads (the .Invoke thing)
否则我可以向窗口的消息循环发送消息,但我不太了解消息传递以及是否可以发送这样的自定义"消息
Otherwise I can send a message to the message loop of the window, but I don't know a lot about message passing and if I can send "custom" messages like this
示例:
private uint lgLcdOnConfigureCB(int connection, System.IntPtr pContext)
{
OnConfigure(EventArgs.Empty);
return 0U;
}
这个回调是从另一个我无法控制的程序调用的,我想在主线程(处理我的winform的那个)中运行 OnConfigure 方法,怎么做?或者换句话说,我想在不需要考虑锁的情况下运行 OnConfigure
this callback is called from another program which I don't have control over, I would like to run OnConfigure method in the main thread (the one that handles my winform), how to do it? Or in other words, I would like to run OnConfigure without the need of thinking about locks
编辑 1:
我对此异常有疑问:
CallbackOnCollectedDelegate 被检索消息:在 GarbageCollector 中收集的委托G19dotNet!G19dotNet.LgLcd+lgLcdOnSoftButtonsCB::Invoke"上运行回调.在非托管代码委托期间应确保永远不会被删除,直到您确定它们永远不会被调用
CallbackOnCollectedDelegate retrived Message: Callback run on delegate 'G19dotNet!G19dotNet.LgLcd+lgLcdOnSoftButtonsCB::Invoke' collected in GarbageCollector. During unmanaged code delegates should be ensured will never be deleted until you are sure they will never be called
编辑 2:
问题由我自己解决,感谢 Stackoverflow 总是帮助我!供以后参考:将委托定义为函数指针
Issue resolved by myself, thanks to Stackoverflow which always helps me! For future reference: Defining a delegate as a function pointer
推荐答案
如果您正在使用 WinForms 并且想要在 UI 线程上执行某些操作,则需要调用 Invoke
或 BeginInvoke
在该线程上创建的某个控件(可以是 Button
或 Form
或其他任何东西)上.您需要引用它才能执行此操作.
If you're using WinForms and you want to execute something on the UI thread, you need to call either Invoke
or BeginInvoke
on some control (be it a Button
or a Form
or whatever) that was created on that thread. You'll need a reference to it in order to do this.
例如,使用您的代码并假设您引用了一个名为 form
的表单:
For example, with your code and assuming that you have a reference to a form called form
:
private uint lgLcdOnConfigureCB(int connection, System.IntPtr pContext)
{
form.Invoke(new MethodInvoker(() => OnConfigure(EventArgs.Empty)));
return 0U;
}
这篇关于C#:如何强制“调用"通过以某种方式从另一个线程发出信号来从主线程获取一个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!