2014-12-08 81 views
1

我有一个WinForm应用程序有两种不同的形式。如果第一个命令行参数是“下载”,则应出现Download表单。我在Main方法的Application.Run(new Download(args));行上获得ObjectDisposedException行。无法访问Program.cs上的处置对象主要方法

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() 
+0

它说什么处置对象是什么? – 2014-12-08 09:01:31

+0

这可能是由于您的Main方法在Download对话框初始化之前退出。或者是WinForms中的Run()阻塞调用?父应用可能在它出现之前调用Dispose()。 – olitee 2014-12-08 09:03:45

+0

@OmriAharon我添加了一个截图。 – Disasterkid 2014-12-08 09:04:25

回答

2

你在下载构造函数中做这样的事吗?

enter image description here

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

+0

是的,我是!我正在使用'this.Close()'! – Disasterkid 2014-12-08 09:17:26

+0

这就是问题所在。你正在创建一个对象时... – 2014-12-08 09:19:28

1

您发布的代码是好的,但如果一个对象设置例外的任何地方occures您的下载类里面抛出了调用堆栈直到你看到它(主要方法),

原因是你试图设置你的窗体后可以看到它。

您可以尝试打断ObjectDisposed Exceptions并找到它引发的确切行,您可以在Debug - > Exceptions下执行此操作。

+0

我的'下载'表单中没有后台线程。但是在我的'ApsisRunner'表单中,如果第一个命令行参数不是一个“下载”字符串,那么我将使用'BackgroundWorker's。 – Disasterkid 2014-12-08 09:16:47