Updating Progress Bar from Thread in C#
<pre><code>    public class Consumer
    {
        Queue&lt;int&gt; queue;
        Object lockObject;
        public DateTime secondTime;

        public Consumer(Queue&lt;int&gt; queue, Object lockObject)
        {
            this.queue = queue;
            this.lockObject = lockObject;
        }

        public void consume(string filepath)
        {
            int item = 0;
            int TotalConsumedElements = 0;

            FileStream fileStream = new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
            TextWriter write = new StreamWriter(fileStream);
            int pre = 0;

            DateTime firstTime = DateTime.Now;

            while (true)
            {
                lock (lockObject)
                {
                    if (queue.Count == 0)
                    {
                        //Thread.Sleep(1000);
                        Monitor.PulseAll(lockObject);
                        continue;
                    }

                    item = queue.Dequeue();
                    if (item == 0)
                    {
                        break;
                    }

                    string TempString = item + " " + Environment.NewLine;
                    TotalConsumedElements++;

                    write.Write("“" + item + "”,");
                    write.Flush();
                    pre = item;

                    Console.WriteLine("Consuming: "+item);
                    Console.WriteLine("TotalConsumedElements: "+ TotalConsumedElements);
                }

            }

            DateTime secondTime = DateTime.Now;
            TimeSpan duration = secondTime - firstTime;
            string time = duration.TotalMilliseconds.ToString();
            Console.WriteLine("Time in Milliseconds : " + time);
            write.WriteLine();
            write.WriteLine("Total time taken: " + time+" ms");
            write.WriteLine("The largest prime number generated: " + pre);
            write.Flush();
            write.Close();

            MessageBox.Show("\nTotal time taken: " + time+
            " ms\nThe largest prime number generated: " + pre, "Finish",
            MessageBoxButtons.OK, MessageBoxIcon.Information);

        }
    }




    public class Producer 
    {
        Queue&lt;int&gt; queue;
        Object lockObject;

        public int ProgressPercent = 0;
        int TotalProducedElements = 0;
        public bool check1 = false;

        public Producer(Queue&lt;int&gt; queue, Object lockObject)
        {
            this.queue = queue;
            this.lockObject = lockObject;
        }

        private bool IsPrime(int num)
        {
            if (num == 0)
                return true;
            num = Math.Abs(num);
            for (int i = 2; i &lt;= Math.Sqrt(num); i++)
                if (num % i == 0)
                    return false;
            return true;
        }

        public void produce(int target)
        {

            try
            {

                int seq = 0;
                while (seq++ &lt; target)
                {

                    lock (lockObject)
                    {
                        int item = seq;
                        if (IsPrime(item))
                        {
                            queue.Enqueue(item);
                            Console.WriteLine("Producing: " + item);

                        }
                        TotalProducedElements++;

                        ProgressPercent = seq;

                        Console.WriteLine("ProgressPercent: " + ProgressPercent);
                        if (queue.Count == 0)
                        {
                            Monitor.PulseAll(lockObject);
                        }
                    }
                }
                queue.Enqueue(0);
              }
            catch (Exception e)
            {
                MessageBox.Show("Still working", "Warnning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }
    }
}

 private void Start_Click(object sender, EventArgs e)
    {           

                    Object lockObj = new object(); 

                    Queue&lt;int&gt; queue = new Queue&lt;int&gt;();  

                    Producer p = new Producer(queue, lockObj);

                    Consumer c = new Consumer(queue, lockObj);



                    int target = int.Parse(TargetText.Text);
                    string path = Path.Text;

                    Thread Thread1 = new Thread(() =&gt; p.produce(target));
                    Thread Thread2 = new Thread(()=&gt;c.consume(path));

                    Thread1.Start();



                    Thread2.Start();
                   progressBar1.Maximum = target;

                  while(true)
                       {
                         if(p.ProgressPercent==0)
                            {
                               Thread.sleep(1000);
                            }
                            else
                             {
                               progressBar1.Value=p.ProgressPercent;
                             }

                        }

                 }
</code></pre>

<p>I have two classes working on same queue. one to produce a set of integers and the 
second is to consume that integers from queue.
and during all this i want to update my progress bar by that percentage. 
So how to update progress bar from consumer without blocking for my GUI?</p>

<p>Note I have used progressbar.Invoke and deletegate but didn't work.</p>
