我正在使用 Visual Studio C++ 2010。
我想从主线程以外的 GUI 中进行线程安全更改,从在主 Form 类之外声明的函数。这是我的一些代码:
在主类之外:
public delegate void DEBUGDelegate(String^ text);
(...)
int lua_debug(lua_State *L){
// boolean debug(message)
Globals^ Global = gcnew Globals;
String^ debugMsg = gcnew String(lua_tostring(L, 1));
DEBUGDelegate^ myDelegate = gcnew DEBUGDelegate(Global->FORM, &Form1::DEBUGDelegateMethod);
Global->FORM->Invoke(myDelegate, gcnew array<Object^> { "HEYO! \r\n" });
lua_pushboolean(L, true);
return 1;
}
在主类内部:
public ref class Form1 : public System::Windows::Forms::Form
{
(...)
public: void DEBUGDelegateMethod(String^ text)
{
this->DEBUGBOX->Text += text;
}
(...)
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
Globals^ Global = gcnew Globals;
Global->FORM = this;
}
private: System::Void button1_Click_1(System::Object^ sender, System::EventArgs^ e)
{
Globals^ Global = gcnew Globals;
DEBUGDelegate^ myDelegate = gcnew DEBUGDelegate(this, &Form1::DEBUGDelegateMethod);
this->Invoke(myDelegate, gcnew array<Object^> { "HEYO! \r\n" });
}
}
所以问题是,如果我评论函数“lua_debug”并且其余的保持不变,它可以正常工作并且单击button1会使文本出现在调试文本框中。当我使用 lua_debug 取消注释该部分时,出现错误:
d:\prog\c++\x\x\Form1.h(146): error C2653: 'Form1' : is not a class or namespace name
1>d:\prog\c++\x\x\Form1.h(146): error C2065: 'DEBUGDelegateMethod' : undeclared identifier
1>d:\prog\c++\x\x\Form1.h(146): error C3350: 'X::DEBUGDelegate' : a delegate constructor expects 2 argument(s)
146行是:
DEBUGDelegate^ myDelegate = gcnew DEBUGDelegate(Global->FORM, &Form1::DEBUGDelegateMethod);
==================================================== = @编辑
在 Form1 声明后移动 lua_debug 后,我收到此错误:
d:\prog\c++\x\x\Form1.h(1829): error C2440: 'initializing' : cannot convert from 'System::Windows::Forms::Form ^' to 'X::Form1 ^'
1> No user-defined-conversion operator available, or
1> Cast from base to derived requires safe_cast or static_cast
1>d:\prog\c++\x\x\Form1.h(1829): error C3754: delegate constructor: member function 'X::Form1::DEBUGDelegateMethod' cannot be called on an instance of type 'System::Windows::Forms::Form ^'
排队:
DEBUGDelegate^ myDelegate = gcnew DEBUGDelegate(Global->FORM, &Form1::DEBUGDelegateMethod);
Global->FORM 声明为:
static Form^ FORM;
在全局类。