更新时间:2023-12-06 12:20:28
TestGUI 是一个CLI / C ++类,你应该使用 ^
不是 *
它的指针,但这不是唯一的问题。看来你想在pthread中执行一个CLI / C ++类的成员方法。要使其工作,您可以尝试以下方式:
Since TestGUI
is a CLI/C++ class, you should use the ^
not *
to dereference its pointer, but thats not the only problem. It seems you want to execute a CLI/C++ class member method in a pthread. To make it work, you can try the following way:
*从 StaticCallFunc $ c> TestGUI 类并使其成为一个全局方法。
*要通过 TestGUI
指向非托管函数,使用 gcroot
。所以定义一个容器类。 PtrContainer
作为成员拥有 gcroot
。
*Remove StaticCallFunc
from TestGUI
class and make it a global method.
*To pass TestGUI
pointer to an unmanaged function you can use gcroot
. So define a container class e.g. PtrContainer
which has gcroot
as a member.
//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
如下:
When you bind to pthread_create
you can use PtrContainer
as the following:
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 );
}
您应该删除驱动程序中的容器指针c> StaticCallFunc(void cClientFunc c>);
And you should delete the container pointer inside the driver method (StaticCallFunc
) after you have done with it:
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组件时,您必须小心地以线程安全的方式调用您的控件。
One more note; when you are accessing a .NET gui component in a multithread way, you must be careful to make calls to your controls in a thread-safe way.
编辑:我有添加了以下完整的源代码,它在Visual Studio 11,Windows7下编译和工作。
I have added the following complete source code which compiles and works under 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