Search code examples
c#multithreadingthreadpoolqueueuserworkitem

C# Can I pass more than one data into my target method when using ThreadPool?


use ThreadPool.QueueUserWorkItem (WaitCallback, Object) to start a thread with my target method and data. Can I pass more than one data into my method? the second parameter in QueueUserWorkItem (WaitCallback, Object) can be an array?


Solution

  • Here is a example of using a class so you can get strongly typed pramaters

    public class CreateUserTaskInfo
    {
        public string username { get; };
        public string password { get; };
        public string sqlServer { get; };
        public string database { get; };
        public string practice { get; };
        public RemoteUserManager client { get; };
        public CreateUserTaskInfo(RemoteUserManager cli, string usr, string pass, string sql, string db, string prac)
        {
            client = cli;
            username = usr;
            password = pass;
            sqlServer = sql;
            database = db;
            practice = prac;
        }
    }
    
    public void ExampleFunction(...)
    {
        //gather up the variables to be passed in
        var taskInfo = new CreateUserTaskInfo(remote, user, password, SqlInstancePath, AccountID, practiceName);
    
        //queue the background work and pass in the state object.
        ThreadPool.QueueUserWorkItem(new WaitCallback(RemoteUserManagerClient.CreateUser), taskInfo);
    }
    
    static public void CreateUser(object stateInfo)
    {
        CreateUserTaskInfo ti = (CreateUserTaskInfo)stateInfo;
    
        //use ti in the method and access the properties, it will be 
        // the same object as taskInfo from the other method
    }