0

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();
        }            
    }
Community
  • 1
  • 1
bebo
  • 67
  • 1
  • 10

2 Answers2

1

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.

BFree
  • 102,548
  • 21
  • 159
  • 201
0

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

    }
bebo
  • 67
  • 1
  • 10
  • You also have to insert t.SetApartmentState(ApartmentState.STA) or lots of stuff won't work right (clipboard, shell dialogs, drag+drop). – Hans Passant Dec 16 '10 at 20:15