从一个必须在另一个线程上执行的线程调用一个函数 [英] Calling a function from one thread which must be executed on another thread

查看:72
本文介绍了从一个必须在另一个线程上执行的线程调用一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好, 

Hi everyone, 

In 我的代码我从工作线程调用一个函数,因为这个函数是由它在工作线程上运行的工作线程调用的。

In  my code i get call to an function from worker thread, as this function is invoked by worker thread it runs on worker thread.

但是我有另一个函数,我从这个函数调用它必须同步在主线程上执行。  

But i have another function which i call from this function which must be executed on main thread synchronously.  

例如

void fun()

void fun()

{

//在工作线程中调用此函数

//This function is invoked in worker thread

//现在DoSomehing() 函数必须在主线程上执行。

//Now DoSomehing()  function must be executed on main thread.

DoSomehing();

DoSomehing();

}

在c#中这可以使用系统Dispatcher类可以实现如下。

In c# this can be achieved using systems Dispatcher class as follows.

dispatcher.InvokeAsync(()=>

        ;      {
$
               funtionToBeExecutedOnMainThread( );;
            });

dispatcher.InvokeAsync(() =>
                        {
                            funtionToBeExecutedOnMainThread();
                        });

This  m_dispatcher 属于必须执行函数的主线程。

This m_dispatcher  belongs to the main thread on which function must be executed.

我想在C ++中实现相同的功能。

I want to achieve the same in C++.

任何人都可以帮我解决这个问题。 。

Could anyone please help me with this.

谢谢和问候,

Sujit

推荐答案

感谢您在此处发帖。

>> 从一个线程调用一个必须在另一个线程上执行的功能                                   

>>Calling a function from one thread which must be executed on another thread                                 

原生c ++库中似乎没有这样的调度程序。

It seems there has no such dispatcher in native c++ library.

你可以创建一个支持托管代码的c ++ / cli项目。或者创建
混合程序集
,其中包含非托管计算机指令和MSIL说明。这允许它们调用.NET组件并被其调用,同时保持与完全不受管理的组件的兼容性。

You could create a c++/cli project which support managed code. Or create a mixed assembly which contains unmanaged machine instructions and MSIL instructions. This allows them to call and be called by .NET components, while retaining compatibility with components that are entirely unmanaged.

最好的问候,

Baron Bi


这篇关于从一个必须在另一个线程上执行的线程调用一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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