none
C# - Microsoft Word VSTO - Update ProgressBar in Form RRS feed

  • Question

  • I have posted the same questions to StackOverflow and didn't get any response. Hopefully I can get some help here.https://stackoverflow.com/questions/52393138/c-sharp-microsoft-word-vsto-update-progressbar-in-form

    I am trying to update the ProgressBar for while doing some lengthy document manipulation in a Form invoked from the Ribbon button click event. But I was getting exception "Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on."

    The Form works as expected when invoked from a standalone application. Below is a mark up of a sample Form

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();           
        }
    
        private async void Test_Click(object sender, EventArgs e)
        {
            progressBar.Minimum = 0;
            progressBar.Maximum = 100;
            progressBar.Step = 1;
    
            var progressHandler = new Progress<int>(value =>
            {
                //throw the error: "Cross-thread operation not valid: Control 'progressBar' accessed from a thread other than the thread it was created on."
                progressBar.Value = value;
            });
    
            var progress = progressHandler as IProgress<int>;
            await Task.Run(() =>
            {
                for (int i = 0; i != 100; ++i)
                {
                    if (progress != null)
                        progress.Report(i);
                    Thread.Sleep(100);
                }
            });
            progressBar.Value = progressBar.Maximum;
        }    
    }

    It works fine when called from a standalone app.

      static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

    But I am getting the exception "Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on." when I called it from the Ribbon button click event though. See the code below.

       private void btnRibbon_Click(object sender, RibbonControlEventArgs e)
        {
            var bf = new Form1();            
            bf.Show();            
        }

    Have been playing with it for a couple days now and couldn't figure what the problem was.

    Thanks, Brian


    tofuTnT

    Thursday, September 20, 2018 12:30 AM

Answers

  • Hi Brain,

    When you try to update a win form from a separate thread, you will get the following error:

     "Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on."

    Getting the error in this line:

    //throw the error: "Cross-thread operation not valid: Control 'progressBar' accessed from a thread other than the thread it was created on."
                    ProgressBar.Value = i;

    So please try the code below:

    SetControlPropertyValue(progressBar, "value", i); //This is a thread safe method

    And then copy the following code to your project:

    delegate void SetControlValueCallback(Control oControl, string propName, object propValue);
    private void SetControlPropertyValue(Control oControl, string propName, object propValue)
    {
        if (oControl.InvokeRequired)
        {
          SetControlValueCallback d = new SetControlValueCallback(SetControlPropertyValue);
          oControl.Invoke(d, new object[] { oControl, propName, propValue });
        }
        else
          {
            Type t = oControl.GetType();
            PropertyInfo[] props = t.GetProperties();
            foreach (PropertyInfo p in props)
             {
               if (p.Name.ToUpper() == propName.ToUpper())
                {
                 p.SetValue(oControl, propValue, null);
                }
             }
          }
    }

    Hopefully it helps you. Please feel free to ask any questions.

    Best Regards,

    Yuki


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.





    Thursday, September 20, 2018 3:11 AM
    Moderator

All replies

  • Hi Brain,

    When you try to update a win form from a separate thread, you will get the following error:

     "Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on."

    Getting the error in this line:

    //throw the error: "Cross-thread operation not valid: Control 'progressBar' accessed from a thread other than the thread it was created on."
                    ProgressBar.Value = i;

    So please try the code below:

    SetControlPropertyValue(progressBar, "value", i); //This is a thread safe method

    And then copy the following code to your project:

    delegate void SetControlValueCallback(Control oControl, string propName, object propValue);
    private void SetControlPropertyValue(Control oControl, string propName, object propValue)
    {
        if (oControl.InvokeRequired)
        {
          SetControlValueCallback d = new SetControlValueCallback(SetControlPropertyValue);
          oControl.Invoke(d, new object[] { oControl, propName, propValue });
        }
        else
          {
            Type t = oControl.GetType();
            PropertyInfo[] props = t.GetProperties();
            foreach (PropertyInfo p in props)
             {
               if (p.Name.ToUpper() == propName.ToUpper())
                {
                 p.SetValue(oControl, propValue, null);
                }
             }
          }
    }

    Hopefully it helps you. Please feel free to ask any questions.

    Best Regards,

    Yuki


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.





    Thursday, September 20, 2018 3:11 AM
    Moderator
  • Thanks Yuki! That worked! Really appreciate it.

    tofuTnT

    Thursday, September 20, 2018 2:15 PM