main方法中的参数异常(winforms) [英] Argument Exception in the main method (winforms)

查看:212
本文介绍了main方法中的参数异常(winforms)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

     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
    }

我的主表单如下:

    public SpaceInvaders()
    {
        InitializeComponent();

    }

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

这是堆栈跟踪

"位于System.Drawing.Graphics.GetHdc()\ r \ n,位于System.Drawing.BufferedGraphics.RenderInternal(HandleRef refTargetDC,BufferedGraphics缓冲区)\ r \ n,位于System.Drawing.BufferedGraphics.Render()\ r \ n在System.Windows.Forms.Control.WmPaint(Message& m)\ r \ n在System.Windows.Forms.Control.WndProc(Message&m)\ r \ n在System.Windows.Forms.ScrollableControl.WndProc(在System.Windows.Forms.ContainerControl.WndProc(Message& m)\ r \ n在System.Windows.Forms.Form.WndProc(Message& m)\ r \ n在System.Windows System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)\ r \ n在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)\ r \ n在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)\ r \ n在System.Windows.Forms.Application.Application.ComponentManager.System.Windows.Forms .UnsafeNativeMethods.IMsoComponentM anager.FPushMessageLoop(Int32 dwComponentID,Int32原因,Int32 pvLoopData)\ r \ n在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32原因,ApplicationContext上下文)\ r \ n在System.Windows.Forms.Application.ThreadContext .RunMessageLoop(Int32原因,ApplicationContext上下文)\ r \ n在System.Windows.Forms.Application.Run(Form mainForm)\ r \ n在WindowsFormsApplication1.Program.Main()在D:\ Documents and Settings \ Dima \ My Documents \ Visual Studio 2008 \ Projects \ SpaceInvaders \ SpaceInvaders \ Program.cs:Line 18 \ r \ n在System.AppDomain._nExecuteAssembly(Assembly assembly,String [] args)\ r \ n在System.AppDomain.ExecuteAssembly(String assemblyFile) ,证据组装安全性,字符串[] args)\ r \ n在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()\ r \ n在System.Threading.ThreadHelper.ThreadStart_Context(Object state)\ r \ n在System.Threading. ExecutionContext.Run(ExecutionContext executeContext,ContextCallback回调,对象状态)\ r \ na t System.Threading.ThreadHelper.ThreadStart()"

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

参数无效."

我认为我发现了问题:

  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.
       }

推荐答案

我认为您应该使用Step Into(F11)进行调试,以便可以进入SpaceInvaders表单并查看是否提供了任何null或其他无效参数.方法.

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.

在看到堆栈跟踪和代码后进行

请参阅//some code部分.检查g.dispose()之前的代码是否不会触发在Draw方法之后执行的任何事件处理程序.如果是这样,则该事件处理程序应该是需要图形对象的事件处理程序,但是您已经将其处理.因此,图形参数为null.如果您需要任何其他帮助,请放下//some 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.

这篇关于main方法中的参数异常(winforms)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆