I want to execute some operations on a worker thread while displaying a progress bar to the user. I've created a class
public class ProgressBar
{
public void StartAsyncTask(Action action)
{
Task t = new Task(action);
t.start();
}
}
I found out that I can send any method to the StartAsyncTask in the following way:
ProgressBar pb = new ProgressBar();
pb.StartAsyncTask( () => DoSomething(15, "something"));
public void DoSomething(int i, string s)
{
//do something
}
First of all, I can't seem to understand what is and how is lambda expression - () => - translated and how is the Action object passed a delegate with an unknown number of parameters.
I would like to use a BackgroundWorker with my ProgressBar but in this case I would need to invoke the action. So something like this:
void m_backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
Action action = e.Argument as Action; //same action here passed through EventArgs
//but already in a worker thread so no need for the Task object
//and now i need to somehow invoke the action object but i don't know what the parameters are.
action.Invoke( ? );
}
How is it possible in the first example to execute the action without knowing the parameters in StartAsyncTask(Action action) method?
Why do I need to know the parameters when invoking the action in this case?
Everything about how/why/when to use "Action" is pretty unclear to me even if I read MSDN documentation and some other threads here.
Any information on this will help me.