title: Background Worker Thread Problem
body: <p>I am using background worker in my Application</p>
  
  <p>my code for this</p>
  
  <pre><code> void CreateThreadForEachServer()
   {
      DataAccess oDA = new DataAccess();
  
      List&lt;Server&gt; allServerData = oDA.GetAllServers();
  
      foreach (Server serverData in allServerData)
      {
          backgroundWorker = new BackgroundWorker();
  
          backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
  
          backgroundWorker.RunWorkerAsync(serverData);
  
      }
  }
  
  void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
  {
      Server server = (Server)e.Argument;
      CreateSnapshotForEachServer(server);
  }
  
  void CreateSnapshotForEachServer(Server server)
  {
      DataAccess oDA = new DataAccess();
      MsmqMessageFormat message = new MsmqMessageFormat();
  
      try
      {
          message = new Queue().ReadMessageFromMSMQ(server.ServerName);
      }
      catch
      {
      }
   }
  </code></pre>
  
  <p>My problem is when I am  calling this method</p>
  
  <pre><code> try
   {
       message = new Queue().ReadMessageFromMSMQ(server.ServerName);
   }
   catch
   {
   }
  </code></pre>
  
  <p>in a background worker then i can not able to call this method just read a message from MSMQ</p>
  
  <p>But when i can't use background worker just call this method in simple thread like this</p>
  
  <pre><code>void CreateThreadForEachServer()
  {
      DataAccess oDA = new DataAccess();
  
      List&lt;Server&gt; allServerData = oDA.GetAllServers();
  
      foreach (Server serverData in allServerData)
      {
          ThreadStart t = delegate { CreateSnapshotForEachServer(serverData); };
          Thread td = new Thread(t);                
          td.Priority = ThreadPriority.Highest;
          td.Start();
      }
  }
  </code></pre>
  
  <p>then this method call properly  </p>
  
  <pre><code>try
  {
      message = new Queue().ReadMessageFromMSMQ(server.ServerName);
  }
  catch
  {
  }
  </code></pre>
  
  <p>what is the problem with background worker my Queue class is like this</p>
  
  <pre><code> class Queue
   {
       public MsmqMessageFormat ReadMessageFromMSMQ(string queueName)
       {
           MessageQueue messageQueue = null;
  
           messageQueue = new MessageQueue(@".\Private$\" + queueName);
  
           messageQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(MsmqMessageFormat) });
  
           System.Messaging.Message msg = null;
           System.Messaging.Message[] allMessages = messageQueue.GetAllMessages();
  
           if (allMessages.Length &gt; 0)
           {
               msg = messageQueue.Receive();
  
               MsmqMessageFormat readMessage = (MsmqMessageFormat)(msg.Body);
  
               return readMessage;
           }
           else
           {
               return null;
           }
       }
   }
  </code></pre>
  
  <p>and MsmqMessageFormat  class is like this</p>
  
  <pre><code>[Serializable]
  public class MsmqMessageFormat
  {      
      public Zvol Zvol { get; set;}
      public List&lt;PolicyInterval&gt; listPolicyIntervalInfo = new List&lt;PolicyInterval&gt;(); 
  }
  </code></pre>