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 at System.Windows.Forms.Control.WmPaint(Message&m)\r\n at System.Windows.Forms.Control.WndProc(Message& m)\r\n 在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)\r\n 在 System.Windows.Forms.ContainerControl.WndProc(Message& m)\r\n m)\r\n 在 System.Windows.Forms.Form.WndProc(Message& m)\r\n 在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)\r\n 在 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 at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)\r\n 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.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 位于 D:\Documents and Settings\Dima\My Documents\Visual Studio 中的 WindowsFormsApplication1.Program.Main() 2008\Projects\SpaceInvaders\SpaceInvaders\Program.cs:第 18 行\r\n 位于 System.AppDomain._nExecuteAssembly(程序集程序集,String[] args)\r\n 位于 System.AppDomain.ExecuteAssembly(字符串 assemblyFile,证据 assemblySecurity, String[] args)\r\n 位于Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()\r\n 在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态)\r\n 在 System.Threading.ExecutionContext.Run(ExecutionContextexecutionContext、ContextCallback 回调、对象状态)\ r\n 在 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.
}
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.
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您应该使用 Step Into (F11) 进行调试,以便您可以进入 SpaceInvaders 表单并查看是否为方法提供了任何 null 或其他无效参数。
此行可能不会立即引发异常:
但某些初始化函数可能会产生问题。
查看堆栈跟踪和代码后进行编辑:
请参阅
//some code
部分。检查g.dispose()
之前的代码是否不会触发在 Draw 方法之后执行的任何事件处理程序。如果是这样,那么该事件处理程序应该是需要图形对象的事件处理程序,但您已经处理了它。因此图形参数为空。如果您需要任何进一步的帮助,请输入//some code
部分。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:
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 beforeg.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.简短回答:
只是不要在 Draw() 方法中对 Graphics 对象调用 Dispose() 。 Graphics 对象包含本机绘图表面的句柄。如果你处置它,你的表单将无法在屏幕上绘制。
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.
您不应该处置其他人的
Graphics
对象。仅在确定没有其他人会使用它时才处置您拥有的对象。
如果您从其他人那里得到一次性物品,您应该假设(除非他们另有说明)他们会在您用完后处理掉它。您不应该自己处置它,也不应该保存它供以后使用(因为他们可能已经处置了它)
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)