darthwillard
darthwillard

Reputation: 819

spawning thread using a method without being static?

is it possible to spawn a new thread using a method that isn't static? i have a program in wpf, i want to spawn a thread when it starts. i'm trying to do this:

static Thread thread = new Thread(new ThreadStart(SomeMethod));

private void SomeMethod()
{  
SendingMessage("hello");
SendingMessage("what's up");
}

private void SendingMessage(string x)
{
if (x=="hello")
      //Do something awesome here
if (x=="what's up")
      //do something more awesomer here
}

public MainWindow()
{
     InitializeComponent();
     thread.Start();
}

i believe i'm doing something wrong here.

Upvotes: 0

Views: 156

Answers (1)

Ilia G
Ilia G

Reputation: 10221

It is not going to compile because you trying to reference instance member within static context. Simply move the Thread thread = new Thread(new ThreadStart(SomeMethod)); into your constructor and it should complile.

Upvotes: 3

Related Questions