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);
}
}
}