bebo
bebo

Reputation: 67

call Invoke method from a custom class

i was have a problem like this

My form doesn't properly display when it is launched from another thread

now my question is how to call Invoke method from a custom class not from a Form

void call_thread()
    {

        Thread t = new Thread(new ThreadStart(this.ShowForm1));
        t.Start();

    }

 delegate void Func();
    private void ShowForm1()
    {            
        if (this.InvokeRequired) //error
        {
            Func f = new Func(ShowForm1);
            this.Invoke(f); //error
        }
        else
        {
            Form1 form1 = new Form1();
            form1.Show();
        }            
    }

Upvotes: 0

Views: 1139

Answers (2)

bebo
bebo

Reputation: 67

i get the Answer

in the thread i can call form1.ShowDialog(); it is not appear as a Dialog because it in another thread

the new code is

void call_thread()
    {

        Thread t = new Thread(new ThreadStart(this.ShowForm1));
        t.Start();

    }

    private void ShowForm1()
    {            
           Form1 form1 = new Form1();
            form1.ShowDialog();

    }

Upvotes: 0

BFree
BFree

Reputation: 103770

You can't. Invoke is specific to Winforms Controls as it enters a message into the Windows Message pump to do whatever it is you need to do. Therefore, in your custom class, where there's obviously not Message Pump, this can't be done.

Upvotes: 1

Related Questions