I have an application that manages user details. Each user is an object and need to perform the same code but needs to be on different threads. The problem I have is that the routine that spawns the threads seems to be unpredictable.

Example


public class User
* * {
* * * * private string m_name = string.Empty;
*
* * * * #region Get/Sets
* * * * public string Username
* * * * {
* * * * * * get
* * * * * * {
* * * * * * * * return m_name;
* * * * * * }
* * * * * * set
* * * * * * {
* * * * * * * * m_name = value;
* * * * * * }
* * * * }
* * }

# Methods
// Delegate as I'm passing in an object
private delegate bool _Test_E3(User _u);
public bool Test_E3(User _u)
* * * * {
* * * * * * Thread.Sleep(rand.Next(500, 2000));
* * * * * * System.Diagnostics.Debug.WriteLine("Test_E3.Username\t= " + _u.Username);
* * * * * * _u = null;
* * * * * * return true;
* * * * }

// _MainUserList.GetUserList() returns list<User> mylist <User>();

// Main Method that kicks off all the users
*public bool Go_E4()
* * * * {
* * * * * * System.Diagnostics.Debug.WriteLine("Go_E4 [ENTER]");

* * * * * * bool b_result = false;

* * * * * * foreach (User _usr in _MainUserList.GetUserList())
* * * * * * {
* * * * * * * * System.Diagnostics.Debug.WriteLine("Go_E4 Executing user = " + _usr.Username);

* * * * * * * * lock (_object)
* * * * * * * * {
* * * * * * * * * *ThreadPool.QueueUserWorkItem(delegate
* * * * * * * * * * {
* * * * * * * * * * * * * * _Test_E3 call_E3 = new _Test_E3(Test_E3);
* * * * * * * * * * * * * * b_result = call_E3.Invoke(_usr);
* * * * * * * * * * * * });
* * * * * * * * }
* * * * * * }

* * * * * * System.Diagnostics.Debug.WriteLine("Go_E4 [EXIT]");
* * * * * * return true;
* * * * }


The problem I have is that the usernames can become stuffed and could miss user 1 and duplicate user 9 and could be random when the system has a little bit of load??? This is the output I get from debug view:

Go_E4 [ENTER]
Go_E4 User = aa1
Go_E4 User = aa10
Go_E4 User = aa2
Go_E4 User = aa3
Go_E4 User = aa4
Go_E4 User = aa5
Go_E4 User = aa6
Go_E4 User = aa7
Go_E4 User = aa8
Go_E4 User = aa9
Go_E4 [EXIT]

Go_E4 Username = aa4
Go_E4 Username = aa10
Go_E4 Username = aa4
Go_E4 Username = aa9
Go_E4 Username = aa3
Go_E4 Username = aa9
Go_E4 Username = aa9
Go_E4 Username = aa9
Go_E4 Username = aa9
Go_E4 Username = aa9