title: How to use Threadpool.QueueUserWorkItem in a windows service?
body: <p>I have a windows service where I am using Threadpool.QueueUserWorkItem. The service connects to multiple client databases, grabs data, converts to XLS and send files to the corresponding FTP.</p>
  
  <p><strong>I have 3 questions regarding the code below:</strong></p>
  
  <ol>
  <li>Am I using the Threadpool.QueueUserWorkItem correctly?</li>
  <li>Do I need to use Lock anywhere in the code to avoid issues? If yes, where and to what object.</li>
  <li>Is there anything that is not correct in the code? If yes, what and how to deal with it?</li>
  </ol>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>private static System.Timers.Timer aTimer = new System.Timers.Timer(50000);
  
  public void OnStart(string[] args)
          {
              CLE.WriteToEventLog("Service Started");
              try
              {
                  aTimer.Elapsed += new ElapsedEventHandler(PerformTimerOperation);
                  aTimer.Enabled = true;
              }
              catch (Exception ex)
              {
                  CLE.WriteToEventLog("Error Starting Service: " + ex.Message);
              }
          }
  
  private void PerformTimerOperation(object source, ElapsedEventArgs e)
          {
              CLE.WriteToEventLog("Timer Operation Started");
                  Clients objClient = new Clients();
                  List&lt;Clients&gt; objClientList = Clients.GetClientList();
  
                  foreach (var list in objClientList)
                  {
                      ThreadPool.QueueUserWorkItem(new WaitCallback(SendFilesToClient), list);
                  }                
          }
  
  private void SendFilesToClient(Object stateInfo)
          {
              CLE.WriteToEventLog("Send Files To Client Started");
              Clients oClient = (Clients)stateInfo;
              CLE.WriteToEventLog("Start Proecessing Client: " + oClient.ClientName + ", ClientId: " + oClient.ClientId);
  
              connectionString = App.Database.PrimaryConnectionString(oClient.ClientId);
  
              string reports = oClient.Reports;
              string[] values = reports.Split(',').Select(sValue =&gt; sValue.Trim()).ToArray();
  
              foreach (string item in values)
              {
      //Send data to FTP based on cliend id
              }
              // At this point all reports are being sent to the FTP. We will update the database with LastExecutionDateTime + 1 hour. This will be used as DateFrom param for all reports for the next execution.
          }
  </code></pre>
  
  <p>The service works fine and I get appropriate results, but I need to make sure I am doing it right and don't run in to issues later.</p>