Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I am facing a problem when creating a thread for method which takes dynamic arguments.
I am using c# 3.5

This is my method
C#
public void ToggleFunction(params int[] values)
{
  Do something
}


I tried many possible ways. But I am facing problem in all the ways I tried.

C#
Thread prvw = new Thread(() => ToggleFunction(myInts));

While creating thread it throws error "A field initializer cannot reference the non-static field, method or propery"

Please help...
Posted
Updated 15-Apr-14 23:08pm
v3

Hi,

I'm writing an article about this subject called: "How To: Simplify the use of threads in WPF"

Here is a preview that can help you:

C#
public void RunByNewThread(Action action)
{
  action.BeginInvoke(ar => ActionCompleted(ar, res => action.EndInvoke(res)), null);
}


Just send your anonymous method as a parameter to this method :)
 
Share this answer
 
v3
P.S.: Here is the article I've promised:

How To: Simplify the Use of Threads in WPF[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900