Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8794811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:18:07+00:00 2026-06-13T23:18:07+00:00

I want to execute some operations on a worker thread while displaying a progress

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:18:08+00:00Added an answer on June 13, 2026 at 11:18 pm

    I think you’re overthinking things a little bit. So let’s start from the top:

    1. A lambda expression is a notation to reference a method execution. Example:

       x => x + 3
      

      At the most basic level, this is representing a function that takes 1 input, x, and then returns a value equal to x + 3. So in your situation, your expression:

      () => DoSomething(15, "Something")
      

      Represents a method taking 0 parameters, and then invoking the method DoSomething(15, "Something"). The compiler is behind the scenes translating that into a Func or Action delegate for you. So it is in effect:

      new Action(delegate()
      {
          DoSomething(15, "Something")
      }); 
      

      The compiler rewrite of my simple expression above would be:

      new Func<int, int>(delegate(int x)
      {
          return x + 3;
      });
      
    2. Next up, if you want to invoke an action later, the syntax for doing so is fairly straightforward:

      Action someMethod = new Action(() => { Console.WriteLine("hello world"); }));
      someMethod(); // Invokes the delegate
      

      So if you have a given Action instance, simply invoking it with the () syntax is all you need, since Action is a delegate that takes 0 parameters and returns nothing.

      A function is similarly easy:

      Func<int, int> previousGuy = x => x + 3;
      var result = previousGuy(3); // result is 6
      
    3. Lastly, if you want to pass along a method to invoke, and you don’t have context for the parameters at that point, you can simply wrap your call in an action and invoke that later. For example:

      var myAction = new Action(() =>
           {
                // Some Complex Logic
                DoSomething(15, "Something");
                // More Complex Logic, etc
           });
      
      InvokeLater(myAction);
      
      public void InvokeLater(Action action)
      {
            action();
      }
      

      All of the data is captured in a closure of your method, and thus is saved. So if you can manage to pass along an Action to your event with the e.Argument property, all you would need to do would be to call (e.Argument as Action)().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to perform some operations on newly registered user when he hits the
i want to execute some commands using NSIS script , but for the commands
I want to execute some Python code, typed at runtime, so I get the
I have <s:property value=somevalue id=someid> feild. and i want to execute some scipt to
I need execute some code before Windows shutdown process each time. So, I want
I want to execute a code helloword.cpp which takes in some argument from console
I want to print some dynamic query to execute a procedure on all tables
I want to wrap some queries in a SharedDbConnectionScope and execute them under a
I have asp.net generated html code. I want execute jQuery click function when user
I need to execute async delete operation with user confirmation. Something like this: public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.