This is the second question in a series concerning updating the UI from another thread. I am trying to use a delegate to tell the UI to execute the update function (no data needs to be passed). I created a delegate in the UI thread and declared it on top of the UI thread:
delegate void MyDel();
public ref class Form1 : public System::Windows::Forms::Form
{
// .....
void testFunc()
{
this->local_long_textBox->Text = "Test!!!!!!!";
}
private:
void startUp()
{
MyDel^ DelInst = gcnew MyDel(this,&CotStinger::Form1::testFunc);
I would like to pass DelInst to another thread upon creation but when I try to declare MyDel as an extern like this on top of another module:
extern delegate MyDel;
I get the error:
Error C2146: syntax error : missing
';'before identifier'MyDel'.
If I try this
extern delegate void MyDel();
I get the error:
Error C2144: syntax error :
'void'should be preceded by';'
So how do I get the other class to recognize the delegate type so I can pass the delegate pointer to the constructor?
The
delegatekeyword is used to define a delegate type, not to declare a variable that happens to be some type of delegate. I.e., once the delegate type has been defined, you don’t need thedelegatekeyword at any point.Additionally, global variables of managed types are not allowed in C++/CLI; the usual workaround is to use a logically-static managed class with public static data members that function as globals:
The same as with any other type — put it in a header file that both classes can
#include.