C++/CLI、CLR 中的线程

问题描述 投票:0回答:1

遇到问题。需要在单独的线程上调用类成员方法 C++/CLI

.h:

ref class BgWorker
{
private:
    void MainLoop();
public:
    void Start();
};

.cpp:

void BgWorker::MainLoop()
{
    while(true)
    {
        ...    
    }
}

void BgWorker::Start()
{
    System::Threading::Thread^ td = gcnew System::Threading::Thread(MainLoop); // v1; didn't working
    System::Threading::Thread^ td = gcnew System::Threading::Thread(gcnew System::Threading::ThreadStart(MainLoop)); // v2; didn't
    std::thread td(MainLoop); // v3, didn't
}

也通过

System::ComponentModel::DoWorkEventHandler()
试过,很可能我做错了什么

我想使用这个构造函数,但是我无法拉出到 MainLoop 的链接:

public : Thread(System::Threading::ThreadStart^ start)

标准流同样的问题

std::thread
,MainLoop 不是函数引用

我在谷歌上找不到任何关于我的问题的信息。非常感谢您的帮助!

multithreading c++-cli clr
1个回答
0
投票

现在VS不骂人有两种选择:

System::Threading::Thread^ td = 
    gcnew System::Threading::Thread(gcnew System::Threading::ThreadStart(this, &MainLoop));
    std::thread td(this);

我认为它应该有效

[编辑]

&MainLoop
- “&”编译器不会跳过

第二个选项导致错误 C2665

© www.soinside.com 2019 - 2024. All rights reserved.