how can i use delegate to output several param in multi-thread?
-
My sample as follows: Now i use Func<> can output 1 param, but i need 3 param output, how can i do it? Func call = y => fun(y, z); //i need output o1,o2, like y=>fun(y,z,out o1,out o2) ? ThreadStart start = () => fun1(x, call); Thread th = new Thread(start);
-
My sample as follows: Now i use Func<> can output 1 param, but i need 3 param output, how can i do it? Func call = y => fun(y, z); //i need output o1,o2, like y=>fun(y,z,out o1,out o2) ? ThreadStart start = () => fun1(x, call); Thread th = new Thread(start);
Use a lambda:
private void DoThread(int i, out int j, out int k) { j = i \* 2; k = i \* 3; } ... int x = -1; int y = -1; Thread start = new Thread(() => DoThread(6, out x, out y)); start.Start(); Thread.Sleep(1000); Console.WriteLine("{0},{1}", x, y);
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
Use a lambda:
private void DoThread(int i, out int j, out int k) { j = i \* 2; k = i \* 3; } ... int x = -1; int y = -1; Thread start = new Thread(() => DoThread(6, out x, out y)); start.Start(); Thread.Sleep(1000); Console.WriteLine("{0},{1}", x, y);
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
oh, DoThread function i want to transfer to Thread as a var, your sample is call immidiately. ThreadO th_o = new ThreadO(x => c( x, n,out F)); //this is Func<>,but i also want to out F Thread th = new Thread(new ThreadStart(th_o.Cons_Mult)); th.Start(); private double c(double x,double n,out double F) { } class ThreadO { public ThreadO(Func fun1) { ... } }
-
My sample as follows: Now i use Func<> can output 1 param, but i need 3 param output, how can i do it? Func call = y => fun(y, z); //i need output o1,o2, like y=>fun(y,z,out o1,out o2) ? ThreadStart start = () => fun1(x, call); Thread th = new Thread(start);
If you can handle one parameter, you can handle multiple; encapsulate all three items in a class, and set an instance of that new class as your parameter.
class MyClass
{
public string o1;
public object o2;
public int y { get; set; }
public int z { get; set; }
}:)
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)
-
If you can handle one parameter, you can handle multiple; encapsulate all three items in a class, and set an instance of that new class as your parameter.
class MyClass
{
public string o1;
public object o2;
public int y { get; set; }
public int z { get; set; }
}:)
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)
Yes,haha, you are right, but i want my code simplex and efficient, so i hope no more class, only several variable can solve it. Func<> can output one param, but i want more output param. ,i don't know how to do it. how can i use delegate to define multi-output delegate ?
-
My sample as follows: Now i use Func<> can output 1 param, but i need 3 param output, how can i do it? Func call = y => fun(y, z); //i need output o1,o2, like y=>fun(y,z,out o1,out o2) ? ThreadStart start = () => fun1(x, call); Thread th = new Thread(start);
I think what you want here is a way to "compose" a Func that you can use later
// sample Struct, but you could use a Class also
public struct TestStruct
{
int X;
int Y;public TestStruct(int x, int y) { X = x; Y = y; }
}
// define the Func
private Func TestFunc = (x, y) =>
{
return new TestStruct(x * 4, y * 5);
};private TestStruct testStruct1;
// test in some method
testStruct1 = TestFunc(100, 200);«Tell me and I forget. Teach me and I remember. Involve me and I learn.» Benjamin Franklin
-
Yes,haha, you are right, but i want my code simplex and efficient, so i hope no more class, only several variable can solve it. Func<> can output one param, but i want more output param. ,i don't know how to do it. how can i use delegate to define multi-output delegate ?
smallkubi wrote:
i want my code simplex and efficient, so i hope no more class
How would boxing the result be ineffcient? And no, that was not complex.
smallkubi wrote:
how can i use delegate to define multi-output delegate ?
You can
return
an struct or object, you can useout
parameters. Or even a callback that gets called for each output-variable :)Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)