So here I am back again with my tail between my legs.
I've spent the whole day trying to update my form lable during runtime.
I got there in the end with what I think is threadsafe, then only to spend a few more hours trying do it from outside the form class and failing :|
Here is some short reproducer code to illustrate my goal in the hope someone might be able to assist.
namespace daniwebexample
{
public partial class Form1 : Form
{
Thread NewThread;
public delegate void UpdateLabel(string text);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
NewThread = new Thread(new ThreadStart(MyNewThread));
NewThread.Start();
}
public void _UpdateLabel(string text)
{
if (label1.InvokeRequired)
{
UpdateLabel updt = new UpdateLabel(_UpdateLabel);
Invoke(updt, new object[] { text });
}
else
{
label1.Text = text;
label1.Refresh();
}
}
public void MyNewThread()
{
_UpdateLabel("New Text");
}
}
public class OtherClass
{
public void MyNewThread()
{
Form1._UpdateLabel("New Text"); //I thought I could use delegate from here.
}
}
}
I am having a torrid time, I've tried creating a new instance of form1 and calling delegate with that, but I need the current instance. And as I understand it, I cannot have a static form, and even if I could I'd still need an instance reference, and I cannot get that reference from Application.Run(new Form1); in the programs Main()
Basically, I've just run out of ideas to try, and here I am wringing my preverbial cap.