Gre*_*ego 1 windows c++-cli pthreads visual-c++
我在Visual C++ 2008 Professional上使用C++/CLI,因为我使用的是Windows Forms,这意味着我有托管代码而我正在尝试调用静态函数LoginAccounts,但是我得到一个错误,可能是因为我混合了使用非托管代码管理,但我无法弄清楚该做什么.我正在使用PThread for Windows
System::Void testing_Click(System::Object^ sender, System::EventArgs^ e) {
pthread_create(&t, NULL, &Contas::LoginAccounts, this); //Error in this line
}
Run Code Online (Sandbox Code Playgroud)
错误13错误C3374:除非创建委托实例,否则无法获取'Tester :: Test :: LoginAccounts'的地址
我该怎么做才能解决这个问题?这可能是一个简单的解决方案,但我无法弄清楚.提前致谢.
void LoginAccounts(){
this->btn_next->Enabled = false;
this->login_accounts->Enabled = false; //Unhandled exception here
if(this->clb_contas->CheckedItems->Count <= 0){ //Unhandled exception here
}
}
System::Void testing_Click(System::Object^ sender, System::EventArgs^ e) {
ThreadStart^ start = gcnew ThreadStart(this, &Login::LoginAccounts);
Thread^ t = gcnew Thread(start);
t->Start();
}
Run Code Online (Sandbox Code Playgroud)
如果您只想调用托管代码,那么使用pthreads就没有意义了.请改用System :: Threading :: Thread类.您仍然需要创建错误消息抱怨的委托,委托是函数指针的托管等效项.有了铃声,它们不仅存储函数地址,还包装对象指针.使代码看起来类似于:
using namespace System::Threading;
...
private:
void LoginAccounts() {
// etc...
}
System::Void testing_Click(System::Object^ sender, System::EventArgs^ e) {
ThreadStart^ start = gcnew ThreadStart(this, &Form1::LoginAccounts);
Thread^ t = gcnew Thread(start);
t->Start();
}
Run Code Online (Sandbox Code Playgroud)
注意LoginAccounts()在这里是一个实例方法,不需要使用this引用来执行hokeypokey .
如果你确实想要使用pthreads,那么使用Marshal :: GetFunctionPointerForDelegate()将委托转换为可以传递给本机代码的指针.注意,你必须保持自己引用的委托对象.垃圾收集器无法查看本机代码所持有的引用.而且你还无法通过这个无钉扎.这些是非常难看的细节,您可以通过简单地使用Thread类来避免这些细节.
| 归档时间: |
|
| 查看次数: |
1816 次 |
| 最近记录: |