无法在Program.cs主方法上访问已处置的对象

时间:2014-12-08 08:53:48

标签: c# winforms objectdisposedexception

我有一个WinForm应用程序,有两种不同的形式。如果第一个命令行参数是“download”,则应显示Download表单。我在我的计划的ObjectDisposedException方法的Application.Run(new Download(args));行上获得Main

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        if (args.Length > 0)
            switch (args[0])
            {
                case "download":
                    if (args.Length == 4)
                        Application.Run(new Download(args));
                    break;
                default:
                    Application.Run(new ApsisRunner(args));
                    break;
            }
    }
}

enter image description here

更新 异常堆栈跟踪

   at System.Windows.Forms.Control.CreateHandle()
   at System.Windows.Forms.Form.CreateHandle()
   at System.Windows.Forms.Control.get_Handle()
   at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
   at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
   at System.Windows.Forms.Control.set_Visible(Boolean value)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at ApsisCampaignRunner.Program.Main(String[] args) in c:\Users\pedram.mobedi\Documents\GitHub\Postbag\ApsisCampaignRunner\Program.cs:line 31
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

2 个答案:

答案 0 :(得分:2)

你是否在下载表单的构造函数中做了类似的事情?

enter image description here

问题可能出在下载表单的代码中。您不应该在构造函数中关闭或处理Form。

答案 1 :(得分:1)

你发布的代码很好,但是如果一个对象处置异常发生在你的Download类中的任何地方,它会被调用堆栈抛出,直到你看到它为止(主方法),

原因是您在处理后尝试将表单设置为可见。

您可以尝试中断ObjectDisposed Exceptions并找到它抛出的确切行,您可以在Debug - &gt;下执行此操作。异常。