so much shit hits fan when you try to update a label, progressbar or windows control from a thread.
Normally people would go through the trouble of declaring delegates, calling invoke with empty object arrays, shit like that which takes up roughly 6-7 lines of code.
//Declare a new delegate specific to updating the text of a label
delegate void UpdateLabelTextDelegate(Label lbl, string text);
//This method will invoke the delegate and pass our args along
void UpdateLabelText(Label lbl, string text)
{
UpdateLabelTextDelegate dlg = new UpdateLabelTextDelegate(UpdateLabelTextByDelegate);
//Invoke this method on the control's original that owns the label's handle
lbl.Invoke(dlg, new object[] { lbl, text });
}
//This method is invoked by our delegate and actually updates the label text
void UpdateLabelTextByDelegate(Label lbl, string text)
{
lbl.Text = text;
}
//Call the method now
UpdateLabelText(myLabel, "What a great post");
What a waste of time just to change a frikken label!To make matters worse, it turns your code to garbage and makes it utterly unreadable.
Luckily, Microsoft decided to stop pounding our balls against a wall and provide us with MethodInvoker.
pbarLoading.Invoke((MethodInvoker)delegateShort sweet and simple!
{
pbarLoading.Value++;
});
[ ref ]