0

Good morning guys, I'm trying to write some application in C#, where I like to update the UI (A progress bar in this case) from another thread and class.

But I just can't get it to work, I googled and search around but I'm afraid I just don't get it. I have a windows form application, I start a thread when I click a button, and somewhere in this thread I would like to update my UI.

I either get: An object reference is required for the non-static field, method, or property or something in the direction of the object being created by a different thread. (At the location where I try to call Form1.UpdateProgressBar(value); in fileReader).

I have no experience in object orienting programming, I usually stick with C. If anyone could tell me the right way to do this, I would be very happy.

Edit_1: Alright.. Combinations of errors, the answer so far might have helped if I didn't have he static issue. And fixing the static issue by making the entire class static creates another X amount of errors on its own, including:

Static classes cannot have instance constructors

namespace TestCode
{

  public partial class Form1 : Form
  {
    static fileReader SourceReader;
    public Thread SearchThread { get; set; }

    public Form1()
    {
        InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)
    {
        folderBrowserDialog1.ShowDialog();
        Console.WriteLine(folderBrowserDialog1.SelectedPath);

        this.SearchThread = new Thread(new ThreadStart(this.ThreadProcSafe));
        this.SearchThread.Start();
    }


    public void UpdateProgressBar( int value)
    {

       progressBar1.Value =value;

    }

    private void ThreadProcSafe()
    {
        SourceReader = new fileReader(folderBrowserDialog1.SelectedPath);
    }
  }
}

Class 2:

     namespace TestCode
     {
         class fileReader
         {

             public fileReader(String path)
             {
                int value = 20;
                /*Do some stuff*/
                  Form1.UpdateProgressBar(value);

             }

          }

     }
koldewb
  • 452
  • 1
  • 5
  • 18
  • Your fileReader class is not static that's why the compiler says you need a reference to that class before calling it's constructor. – M. Adeel Khalid Apr 11 '17 at 05:57
  • 1
    This is a bad design. `fileReader` should not depend on `Forms`. Look into the usage of `Task` and `IProgress` for better progress notification. Example: https://blogs.msdn.microsoft.com/dotnet/2012/06/06/async-in-4-5-enabling-progress-and-cancellation-in-async-apis/ – Zein Makki Apr 11 '17 at 06:00

2 Answers2

0

Check if an invoke is required and inf needed then use the controls Invoke function:

public void UpdateProgressBar( int value)
{
   if(progressBar1.InvokeRequired){
       progressBar1.Invoke(new MethodInvoker(() => progressBar1.Value=value));
   }else{
       progressBar1.Value =value;
   }
}
-1

You can use a MethodInvoker when trying to modify the UI from another class like this:

ProgressBar progressBar = Form1.progressBar1;
MethodInvoker action = () => progressBar.Value = 80;
progressBar.BeginInvoke(action);

while you can use this when working in a different Thread ( a Task for example ):

progressBar1.Invoke((Action)(() => progressBar1.Value=50))

But consider the comments on your post. It's not needed to be dependent on Forms in fileReader

Side note: I don't know how you didn't find your problem on here:

how to update a windows form GUI from another class?

How to update the GUI from another thread in C#?

Community
  • 1
  • 1
Nico
  • 1,727
  • 1
  • 24
  • 42