2014-12-08 75 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