从Windows窗体类调用PThread到Windows窗体类内的函数

时间:2012-03-31 11:54:41

标签: c++ visual-studio-2008 visual-c++ pthreads

我在Visual Studio 2008 Professional中的Windows窗体上使用pthread,但是我在示例源中显示的行中出现错误。可能是因为它是C ++ / CLI,因为它通常在常规类中工作。问题出在这一行:

  

((TestGUI *)上下文) - > TestxFunc();

在函数StaticCallFunc

public ref class TestGUI : public System::Windows::Forms::Form {
        /...
    public:

void TestxFunc(std::string test, std::string test2){
        this->btn_next->Enabled = false;
        cout << "HI, Test: " << test << "," << " Test 2: " << test2 << endl;

 }

static void *StaticCallFunc(void *context){
    std::string test = "foo";
    std::string test2 = "bar";
    printf("\nStarting Thread");
    ((TestGUI*)context)->TestxFunc(); //Line with the error down.
    return 0;

} 

System::Void tester_Click(System::Object^  sender, System::EventArgs^  e) {
      pthread_t t;
      pthread_create(&t, NULL, &TestGUI::StaticCallFunc, this);
}

//...
  

错误C3699:'':不能在类型'Test :: TestxFunc'1&gt;上使用此间接方法   编译器用'^'替换'*'以继续解析

     

错误C2227:' - &gt; TestxFunc'的左侧必须指向   class / struct / union / generic type

我该怎么做才能解决这个问题?这个调用通常适用于常规类,但在Windows窗体中它实际上不是

1 个答案:

答案 0 :(得分:0)

由于TestGUI是CLI / C ++类,因此您应该使用^而非*取消引用其指针,但这不是唯一的问题。您似乎想在pthread中执行CLI / C ++类成员方法。为了使其有效,您可以尝试以下方式:

*从StaticCallFunc课程中删除TestGUI并将其设为全局方法 *要将TestGUI指针传递给非托管函数,您可以使用gcroot。所以定义一个容器类,例如PtrContainer gcroot作为成员。

//dont forget forward declerations
void *StaticCallFunc(void *context); //forward decleration
ref class TestGUI; //forward decleration

//Define a simple argument class to pass pthread_create
struct PtrContainer{
     gcroot<TestGUI^> guiPtr; //you need to include vcclr.h for this
};

绑定到pthread_create时,您可以使用PtrContainer作为以下内容:

System::Void tester_Click(System::Object^  sender, System::EventArgs^  e) {

    //init. container pointer,
    //we use dynamically allocated object because the thread may use it after this method return
    PtrContainer* ptr = new PtrContainer;
    ptr->guiPtr = this;

    pthread_t t;
    pthread_create(&t, NULL, StaticCallFunc, ptr );
}

你应该在完成后删除驱动程序方法(StaticCallFunc)中的容器指针:

void *StaticCallFunc(void *context){
    std::string test = "foo";
    std::string test2 = "bar";
    printf("\nStarting Thread");
    PtrContainer* ptr = reinterpret_cast<PtrContainer*>(context);
    ptr->guiPtr->TestxFunc(test, test2);

    //dont forget to delete the container ptr.
    delete ptr;

    return 0;
}

再说一遍;当您以多线程方式访问.NET gui组件时,you must be careful to make calls to your controls in a thread-safe way.


编辑:我添加了以下完整的源代码,它们在Visual Studio 11,Windows7下编译和工作。

//sample.cpp

#include <iostream>
#include <string>
#include <vcclr.h>
#include <sstream>
using namespace std;
using namespace System;
using namespace System::Windows::Forms;

#include "pthread.h"
#include <stdio.h>

#define PTW32_THREAD_NULL_ID {NULL,0}
#define int64_t _int64

void *StaticCallFunc(void *context); //forward decleration
ref class TestGUI; //forward decleration

//Define a simple argument class to pass pthread_create
struct PtrContainer{
    gcroot<TestGUI^> guiPtr; //you need to include vcclr.h for this
};

ref class TestGUI : public System::Windows::Forms::Form  
{
public:
    TestGUI(void) {
        this->Click += gcnew System::EventHandler(this, &TestGUI::tester_Click );
    }

    void TestxFunc(std::string test, std::string test2){
            cout << "HI, Test: " << test << "," << " Test 2: " << test2 << endl;
    }

    System::Void tester_Click(System::Object^  sender, System::EventArgs^  e) {
        //init. container pointer,
        //we use dynamically allocated object because the thread may use it after this method return
        PtrContainer* ptr = new PtrContainer;
        ptr->guiPtr = this;

        pthread_t t;
        pthread_create(&t, NULL, StaticCallFunc, ptr );
    }
};

void *StaticCallFunc(void *context){
    std::string test = "foo";
    std::string test2 = "bar";
    printf("\nStarting Thread");
    PtrContainer* ptr = reinterpret_cast<PtrContainer*>(context);
    ptr->guiPtr->TestxFunc(test, test2);

    //dont forget to delete the container ptr.
    delete ptr;
    return 0;
}

int main()
{
    TestGUI^ testGui = gcnew TestGUI();
    testGui->ShowDialog();
    return 0;
}

编译:

/analyze- /clr /Od /nologo /MDd /Gm- /Fa".\Debug\" /I".." /Oy- /FU"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\mscorlib.dll" /FU"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.dll" /FU"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Windows.Forms.dll" /Zc:forScope /Fo".\Debug\" /Gy- /Fp".\Debug\Debug.pch" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "CLEANUP_C" /D "_VC80_UPGRADE=0x0600" /D "_MBCS" /WX /errorReport:queue /GS /Fd".\Debug\" /fp:precise /FR".\Debug\" /W3 /Z7 /Zc:wchar_t /EHa