Visual Studio C++2010を使用しています。
メインスレッド以外から、メインフォームクラスの外部で宣言された関数から、GUIでスレッドセーフな変更を行いたい。これが私が持っているいくつかのコードです:
メインクラスの外:
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);
================================================== = @EDIT
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;
グローバルクラスで。