c#-进度条[Threading]跨线程操作无效:控制'progressBar'从其他线程(而不是在其上创建的线程)访问

Mah*_*esh 2 c#-3.0

我坚持上述问题。我有很多解决方案,但没有一个对我有用。请在这里找到我的代码

private void btnRunQuery_Click(object sender, EventArgs e)
    {
        try
        {

            Thread ProcessThread = new Thread(Process);
            ProcessThread.Start();

            Thread.CurrentThread.Join();
        }
        catch
        {
            Debug.WriteLine("Error in model creation");
            Console.WriteLine("Error in model creation");
        }
        finally
        {
            //dsModel = null;
        }
    }


private void Process()
    {

        using (var dataContext = new IControlerDataContext())
        {
            dataContext.EnlistTransaction();

            IItemPropertyRepository itemPropertyRepository = ObjectContainer.Resolve<IItemPropertyRepository>();
            IList<ItemProperty> itemPropertyCollection = itemPropertyRepository.LoadAll();
            totalCount = itemPropertyCollection.Count;
            currentCount = 0;

            foreach (var itemProperty in itemPropertyCollection)
            {
                try
                {
                    message = string.Empty;
                    currentCount++;
                    if (itemProperty.DeletedDate == null && (itemProperty.MetaItemProperty.ValueType == MetaItemPropertyValueType.MetaItemTableProperty || itemProperty.MetaItemProperty.ValueType == MetaItemPropertyValueType.MetaItemTableMultiSelectProperty))
                    {
                        //Property refresh issue in only applicable for table and multitable property.
                        //Need to filter the itemproperty for Table and multitable select property.
                        message = ProcessItemProperty(itemProperty);
                        //txtLogDetails.Text += message + Environment.NewLine;
                        //txtLogDetails.Refresh();
                        //txtLogDetails.ScrollToCaret();
                    }
                    //Log(message);
                    //progressBar.Value = (Int32)(currentCount * 100 / totalCount);
                    //progressBar.Refresh();
                    Invoke(new MyDelegate(ShowProgressBar), (Int32)(currentCount * 100 / totalCount));

                }
                catch (Exception ex)
                {
                    txtLogDetails.Text += "EXCEPTION ERROR : " + itemProperty.Id.ToString();
                    dataContext.RollBackTransaction();
                }
            }
            dataContext.CompleteTransaction();
        }
    }

 delegate void MyDelegate(int percentage);
    private void ShowProgressBar(int percentage)
    {
        progressBar.Value = percentage;
        progressBar.Refresh();
        //txtLogDetails.Text = message;
    }
Run Code Online (Sandbox Code Playgroud)

当执行“ Invoke(new MyDelegate(ShowProgressBar),(Int32)(currentCount * 100 / totalCount));” 这条线超出范围。它进入里面,再也没有回来。而且也没有遇到异常。

谁能帮我这个忙吗?

谢谢,Mahesh

小智 5

progressBar必须从创建控件的线程访问该控件。使用BeginInvoke

我会替换这条线...

Invoke(new MyDelegate(ShowProgressBar), (Int32)(currentCount * 100 / totalCount)); 
Run Code Online (Sandbox Code Playgroud)

...由这个...

this.progressBar.BeginInvoke( 
    (MethodInvoker)delegate() { 
        this.progressBar.Value =
           Convert.ToInt32(currentCount * 100 / totalCount); } );
Run Code Online (Sandbox Code Playgroud)

或者您可以替换这些行...

progressBar.Value = percentage; 
        progressBar.Refresh(); 
        //txtLogDetails.Text = message; 
Run Code Online (Sandbox Code Playgroud)

...按那些线...

this.progressBar.BeginInvoke( 
    (MethodInvoker)delegate() { 
    progressBar.Value = percentage; 
    progressBar.Refresh(); 
    //txtLogDetails.Text = message; 

    } );
Run Code Online (Sandbox Code Playgroud)