简体   繁体   English

在C#中从线程更新进度条

[英]Updating Progress Bar from Thread in C#

    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? 那么如何在不阻止我的GUI的情况下更新消费者的进度条?

Note I have used progressbar.Invoke and delegate but didn't work. 注意我使用了progressbar.Invokedelegate但是没有用。

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. Invokers将允许您从线程内部更改窗口的控件。 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; 您还需要using System.Threading;添加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();

You could use a BackgroundWorker (System.ComponentModel) to accomplish this easily. 您可以使用BackgroundWorker(System.ComponentModel)轻松完成此任务。 Take a look at: http://www.codeproject.com/Tips/83317/BackgroundWorker-and-ProgressBar-demo 看看: http//www.codeproject.com/Tips/83317/BackgroundWorker-and-ProgressBar-demo

Clear: 明确:

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

                    }

Replace: 更换:

ProgressPercent = seq;

to

UpdateProgres(seq);

Add: 加:

public void UpdateProgres(int _value)
    {
        if (InvokeRequired)
        {
            Invoke(new Action<int>(UpdateProgres), _value);
            return;
        }

        progressBar1.Value = _value;
    }

Summary your needed function; 总结您需要的功能;

public void UpdateProgres(int _value)
    {
        if (InvokeRequired)
        {
            Invoke(new Action<int>(UpdateProgres), _value);
            return;
        }

        progressBar1.Value = _value;
    }

Or need Lock the value. 或者需要锁定值。 For Example; 例如;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM