1

I have a WinForm application with two different forms. If the first command line argument is "download", the Download form should appear. I get an ObjectDisposedException on the Application.Run(new Download(args)); line on the Main method of my program.

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

Update: Exception stack trace

   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()
disasterkid
  • 6,948
  • 25
  • 94
  • 179
  • Does it say what the disposed object is? – Omri Aharon Dec 08 '14 at 09:01
  • This may be due to your Main method exiting before the Download dialog is initialised. Or is Run() a blocking call in WinForms? The parent app may be calling Dispose() on it before it appears. – olitee Dec 08 '14 at 09:03
  • @OmriAharon I added a screenshot. – disasterkid Dec 08 '14 at 09:04
  • Can you try to change your project to a Console Application? – Omri Aharon Dec 08 '14 at 09:06
  • @olitee This is the entry point to the program, so how is it possible for Main method exist before the Download dialog is initialized. Or, I mean, how can I make sure of that? I am running this app with command line arguments, so I don't understand what the role of the parent app is here. – disasterkid Dec 08 '14 at 09:08
  • 1
    The problem can occur anywhere in your application, can you post the exception call stack – quadroid Dec 08 '14 at 09:08
  • It's possible if the Application.Run method does not block, which is why I asked. It might be helpful to know exactly which object is throwing the disposed exception. Can you look at the exception details and let us know? Is it the Application or Download object ... Or something else? – olitee Dec 08 '14 at 09:11
  • @olitee Application.Run is a blocking call – quadroid Dec 08 '14 at 09:11
  • @Console I added the stack trace at the end of the question. – disasterkid Dec 08 '14 at 09:12
  • @It's the `Download` object that is triggering the exception. – disasterkid Dec 08 '14 at 09:12
  • My guess is you the form is open for edit in a visual tool and somehow the controls are in some funky state. Wild guess. Restart your Visual C#.. – Hanan Dec 08 '14 at 09:15

2 Answers2

4

Are you doing something like this one in the Constructor of Download form?

enter image description here

The problem might be in code of Download Form. You should not close or dispose Form in the constructor.

Sarvesh Mishra
  • 2,014
  • 15
  • 30
1

The code you posted is fine, but if a object disposed exception occures anywhere inside your Download class it is thrown up the call stack until where you see it (the main method),

The cause is that you try to set your form visible after you disposed it.

You can try to break on ObjectDisposed Exceptions and find the exact line it is thrown, you can do so under Debug -> Exceptions.

quadroid
  • 8,444
  • 6
  • 49
  • 82
  • I do not have a background thread in my `Download` form yet. But in my `ApsisRunner` form which will be run if the first command line argument is not a string that is "download", I am using `BackgroundWorker`s. – disasterkid Dec 08 '14 at 09:16