C# Programming
07-02-2009, 08:50 PM
Hi all,
I am trying to invoke the forms main thread from a worker thred, but I am desperately trying to avoid Control.InvokeRequired. To better explain, here is the code:
Simple class: (MyClass.cs)
public class MyClass
{
public delegate void LiveStatsLoginRequest(object sender, LiveStatsLoginEventArgs e);
public event LiveStatsLoginRequest OnLSLoginRequest;
public MyClass()
{
}
public void OpenConnection(string postData)
{
var args = new object[] {postData, Operation.Check, LiveStatsAction.Authenticate};
var t = new Thread(dowork);
t.Start(args);
}
private void dowork(object args)
{
DoSomethingThatTakesALongTime();
OnLSLoginRequest.Invoke(this, new EventArgs());
}
}
Now the main form (MainForm.cs)
public partial class MainForm : Form
{
private MyClass myClass;
public MainForm()
{
InitializeComponent();
myClass = new MyClass();
MyClass.OnLSLoginRequest += LiveStats_OnLSLoginRequest;
}
private void Form_Load(object sender, EventArgs e)
{
myClass.OpenConnection(new object[]{"Something"};
}
private void LiveStats_OnLSLoginRequest(object sender, LiveStatsLoginEventArgs e)
{
this.TextBox1.Text = "Logged in!";
}
}
The problem is that I get an error due to cross threading. I know that I can fix this my doing the following:
MainForm.cs
private void LiveStats_OnLSLoginRequest(object sender, LiveStatsLoginEventArgs e)
{
if (InvokeRequired)
{
//do invoke
}
else
{
this.TextBox1.Text = "Logged in!";
}
}
But I think this is messy and I would like to do the invoking through the class itself. I will eventually be releasing this code open source and don't want the user to have to worry/need to invoke the main form.
How can I solve this?
many thanks
(Sorry for the crude code!)
Bluetooth Marketing (http://www.blue365.eu)|Bluetooth Advertising (http://www.blue365.eu)
I am trying to invoke the forms main thread from a worker thred, but I am desperately trying to avoid Control.InvokeRequired. To better explain, here is the code:
Simple class: (MyClass.cs)
public class MyClass
{
public delegate void LiveStatsLoginRequest(object sender, LiveStatsLoginEventArgs e);
public event LiveStatsLoginRequest OnLSLoginRequest;
public MyClass()
{
}
public void OpenConnection(string postData)
{
var args = new object[] {postData, Operation.Check, LiveStatsAction.Authenticate};
var t = new Thread(dowork);
t.Start(args);
}
private void dowork(object args)
{
DoSomethingThatTakesALongTime();
OnLSLoginRequest.Invoke(this, new EventArgs());
}
}
Now the main form (MainForm.cs)
public partial class MainForm : Form
{
private MyClass myClass;
public MainForm()
{
InitializeComponent();
myClass = new MyClass();
MyClass.OnLSLoginRequest += LiveStats_OnLSLoginRequest;
}
private void Form_Load(object sender, EventArgs e)
{
myClass.OpenConnection(new object[]{"Something"};
}
private void LiveStats_OnLSLoginRequest(object sender, LiveStatsLoginEventArgs e)
{
this.TextBox1.Text = "Logged in!";
}
}
The problem is that I get an error due to cross threading. I know that I can fix this my doing the following:
MainForm.cs
private void LiveStats_OnLSLoginRequest(object sender, LiveStatsLoginEventArgs e)
{
if (InvokeRequired)
{
//do invoke
}
else
{
this.TextBox1.Text = "Logged in!";
}
}
But I think this is messy and I would like to do the invoking through the class itself. I will eventually be releasing this code open source and don't want the user to have to worry/need to invoke the main form.
How can I solve this?
many thanks
(Sorry for the crude code!)
Bluetooth Marketing (http://www.blue365.eu)|Bluetooth Advertising (http://www.blue365.eu)