1

I've been doing this in Winforms forever :

new Thread((query_) =>
{
    Functions.UpdateInsert(query_);
}).Start(query);

But when I try to do this in WPF, the IDE tells me "Delegate 'System.Threading.ThreadStart' does not take 1 arguments."

Is this done differently in WPF?

Any help would be appreciated. Thanks!

AyushISM
  • 381
  • 7
  • 21
  • No, it is not done differently in WPF, and there is ParametrizedThreadStart exists. Are you sure its not XNA? – Sergey Berezovskiy Sep 10 '14 at 14:16
  • Passing parameters to threads is obsolete. Just close over `query` in your lambda. Also, prefer a LongRunning task. – usr Sep 10 '14 at 15:27

3 Answers3

2

try using it this way :

Thread t = new Thread(()=> Functions.UpdateInsert(query));
t.Start();
Servy
  • 202,030
  • 26
  • 332
  • 449
Youness
  • 1,468
  • 1
  • 9
  • 19
0

If you take a look at this answer, you can see that Thread.Start can be called with no parameters.

I'm not sure if you're trying to do this all in one line, but if you start your thread like this instead, it should work just fine:

Thread thread = new Thread(() => Functions.UpdateInsert(query_));
thread.Start();
Community
  • 1
  • 1
Sean Cogan
  • 2,516
  • 2
  • 27
  • 42
-1

Assuming that your UpdateInsert function does some database operation and you want to start a thread to not block the user interface while doing it you should try the Async/await approach to asynchronous programming:

public async void UpdateMyThing() {
     LoadingIndicator.IsEnabled = true; //or something like that
     var query = ...
     await Task.Run(() => Functions.UpdateInsert(query));
     LoadingIndicator.IsEnabled = false; //we are done
}

This leaves all the threading/waiting/complex stuff for the C# compiler and lets us keep it simple!

Otherwise, it seems there is nothing wrong with your syntax:

new Thread((a) => { }).Start(new object());

This works in my IDE.

Bas
  • 26,772
  • 8
  • 53
  • 86