No Overload For 'void' Matches Delegate 'ThreadStart' - c#

I want to run function with void as return type in new thread, but it always shows this error:
No overload for 'myVoid' matches delegate 'ThreadStart'
and my code :
Thread t = new Thread(new ThreadStart(myVoid)); // <-- Error Shows Here
t.Start("Test","Test2");
// And The Void :
void myVoid(string text, string text2)
{
Console.WriteLine(text + text2);
}
How I fix it? What is wrong?

ThreadStart delegate expects a delegate that takes no parameters. If you would like to use myVoid in a thread, you need to provide a way to make a match between myVoid and a no-argument delegate.
One way of doing it is by providing a lambda, like this:
Thread t = new Thread(new ThreadStart(() => myVoid("Test", "Test2")));
t.Start();

The ThreadStart delegate you are using does not define any arguments.
This means your method myVoid which has 2 string arguments does not match the delegate.

Related

How to convert thread Code (VB.net to C#)..?

how to convert my VB.net code to C#..?
I want to use a looping function with a thread.
VB.Net Code
For i As Integer = 0 To _port_count
Dim worker As New Threading.Thread(AddressOf looping)
worker.Start(_list(i))
commThread.Add(worker)
Next
public sub looping(Byvar PortList As PortList) 'looping function
C# Code
for (int i = 0; i <= _port_count; i++)
{
Thread worker = new Thread(looping);
worker.Start(_list[i]);
commThread.Add(worker);
}
public static void looping (PortList PortList) {}
but C# code didn't work. :(
thanks for your helps.
The Thread constructor will take either a ThreadStart delegate or a ParameterizedThreadStart delegate. The first has no parameters and the second has one parameter of type object. If you're using a named method then it has to match one of those two signatures, which yours does not. VB will allow you to do the wrong thing and attempt to clean up for you if it can but C# expects you to do the right thing yourself. These days, if you want to call a method whose signature does not match, you can use a Lambda with a matching signature instead, then call your method in that:
Thread worker = new Thread(() => looping(_list[i]));
worker.Start();
What's declaration of looping function ?
And some more info ?
Here is an example from Microsoft Docs.
using System;
using System.Threading;
public class Work
{
public static void Main()
{
// Start a thread that calls a parameterized static method.
Thread newThread = new Thread(Work.DoWork);
newThread.Start(42);
// Start a thread that calls a parameterized instance method.
Work w = new Work();
newThread = new Thread(w.DoMoreWork);
newThread.Start("The answer.");
}
public static void DoWork(object data)
{
Console.WriteLine("Static thread procedure. Data='{0}'",
data);
}
public void DoMoreWork(object data)
{
Console.WriteLine("Instance thread procedure. Data='{0}'",
data);
}
}
// This example displays output like the following:
// Static thread procedure. Data='42'
// Instance thread procedure. Data='The answer.'
link: https://msdn.microsoft.com/en-us/library/1h2f2459(v=vs.110)
Change you looping-method signature to following:
public static void looping (object PortList)
As documentation says, ParameterizedThreadStart passed as Thread constructor parameter has to be object.

How to put a method with aguments on a different thread?

I have the method that takes an argument from external dll through a delegate pointer:
public delegate void NotificationsCallbackDelegate(CinectorNotification message);
NotificationsCallbackDelegate notificationsCallbackDeleg;
IntPtr notificationsCallbackPointer;
notificationsCallbackDeleg = NotificationsCallback;
notificationsCallbackPointer = Marshal.GetFunctionPointerForDelegate(notificationsCallbackDeleg);
instance.SetNotificationsCallback(notificationsCallbackPointer);
private void NotificationsCallback(CinectorNotification notif)
{
Log.AppendText("[" + notif.timestamp + "] " + notif.type + ": " + notif.message + Environment.NewLine);
}
So, input argument 'message' is something that can be passed by dll at any time during the application flow, whenever the external engine generates some log. It is like an event.
I use the following code to put a method on a new thread:
private void startNotifications()
{
Thread NotificationsThread = new Thread(new ThreadStart(NotificationsCallback));
NotificationsThread.IsBackground = true;
NotificationsThread.Start();
}
However, the method NotificationsCallback(CinectorNotification notif) takes in an argument and therefore doesn't match the delegate for ThreadStart. So how would I put a method like this on a different thread?
You can use Task.Run and supply it a lambda where you call NotificationsCallback with whatever arguments you need For example:
private void startNotifications()
{
Task.Run(() => NotificationsCallback(arg1, arg2) );
}
This will result in NotificationsCallback being run on on a ThreadPool thread.
For more information on Task.Run, see: https://msdn.microsoft.com/en-us/library/hh195051(v=vs.110).aspx
You code would work if there is a method passed in the parameter is parameterless.
If you want a parametrised thread, you should use another overload of the Thread constructor which will take ParameterizedThreadStart.
Start method on the thread is then called with an argument which is passed on to the thread method like this:
Thread thread = new Thread(WorkerMethod);
thread.Start(35); //for example 35
void WorkerMethod(object parameter)
{
int? intParameter = parameter as int?; //for example
if (intParameter.HasValue)
{
//do stuff
}
}
You can also use Task, which is a more modern approach to multi-threading.
Task.Factory.StartNew(() => WorkerMethod(35)); //for example 35
Or Task.Run in .NET 4.5 and newer. In task one can pass as many paramaters as wanted (without creating a container for them) and also the parameters can be strongly typed... not just object.
Instead of using ThreadStart you could use ParameterizedThreadStart and pass the required parameters in Thread.Start.

How to "pass in any number of arguments to the method" of a thread through lambda expression?

I follow Passing Data to a Thread of "PART 1: GETTING STARTED" of "Threading in C#" by Joseph Albahari.
Namely the passage:
====== Start of quote
"With this approach, you can pass in (to where?) any number of arguments to the method. You can even wrap the entire implementation in a multi-statement lambda:
new Thread (() =>
{
Console.WriteLine ("I'm running on another thread!");
Console.WriteLine ("This is so easy!");
}).Start();*
You can do the same thing almost as easily in C# 2.0 with anonymous methods:
new Thread (delegate()
{
...
}).Start();
============ End of quote
That is, I've tried the "easily" as:
new Thread
(delegate
{
Console.WriteLine("I'm running on another thread!");
Console.WriteLine("This is so easy!");
}
).Start();
but it produces the error:
The call is ambiguous between the following methods or properties:
'System.Threading.Thread.Thread(System.Threading.ThreadStart)' and
'System.Threading.Thread.Thread(System.Threading.ParameterizedThreadStart)'
How can you disambiguate the code in order to run it? Answered (missed parenthesis. Anyway, it wasn't the original main question)
Also, I did not quite grasp where is the empty list () => directed/applied to?
A well as, what is the method to which " you can pass in any number of arguments to the method"?
How to understand the passing of (any number of) arguments through the empty list?
Update (addressing Jon Skeet's comment):
No, I am not stuck with C# 2.
The same question(s) to the previous passage:
========== Start of quote:
"The easiest way to pass arguments to a thread’s target method is to execute a lambda expression that calls the method with the desired arguments:
static void Main()
{
Thread t = new Thread ( () => Print ("Hello from t!") );
t.Start();
}
static void Print (string message)
{
Console.WriteLine (message);
}
With this approach, you can pass in any number of arguments to the method."
=============== End of quote
Update2:
The most complete answer is IMO by #Lee though I marked as correct another answer for guessing to answer at once what I have not even originally asked - how to put something in empty parenthesis (I'm already afraid to call it by list or by arguments)
You need to make the argument list explicit:
new Thread
(delegate()
{
Console.WriteLine("I'm running on another thread!");
Console.WriteLine("This is so easy!");
}
).Start();
The delegate keyword allows you to define an anonymous method. Lambda expressions (i.e. using the () => { ... } syntax) are similar, however using delegate allows you to omit the parameter list. This is ambiguous in this case, since there are two constructors for Thread which take different delegate types. One takes a ThreadStart which is defined as
delegate void ThreadStart();
and the other takes a ParameterizedThreadStart which is defined as:
delegate void ParameterizedThreadStart(object state);
Since you are omitting the parameter list, the compiler does not know which delegate type you are using.
I assume the "any number of arguments" are variables which are closed-over by your delegate. For example you could have:
string message = "This is so easy!";
var thread = new Thread(delegate() {
Console.WriteLine(message);
});
thread.Start();
You can use a ParameterizedThreadStart to pass an arbitrary object to your thread delegate e.g.
public class ThreadData {
//properties to pass to thread
}
ThreadData data = new ThreadData { ... }
Thread thread = new Thread((object state) => {
ThreadData data = (ThreadData)state;
});
thread.Start(data);
You need the parentheses after delegate to specify the parameters, in this case no parameters:
new Thread(
delegate() {
Console.WriteLine("I'm running on another thread!");
Console.WriteLine("This is so easy!");
}
).Start();
The "any number of arguments" that the author is talking about is that you can use data from the scope where the delegate is created inside the code that runs in a separate thread, without having to pass the data to the Start method:
string msg1 = "I'm running on another thread!";
string msg2 = "This is so easy!";
new Thread(
delegate() {
Console.WriteLine(msg1);
Console.WriteLine(msg2);
}
).Start();
What's actually happening is that the variables are no longer local variables in the method, instead they are automatically stored in a closure, which the delegate shares with the method where it is defined.
This works well as long as you only want to start one thread. If you want to start multiple threads that uses different data, you would either pass the data into the Start method or create a class that can hold the data, put the code for the thread in the class, and create one instance for each thread that you start.
In order to resolve ambiguous call, you can add empty parentheses (delegate will be treated as ThreadStart delegate):
new Thread(delegate() {
Console.WriteLine("I'm running on another thread!");
Console.WriteLine("This is so easy!");
}).Start();
Or add state parameter (delegate will be treated as ParametrizedThreadStart) It works, but it's odd, because you don't need that parameter.
new Thread(delegate(object state) {
Console.WriteLine("I'm running on another thread!");
Console.WriteLine("This is so easy!");
}).Start();
Or cast delegate to ThreadStart
new Thread((ThreadStart)delegate {
Console.WriteLine("I'm running on another thread!");
Console.WriteLine("This is so easy!");
}).Start();
() => isn't an empty list in C#. In the context it is used in the book it is the start of a lambda expression. The () means that this expression takes no arguments.

How to pass a method to Thread constructor?

How can I pass a Method2 to thread as shown in below if i write code as shown in below it shows an error Error
The best overloaded method match for
'System.Threading.Thread.Thread(System.Threading.ThreadStart)' has
some invalid arguments
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Main Thread : ");
Thread obj = new Thread(Method2);
obj.Start();
Console.ReadLine();
}
private static int Method2(int a)
{
return a;
}
}
When i use the following code it works
Thread obj = new Thread(() => Method2(1));
But why it's not working when i pass delegate object
delegate int del(int i);
del d = Method2;
Thread obj = new Thread(d);
What is the difference between above 2 , in 1st case i used Lambda expression in second case directly passed delegate object is there any thing else?
The Thread constuctor takes two types of delegates, ThreadStart and ParameterizedThreadStart. To see what type of method these delegates accept you can create it and the constructor will show you, so for example,
var parameterMethod = new ParameterizedThreadStart(...
If you type the above you will see the delegate takes a function with one object as the parameter and returns void.
void (object) target
This is why your Method that takes an int will not work because the signature does not match the delegate target signature. When you wrap it in a lambada, you are actually passing in a method that takes no parameters and returns void, so the Method2 signature is not even looked at. Wrapping method calls with a lambada like this can be very useful for getting the signature to match.
delegate void del(object obj);
del d = () => Method2(1);
Thread obj = new Thread(d);
There is an infinite number of possible method signatures so Threads and Tasks keep it simple and say if you want to pass something, pass a single object. Since all types derive from object, this lets you pass in anything you want, then cast it later.
Try this:
Thread obj = new Thread(() => Method2(some_int_value));

C# Threadstart Error

I want to start a new thread for one simple method but that method has variables I need to pass it.
Thread tempmovethread = new Thread(new ThreadStart(widget.moveXYZINCHES(xval,yval,zval));
I am getting the error: "Method name expected".
That is the right method name and I did something very similar to this in an earlier bit of code and it worked, the only difference is the method I called before didnt need any variables to be passed:
executethread = new Thread(new ThreadStart(execute.RunRecipe));
Is it possible to start a new thread and pass the variables like this, or do I have to do it another way?
Use an Action to create the correct delegate type.
Thread tempmovethreading = new Thread(new ThreadStart(new Action(() => widget.moveXYZINCHES(xval,yval,zval)));
tempmovethread = new Thread(new ParametrizedThreadStart(widget.moveXYZINCHES);
tempmovethread.Start(new []{xval,yval,zval});
BUT
you should appropriately change the method's signature like this (assuming the used parameters are of type int:
public void moveXYZINCHES(object state)
{
int xval = (state as int[])[0],yval = (state as int[])[1],zval = (state as int[])[2];
...your code
}

Categories