>From the code below, I have this method/delegate which is called fromanother thread, Debug_Console is a listbox control on my main form, i
created the delegate to avoid the cross threading error. Now the
strange thing is, when i call the method from my other thread, the
entry string is added to the listbox, but it also throws an exception
after the string is added. I cant work out why.
The exception is:
=========================
System.InvalidOperationException: Cross-thread operation not valid:
Control 'DebugConsole' accessed from a thread other than the thread it
was created on.
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.SendMessage(Int32 msg, Int32
wparam, String lparam)
at System.Windows.Forms.ListBox.NativeAdd(Object item)
at System.Windows.Forms.ListBox.ObjectCollection.AddI nternal(Object
item)
at System.Windows.Forms.ListBox.ObjectCollection.Add( Object item)
at UsenetProject.MainForm.Debug_Console_Add_Item(Stri ng entry) in C:
\Users\Jack\Documents\Misc Stuff\My Programs\UsenetProject
\UsenetProject\UsenetProject\Form1.cs:line 46
=========================
I thought the whole point of invoking the new delgate is to avoid the
cross threading problem, what am i doing wrong?
My code:
public delegate void UpdateDebugConsoleDelegate(string entry);
public void Debug_Console_Add_Item(string entry)
{
if (this.DebugConsole.InvokeRequired)
{
UpdateDebugConsoleDelegate theDelegate = new
UpdateDebugConsoleDelegate(this.Debug_Console_Add_ Item);
this.Invoke(theDelegate, new object[] { entry });
}
try
{
DebugConsole.Items.Add(entry);
}
catch(Exception e)
{
MessageBox.Show(e.ToString());
}
}
Thanks very much for any help,
Jack