无法在 C# 中使用线程更新进度条 [英] unable to update progress bar with threading in C#

查看:61
本文介绍了无法在 C# 中使用线程更新进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    private void button1_Click(object sender, EventArgs e)
    {
        PROGRESS_BAR.Minimum = 0;
        PROGRESS_BAR.Maximum = 100;
        PROGRESS_BAR.Value = 0;

        for (int i = 0; i < 100; i++)
        {
            Thread t = new Thread(new ThreadStart(updateProgressBar));
            t.IsBackground = true;
            t.Start();
        }

    }

    private void updateProgressBar()
    {   
          PROGRESS_BAR.PerformStep();
          Thread.Sleep(4000);
    }

我总是收到这个错误:跨线程操作无效:控制 '' 从创建它的线程以外的线程访问.

I always get this error: Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.

我尝试在 google 中搜索解决方案,但不幸的是,所有这些都不适合我.有谁知道如何解决这个问题?提前致谢..

I tried to search in google for solutions and unfortunately all of them didn't work for me. does any one know how to solve this? thanks in advance..

推荐答案

您无法与来自非 UI 线程的 UI 元素进行交互.你需要使用像

You cannot interact with UI elements from non-UI thread. You need to use code like

this.BeginInvoke((Action)(() => PROGRESS_BAR.PerformStep()));

这篇关于无法在 C# 中使用线程更新进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆