主方法(winforms)中的参数Exception
本文关键字:参数 Exception 方法 winforms | 更新日期: 2023-09-27 17:50:23
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)
{
}
这里是堆栈跟踪
" at System.Drawing.Graphics.GetHdc()'r'n at System.Drawing.BufferedGraphics。RenderInternal(HandleRef refTargetDC, BufferedGraphics缓冲区)'r'n at System.Drawing.BufferedGraphics.Render()'r'n at system . windows . forms . control . wmppaint (Message&m)'r'n at system . windows . forms . control . windowproc (Message&m)'r'n at System.Windows.Forms.ScrollableControl.WndProc(Message&m)'r'n at system . windows . forms . containercontrol . windowproc (Message&m)'r'n at system . windows . forms . form . windowproc (Message&m)'r'n at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)'r'n at system . windows . forms . control . controlnativewindow . windowproc (Message&m)'r'n at System.Windows.Forms.NativeWindow。DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)'r'n at system . windows . forms . unsafenativeemethods . dispatchmessagew (MSG&'r'n at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager. msg 'r'nFPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)'r'n at System.Windows.Forms.Application.ThreadContext. confRunMessageLoopInner(Int32 reason, ApplicationContext context)'r'n at System.Windows.Forms.Application.ThreadContext。runmessagelloop (Int32 reason, ApplicationContext context)'r'n at System.Windows.Forms.Application。运行(窗体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(对象状态)'r'n at System.Threading.ExecutionContext。运行(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或其他无效的参数提供给方法。
下面这行不能立即抛出异常:
Application.Run(new SpaceInvaders());
但是一些初始化函数可能会产生问题。
查看堆栈跟踪和代码后编辑:
请参阅//some code
部分。检查g.dispose()
之前的代码是否没有触发在Draw方法之后执行的任何事件处理程序。如果是这样,那么该事件处理程序应该是需要图形对象的事件处理程序,但您已经处理了它。因此图形参数为空。如需进一步协助,请注明//some code
部分
简短回答:
只是不要在Draw()方法中的Graphics-object上调用Dispose()。图形对象包含本机绘图表面的句柄。如果您处置它,窗体就不能在屏幕上绘制。
你不应该处置别人的Graphics
对象。
只处理你拥有的对象,并且只在你确定没有其他人会使用它的情况下。
如果你从别人那里得到一个一次性物品,你应该假设(除非他们另有说明)他们会在你用完后处理掉它。您不应该自己处理它,也不应该保存它以供以后使用(因为他们可能已经处理了它)