Hi,
another 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.InvalidO perationExcepti on: Cross-thread operation not valid:
Control 'DebugConsole' accessed from a thread other than the thread it
was created on.
at System.Windows. Forms.Control.g et_Handle()
at System.Windows. Forms.Control.S endMessage(Int3 2 msg, Int32
wparam, String lparam)
at System.Windows. Forms.ListBox.N ativeAdd(Object item)
at System.Windows. Forms.ListBox.O bjectCollection .AddInternal(Ob ject
item)
at System.Windows. Forms.ListBox.O bjectCollection .Add(Object item)
at UsenetProject.M ainForm.Debug_C onsole_Add_Item (String entry) in C:
\Users\Jack\Doc uments\Misc Stuff\My Programs\Usenet Project
\UsenetProject\ UsenetProject\F orm1.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 UpdateDebugCons oleDelegate(str ing entry);
public void Debug_Console_A dd_Item(string entry)
{
if (this.DebugCons ole.InvokeRequi red)
{
UpdateDebugCons oleDelegate theDelegate = new
UpdateDebugCons oleDelegate(thi s.Debug_Console _Add_Item);
this.Invoke(the Delegate, new object[] { entry });
}
try
{
DebugConsole.It ems.Add(entry);
}
catch(Exception e)
{
MessageBox.Show (e.ToString());
}
}
Thanks very much for any help,
Jack
>From the code below, I have this method/delegate which is called from
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.InvalidO perationExcepti on: Cross-thread operation not valid:
Control 'DebugConsole' accessed from a thread other than the thread it
was created on.
at System.Windows. Forms.Control.g et_Handle()
at System.Windows. Forms.Control.S endMessage(Int3 2 msg, Int32
wparam, String lparam)
at System.Windows. Forms.ListBox.N ativeAdd(Object item)
at System.Windows. Forms.ListBox.O bjectCollection .AddInternal(Ob ject
item)
at System.Windows. Forms.ListBox.O bjectCollection .Add(Object item)
at UsenetProject.M ainForm.Debug_C onsole_Add_Item (String entry) in C:
\Users\Jack\Doc uments\Misc Stuff\My Programs\Usenet Project
\UsenetProject\ UsenetProject\F orm1.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 UpdateDebugCons oleDelegate(str ing entry);
public void Debug_Console_A dd_Item(string entry)
{
if (this.DebugCons ole.InvokeRequi red)
{
UpdateDebugCons oleDelegate theDelegate = new
UpdateDebugCons oleDelegate(thi s.Debug_Console _Add_Item);
this.Invoke(the Delegate, new object[] { entry });
}
try
{
DebugConsole.It ems.Add(entry);
}
catch(Exception e)
{
MessageBox.Show (e.ToString());
}
}
Thanks very much for any help,
Jack
Comment