C#中:如何强制"调用"通过从另一个线程以某种方式信令从主线程的方法 [英] C#: How to force "calling" a method from the main thread by signaling in some way from another thread

查看:284
本文介绍了C#中:如何强制"调用"通过从另一个线程以某种方式信令从主线程的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉长标题,我不知道甚至如何表达问题的方式。

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;
}



这个回调从另外一个程序,我不拥有控制权叫,我想在主线程中运行OnConfigure法(即处理我的WinForm的),怎么办呢?
或者换句话说,我想,而不需要考虑的锁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 retrived
消息:回调会议代表'G19dotNet跑! G19dotNet.LgLcd + lgLcdOnSoftButtonsCB ::在GarbageCollector收集调用。在非托管代码代表应保证不会被删除,直到你确信他们将永远不会被调用。

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:

问题由自己解决,这要归功于其#1总是帮助我!
以供将来参考:定义一个委托作为函数指针

Issue resolved by myself, thanks to Stackoverflow which always helps me! For future reference: Defining a delegate as a function pointer

推荐答案

如果您正在使用的WinForms,你想在UI线程上执行的东西,你需要调用调用的BeginInvoke 上的一些控制(无论是按钮表格或其他),这是在该线程创建的。你需要在为了做到这一点对它的引用。

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.

例如,与您的代码,并假设你有一个叫做窗体的引用表格

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆