Argument Exception in the main method (winforms)

喜夏-厌秋 提交于 2019-12-13 05:40:56

问题


     static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new SpaceInvaders());// i get an error here when i start the form application.. it says Argument Exception. Parameter is not valid
    }

My main Form looks like that:

    public SpaceInvaders()
    {
        InitializeComponent();

    }

    public void SpaceInvaders_Load(object sender, EventArgs e)
    {
}

here is stack trace

" at System.Drawing.Graphics.GetHdc()\r\n at System.Drawing.BufferedGraphics.RenderInternal(HandleRef refTargetDC, BufferedGraphics buffer)\r\n at System.Drawing.BufferedGraphics.Render()\r\n at System.Windows.Forms.Control.WmPaint(Message& m)\r\n at System.Windows.Forms.Control.WndProc(Message& m)\r\n at System.Windows.Forms.ScrollableControl.WndProc(Message& m)\r\n at System.Windows.Forms.ContainerControl.WndProc(Message& m)\r\n at System.Windows.Forms.Form.WndProc(Message& m)\r\n at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)\r\n at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)\r\n at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)\r\n at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)\r\n at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)\r\n at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)\r\n at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)\r\n at System.Windows.Forms.Application.Run(Form mainForm)\r\n at WindowsFormsApplication1.Program.Main() in D:\Documents and Settings\Dima\My Documents\Visual Studio 2008\Projects\SpaceInvaders\SpaceInvaders\Program.cs:line 18\r\n at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)\r\n at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)\r\n at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()\r\n at System.Threading.ThreadHelper.ThreadStart_Context(Object state)\r\n at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n at System.Threading.ThreadHelper.ThreadStart()"

"Parameter is not valid."

i think i found the problem:

  public void Draw(Graphics  g,int animationCell)
    {
     // some code
        g.dispose()//that method threw the exception can someone tell me why? i thought you do need to dispose your graphics object at the end when you finish using it.
       }

回答1:


I think you should Debug using Step Into (F11) so that you can go inside the SpaceInvaders form and see if there is any null or otherwise invalid argument provided to methods.

The exception may not be thrown immediately by this line:

Application.Run(new SpaceInvaders());

But some initialization function may be creating problems.

Edit after seeing stack trace and code:

Pls see the //some code section. Check if that code before g.dispose() doesn't fire any event handlers which are executed after Draw method. If so then that event handler should be the one which requires the graphic object, but u already dispose it. Hence the graphic parameter is null. Please put that //some code part if you need any further assistance.




回答2:


Short answer:

Just don't call Dispose() on the Graphics-object in the Draw()-methode. The Graphics-object contains the handle to the native drawing surface. If you dispose it, your Form can't be painted on the screen.




回答3:


You should not dispose someone else's Graphics object.

Only dispose objects that you own, and only when you're sure that no-one else will be using it.

If you get a disposable object from someone else, you should assume (unless they say otherwise) that they will dispose it when you finish. You should not dispose it yourself, and you should not save it for later (since they may have disposed it)



来源:https://stackoverflow.com/questions/5664813/argument-exception-in-the-main-method-winforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!