C ++ / CLI中的错误,除非使用Pthread创建委托实例,否则无法获取函数的地址 [英] Error in C++/CLI, Can't take address of function unless creating delegate instance using Pthread

查看:82
本文介绍了C ++ / CLI中的错误,除非使用Pthread创建委托实例,否则无法获取函数的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual C ++ 2008 Professional上使用C ++ / CLI,并且由于我在使用Windows Forms,这意味着我已经托管了代码,并且试图调用静态函数LoginAccounts,但是我收到错误消息可能是因为我正在将托管代码与非托管代码混合使用,但是我不知道该怎么办。我正在使用Windows PThread

I'm using C++/CLI on Visual C++ 2008 Professional, and since I'm using Windows Forms that means I have managed code and I'm trying to call the static function LoginAccounts, but I get an error probably because I'm mixing Managed with Unmanaged Code, but I can't figure out what to do though. I'm using PThread for Windows

System::Void testing_Click(System::Object^  sender, System::EventArgs^  e) {
    pthread_create(&t, NULL, &Contas::LoginAccounts, this); //Error in this line
}




错误13错误C3374:除非创建委托实例,否则不能使用'Tester :: Test :: LoginAccounts'的地址

Error 13 error C3374: can't take address of 'Tester::Test::LoginAccounts' unless creating delegate instance

我该怎么办这个?
这可能是一个简单的解决方案,但我不知道。
预先感谢。

what do I do to solve this? It's probably a simple solution, but I can't figure out. Thanks in advance.

 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();
        }


推荐答案

使用毫无意义如果您只想调用托管代码,则使用pthreads。请改用System :: Threading :: Thread类。您仍然需要创建错误消息抱怨的委托,委托与函数指针的托管等效。响铃时,它们不仅存储函数地址,而且还包装对象指针。使代码看起来类似于:

There's just no point in using pthreads if all you want to do is call managed code. Use the System::Threading::Thread class instead. You still need to create the delegate that the error message is complaining about, delegates are the managed equivalent of a function pointer. With bells on, they don't just store the function address but also wrap the object pointer. Make the code look similar to this:

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();
    }

请注意,LoginAccounts()如何在此处是实例方法,无需执行带有 this 参考的hokeypokey。

Note how LoginAccounts() is an instance method here, no need to do the hokeypokey with the this reference.

如果确实要使用pthread,请使用Marshal :: GetFunctionPointerForDelegate()将委托转换为指针,然后将其传递给本机代码。注意,您必须保留自己引用的委托对象。垃圾收集器看不到本机代码保留的引用。而且,您仍然无法不通过而传递 this 。这些非常丑陋的细节,只需使用Thread类就可以避免。

If you really, really want to use pthreads then use Marshal::GetFunctionPointerForDelegate() to convert the delegate to a pointer that you can pass to native code. Watch out, you have to keep the delegate object referenced yourself. The garbage collector cannot see references held by native code. And you still can't pass this without pinning. These are very ugly details that you can avoid by simply using the Thread class instead.

这篇关于C ++ / CLI中的错误,除非使用Pthread创建委托实例,否则无法获取函数的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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