Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • c# : How to use ThreadPool.QueueUserWorkItem ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.62k
    Comment on it

    In this post we will see a simple code example which illustrates how to use the ThreadPool to queue up tasks in a multi-threaded environment. We first need to create an object that contains information required for the task. This object will be passed as parameter to a task that is queued for execution by the thread pool.

    private  void QueueWork(string parameter)
    {
    
     // Instantiate  an object containing the information required for the task
       object[] obj = new object[3];
       obj[0] = "Work 1";
       obj[0] = "Work 2";
       obj[0] = "Work 3";
    
        //Queue the task and pass required data as paramaeter
       ThreadPool.QueueUserWorkItem(new System.Threading.ThreadPool.QueueUserWorkItem(DoWork) , obj);
    
    }

    // The thread performs the independent task

    private  void DoWork(object workItem)
    {
         object[] workList =(object[])workItem;
    
         // do the work as per details in workList 
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: