Reputation: 819
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
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