how to use delegate to call function and introduce function as content?
-
Good Idea,i meet it. And if i have a button at main Thread , it can abort Thread "th". How to code button click function? And, if whether Thread th end or abort, i want to run" MessageBox.Show("Thread end")", how can i do it?
smallkubi wrote:
it can abort Thread "th"
Don't. How To Stop a Thread in .NET (and Why Thread.Abort is Evil)[^] If you're using .NET 4.5 or above, you'd probably have better luck using
async
andawait
. Asynchronous Programming with Async and Await (C# and Visual Basic)[^] Otherwise, use something like theBackgroundWorker
component[^].
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Something like this:
Thread th = new Thread(() => fun1(x, y => fun3(y, z)));
Breaking it down:
Action<int> call = y => fun3(y, z);
ThreadStart start = () => fun1(x, call);
Thread th = new Thread(start);
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
hi, If i use Func call= y=>fun3(y,z) , fun3 can be one output param function. But if my function need 2 or more output, how can i do it?
Sorry, I'm not following you. Do you mean you want multiple return values from your function? If so, you'll need to create a class or structure to contain them:
public struct Fun3ReturnValues
{
public int SomeValue { get; set; }
public string SomeOtherValue { get; set; }
}
...
public static Fun3ReturnValues Fun3(int y, int z)
{
return new Fun3ReturnValues
{
SomeValue = 42,
SomeOtherValue = "Hello",
};
}Alternatively, you could use the
Tuple
class[^], but the meaning of the values wouldn't be as clear.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer