Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8452169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:25:58+00:00 2026-06-10T11:25:58+00:00

My application works as intended if I do not use debugging in VisualStudio 2010,

  • 0

My application works as intended if I do not use debugging in VisualStudio 2010, but I get ObjectDisposedException complaints in debug mode. I have read through MS documentation and several other threads and I cannot spot the problem. The points of exception are noted in the code and the stack traces follow. Thanks for any help.

        Form1.cs

        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Linq;
        using System.Text;
        using System.Windows.Forms;
        using System.Threading;
        using CSharpJExcel.Jxl;

        namespace Importer
        {
            public partial class Form1 : Form
            {
                public Form1()
                {
                    InitializeComponent();
                    openFileDialog1.Filter = "Text Files (.xls)|*.xls|All Files (*.*)|*.*";
                    openFileDialog1.FilterIndex = 1;
                    openFileDialog1.FileName = as400TextBox.Text;
                }

                delegate void messageBoxCallback(string text);

                private void button1_Click(object sender, EventArgs e)
                {
                    openFileDialog1.FileName = as400TextBox.Text;
                    if (openFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        as400File = openFileDialog1.FileName;
                        as400TextBox.Text = as400File;
                        buildDataView(as400TextBox.Text);
                    }
                    else
                    {
                        as400TextBox.Text = "";
                        openFileDialog1.FileName = as400TextBox.Text;
                    }

                }

                private void uploadButton_Click(object sender, EventArgs e)
                {

                    Workbook workbook = Workbook.getWorkbook(new System.IO.FileInfo(as400TextBox.Text));
                    var sheet = workbook.getSheet(0);
                    uploadProgress.Maximum = sheet.getRows();
                    workbook.close();

                    backgroundWorker1.RunWorkerAsync((String)as400TextBox.Text);

                }

                private string as400File;

                private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
                {

                    appendMessage(((string)e.Argument));
                    Workbook workbook=null;            
                    workbook = Workbook.getWorkbook(new System.IO.FileInfo(((String)e.Argument)));
                    appendMessage("Workbook opened");            
                    var sheet = workbook.getSheet(0);
                    int r = 1;
                    string barcode, fname, lname, adname, student_id;
                    Cell[] row;
                    //uploadProgress.Maximum = sheet.getRows();

                    while (true)//process xls to SQL
                    {
                        try
                        {
                            row = sheet.getRow(r);
                        }
                        catch (IndexOutOfRangeException)
                        {
                            appendMessage("Processed: " + r);
                            workbook.close();
                            break;

                        }

                        student_id = ((string)row[0].getContents()).Trim();
                        barcode = ((string)row[1].getContents()).Trim();
                        lname = ((string)row[2].getContents()).Trim();
                        fname = ((string)row[3].getContents()).Trim();
                        adname = ((string)row[4].getContents()).Trim() + ((string)row[5].getContents()).Trim();

                        appendMessage(lname + " " + fname + " " + barcode + " " + adname + " " + student_id);
                        backgroundWorker1.ReportProgress(++r);
                    }


                }

                private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
                {
                    uploadProgress.Value = e.ProgressPercentage;
                    appendMessage("Status: " + (int)e.UserState);
                }

                private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
                {
                    appendMessage("Done!");
                }

                private void appendMessage(string msg)
                {

                    if (InvokeRequired)
                    {
                        messageBoxCallback mb = new messageBoxCallback(appendMessage);

                       this.Invoke(mb, new object[] { msg });//<---ObjectDisposedException

                       return;
                    }

                    messageBox.Text += (msg + Environment.NewLine);

                }


                private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
                {

                }


            }
        }


        Program.cs:

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Windows.Forms;

        namespace Importer
        {
            static class Program
            {
                /// <summary>
                /// The main entry point for the application.
                /// </summary>
                [STAThread]
                static void Main()
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Form1());//<--TargetInvocationException (after ObjDisp is swallowed)
                }
            }
        }

    Stack Trace:
    System.ObjectDisposedException was unhandled by user code
      Message=Cannot access a disposed object.
    Object name: 'Form1'.
      Source=System.Windows.Forms
      ObjectName=Form1
      StackTrace:
           at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
           at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
           at Importer.Form1.appendMessage(String msg) in C:\Users\Administrator\Google Drive\prj_tmp\Importer\Importer\Form1.cs:line 188
           at Importer.Form1.backgroundWorker1_DoWork(Object sender, DoWorkEventArgs e) in C:\Users\Administrator\Google Drive\prj_tmp\Importer\Importer\Form1.cs:line 163
           at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
           at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
      InnerException: 

If I swallow this exception I get:

System.Reflection.TargetInvocationException was unhandled
  Message=Exception has been thrown by the target of an invocation.
  Source=mscorlib
  StackTrace:
       at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
       at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
       at System.Delegate.DynamicInvokeImpl(Object[] args)
       at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
       at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
       at System.Threading.ExecutionContext.runTryCode(Object userData)
       at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
       at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       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 AlphaCardImporter.Program.Main() in C:\Users\Administrator\Google Drive\prj_tmp\Importer\Importer\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(Assembly 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.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.NullReferenceException
       Message=Object reference not set to an instance of an object.
       Source=AlphaCardImporter
       StackTrace:
            at AlphaCardImporter.Form1.backgroundWorker1_ProgressChanged(Object sender, ProgressChangedEventArgs e) in C:\Users\Administrator\Google Drive\prj_tmp\Importer\Importer\Form1.cs:line 173
            at System.ComponentModel.BackgroundWorker.OnProgressChanged(ProgressChangedEventArgs e)
            at System.ComponentModel.BackgroundWorker.ProgressReporter(Object arg)
       InnerException: 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:25:59+00:00Added an answer on June 10, 2026 at 11:25 am

    Found it! I was calling junk left over from when I was experimenting.

    This was the offending line:

    appendMessage("Status: " + (int)e.UserState); <-- there is no (int)e.UserState
    

    When I ran without debugging in VS2010 I got a JIT runtime message that was much more meaningful which led me to that line.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My application works for iOS 5.1 but for iOS 6 simulator I get the
My WPF application works fine on a number of machines, but now I get
I have an C# form application that use an access database. This application works
Application works fine. I can access everything in debug except for an Array Adapter
My application works fine on the dev server but when I upload it fails
I have a C# WinForms application with a database backend (oracle) and use NHibernate
I like Django, but for a particular application I would like to use only
I have a Silverlight 4 Beta application where I'd like to use the SharePoint
I have an application that uses disabled JTextFields in several places which are intended
My application is intended to work almost entirely through a Windows 7 taskbar item

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.