• Welcome to Valhalla Legends Archive.
 

.NET Bug?

Started by TehUser, June 14, 2005, 06:16 PM

Previous topic - Next topic

TehUser

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


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


Throws an exception (TargetParameterCountException) back at:

Application.Run(new frmServerMain());

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()

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.

MyndFyre

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:

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." });
}


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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

TehUser

I guess this part was relevant.


private void AddStatus(params Object[] saElements)
{
if (rtbStatus.InvokeRequired)
rtbStatus.BeginInvoke(new AddTextCallback(AddToStatus), saElements);
else
AddToStatus(saElements);
}

MyndFyre

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?
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

TehUser

#4
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.