如何调用下工作?没有线程只有1个指令指针? [英] How does Invoke work underneath? Doesn't thread have only 1 instruction pointer?
问题描述
我想知道它是如何可能的C#调用函数可以工作(我现在正在考虑从辅助线程进行的调用,从GUI线程操纵GUI的方法):
I would like to know how it is possible that C# Invoke function can work (I am now considering the call from worker thread to invoke a method that manipulates GUI from GUI thread):
假设我有两个线程,和他们每个人都有它inctruction指针,指向对当前执行的指令。
Assume I have two threads, and each one of them has it's inctruction pointer, pointing on an instruction that is currently executed.
现在,我打电话调用在辅助线程中运行的 GUI 的线程的委托。这怎么可能当一个GUI线程已经拥有它的指令指针(IP),每个线程只能有一个?
与IP时,我突然调用我的代码,会发生什么?
和它是如何取得的GUI线程以后可以与任何它正在继续(是其前IP某种程度上恢复)?
Now, I call Invoke in worker thread to run a delegate in GUI thread. How is this possible when a GUI thread already has it's instruction pointer (IP), and each thread can only have one? What happens with that IP when I suddenly invoke my code? And how is it made that the GUI thread can afterwards continue with whatever it was doing (is its former IP restored somehow)?
这个问题的泛化是如何当我想从调用线程1函数f()的方式,F()的一些上下文中执行它完成的其他的线程...
Generalization of this question is, how it is done when I want to call a function f() from thread 1 in a way that f() is executed in a context of some other thread...
感谢您的启发:!)
推荐答案
它发送AA窗口消息发布到目标线程。螺纹必须在一个消息循环为Invoke来工作。当线程得到消息,它调用委托
It sends a a Window message to the target thread. The thread must be in a message loop for Invoke to work. When the thread gets the message, it calls the delegate.
没有跨线程IP变化是必要的 - 事实上,更改IP几乎可以肯定崩溃的目标线程。
No cross-thread IP changes are necessary - in fact, changing the IP would almost definitely crash the target thread.
这篇关于如何调用下工作?没有线程只有1个指令指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!