Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8757149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:14:08+00:00 2026-06-13T14:14:08+00:00

public class Consumer { Queue<int> queue; Object lockObject; public Consumer(Queue<int> queue, Object lockObject) {

  • 0
    public class Consumer
    {
        Queue<int> queue;
        Object lockObject;

        public Consumer(Queue<int> queue, Object lockObject)
        {
            this.queue = queue;
            this.lockObject = lockObject;
        }

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


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

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

                   //do some staff
                }

            }

        }
    }




    public class Producer 
    {
        Queue<int> queue;
        Object lockObject;

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

        public Producer(Queue<int> 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 <= Math.Sqrt(num); i++)
                if (num % i == 0)
                    return false;
            return true;
        }

        public void produce(int target)
        { 
            try
            {
                int seq = 0;
                while (seq++ < target)
                {

                    lock (lockObject)
                    {
                        int item = seq;
                        if (IsPrime(item))
                        {
                            queue.Enqueue(item);

                        }
                        TotalProducedElements++;

                        ProgressPercent = seq;

                        if (queue.Count == 0)
                        {
                            Monitor.PulseAll(lockObject);
                        }
                    }
                }
                queue.Enqueue(0);
              }
            catch (Exception e)
            {
            }

        }
    }
}

 private void Start_Click(object sender, EventArgs e)
    {           

                    Object lockObj = new object(); 

                    Queue<int> queue = new Queue<int>();  

                    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(() => p.produce(target));
                    Thread Thread2 = new Thread(()=>c.consume(path));

                    Thread1.Start();



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

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

                        }

                 }

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?

Note I have used progressbar.Invoke and delegate but didn’t work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:14:09+00:00Added an answer on June 13, 2026 at 2:14 pm

    You need two things. First is obviously a thread, the other is invoker.

    Invokers will let you change window’s controls from inside of a thread. Here’s the example:

    Invoke((MethodInvoker)delegate
    {
        Label1.text = "asd";
    }
    

    Threads are ran like this:

    some_thread = new Thread
    (delegate()
    {
    
        {
            //some_thread code
        }
    });
    some_thread.Start();
    

    You also need to add using System.Threading; at the beginning of the file.

    So, the final code should look like this:

    some_thread = new Thread
    (delegate()
    {
    
        {
            for(;;)
            {
                    Invoke((MethodInvoker)delegate
                    {
                                    //update the progress bar here
                    }
            }
        }
    });
    some_thread.Start();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this scenario: class MyClass { Producer p; Consumer c; public static void
public class Test { public static void main(String[] args) { DemoAbstractClass abstractClass = new
public class A { private A(int param1, String param2) {} public static A createFromCursor(Cursor
public class LinkedList { Object contents; LinkedList next = null; public boolean equals(Object item)
public class MyClass { public string MyProperty{ get; set; } Now, I would like
public class WeeklyInspection : Activity { WebView view = (WebView) FindViewById(Resource.Id.inspectionWV); view.Settings.JavaScriptEnabled = true;
public class HttpPostTask extends AsyncTask<Void, Integer, Void> { TextView txtStatus = (TextView)findViewById(R.id.txtStatus); @Override protected
I have this producer / consumer code : MAIN : static void Main() {
public class tryAnimActivity extends Activity { /** * The thread to process splash screen
public class Test1<Type> { public Type getCompositeMessage(Type... strings) { Type val = (Type) ;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.