0

Want to host object not in gui thread, all methods of this object will run in this new thread. Something like that:

Thread thread = new Thread(() =>
{
    MyDataInstance = new MyData();
});

thread.SetApartmentState(ApartmentState.STA);
thread.Start();

But this will not work. Is there any nice way to do it?

(Can create window in other thread, make it invisible, and then host it there, but it seems not the best solution)

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
Jack Malkovich
  • 786
  • 13
  • 33
  • What are you trying to achieve and what exactly is going wrong? What you have is basically correct - but you haven't said what the error is you are getting. – ChrisF Nov 02 '13 at 15:41
  • Sorry, edited question. Want to run all methods of object in this thread. My code will stop, after creating object. – Jack Malkovich Nov 02 '13 at 15:44
  • You maw want to read [this](http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx) – L.B Nov 02 '13 at 16:29

1 Answers1

0

You need to call a method that does something. All you are doing with your current code is setting up your data instance.

So you need to call a method like this:

Thread thread = new Thread(new ThreadStart(MethodThatDoesStuff)) { MyDataInstance = new MyData(); };

thread.SetApartmentState(ApartmentState.STA);
thread.Start();

MethodThatDoesStuff will have to have a loop so that it continues do to things forever, or have some other control mechanisms (setting up event handlers etc.)

ChrisF
  • 134,786
  • 31
  • 255
  • 325