0
votes
     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.
       }
3
Please provide more detail. What's the stack trace?SLaks
Do you really not have anything else in the constructor?jgauffin
maybe code inside InitializeComponent() is giving some problemSubhash Lama
i removed a line that said .. g.dispose (graphics dispose) and i stopped getting that error.. the dispose() method..kept raising that problemDmitry Makovetskiyd
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.Marshal

3 Answers

2
votes

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.

1
votes

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.

1
votes

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)