Valhalla Legends Forums Archive | .NET Platform | .NET Bug?

AuthorMessageTime
TehUser
I was working on my server and I ran across a strange error that I don't understand at all.

[code]
private void myServer_ClientConnected(Socket connectedSocket)
{
AddStatus( System.Drawing.Color.Cyan, "New client connected." );
}
[/code]

Throws an exception (TargetParameterCountException) back at:
[code]
Application.Run(new frmServerMain());
[/code]
in Program->Main()

[quote]
System.Reflection.TargetParameterCountException was unhandled
  Message="Parameter count mismatch."
  Source="mscorlib"
  StackTrace:
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)

       at System.Delegate.DynamicInvokeImpl(Object[] args)

       at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)

       at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)

       at System.Threading.ExecutionContext.runTryCode(Object userData)

       at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)

       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)

       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

       at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)

       at System.Windows.Forms.Control.InvokeMarshaledCallbacks()

       at System.Windows.Forms.Control.WndProc(Message& m)

       at System.Windows.Forms.TextBoxBase.WndProc(Message& m)

       at System.Windows.Forms.RichTextBox.WndProc(Message& m)

       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)

       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)

       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)

       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)

       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)

       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)

       at System.Windows.Forms.Application.Run(Form mainForm)

       at ProjectX.Program.Main() in e:\program.cs:line 16

       at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)

       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)

       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()

       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)

       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

       at System.Threading.ThreadHelper.ThreadStart()
[/quote]

I'm not even sure what more information I can provide that is relevant to error.  Any information appreciated.

P.S. The reason I know it's the AddStatus line is because with that line commented out, this Exception does not occur.
June 14, 2005, 11:16 PM
Myndfyr
My best guess is that you're attempting to do something to a GUI widget on a thread separate from the one that the GUI widget was created on.  For example, when you create a RichTextBox object, it's created on a thread (likely your main entry loop thread).  That thread -- actually, the one on which the RTB's Handle was created (via a call -- implicit or explicit -- to CreateHandle()) -- must be used to update that RTB.  This is true in native Win32 as well as native VB and all .NET languages.

If you are unsure whether or not you will have the thread when you're executing -- for example, when you could be calling such a function from a callback, as you are -- you want to use the Control.Invoke or Control.BeginInvoke method.

Example:
[code]
private delegate void AddStatusCallback(Color color, string message);
private void myServer_ClientConnected(Socket connectedSocket)
{
BeginInvoke(new AddStatusCallback(this.AddStatus), new object[] { Color.Cyan, "New client connected." });
}
[/code]

Let me know if you still get that error.  If so, you might try calling the BeginInvoke method of the control that you add status text to -- which might be a rich text box.
June 14, 2005, 11:27 PM
TehUser
I guess this part was relevant.

[code]
private void AddStatus(params Object[] saElements)
{
if (rtbStatus.InvokeRequired)
rtbStatus.BeginInvoke(new AddTextCallback(AddToStatus), saElements);
else
AddToStatus(saElements);
}
[/code]
June 14, 2005, 11:29 PM
Myndfyr
Is AddToStatus prototyped as params object[] or just object[]?

If it is prototyped as params object[], I'm not sure, but you might actually end up with an array that was 1 item long that contained an array object at index 0.  Can you set a breakpoint on the AddToStatus function, step through, and see where exactly the exception is thrown?
June 14, 2005, 11:34 PM
TehUser
Well, I tried...  I set a breakpoint at the AddStatus line and then Stepped into the function.  The exception was thrown immediately.  However, I think you're probably right about ending up with an array inside of an array, so I'll try modifying that.

Edit: I tried setting AddToStatus to accept a parameter of Object[] instead of params Object[].  I get the same exception thrown.
June 14, 2005, 11:41 PM

Search