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 7654021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:10:30+00:00 2026-05-31T12:10:30+00:00

I am maintaining a program with customer informations. It consists of many forms that

  • 0

I am maintaining a program with customer informations. It consists of many forms that each show some relevant info from the database. This error is in a single form after doing the following

  1. Open the customer search form
  2. View random customer A info in the customerinfo form
  3. open the crm form and it automatically shows customer A. Then add a file to him via draganddrop.
  4. Close the last two forms and select random customer B and do the same.
  5. Close the last two forms and select customer A and add a new file. Error!!!

Here is the code that fails:

private void FireFileCountChanged() {
    if (FileCountChanged != null)
        BeginInvoke(new DeferEvent(FireFileCountChangedDeferred), 2); // FAILS

“An unhandled exception of type ‘System.InvalidOperationException’ occurred in System.Windows.Forms.dll

Additional information: Invoke or BeginInvoke cannot be called on a control until the window handle has been created.”

I tried adding the following:

private void FireFileCountChanged() {
        if (FileCountChanged != null && this.Handle != null) // CHANGED AND FAILS.
            BeginInvoke(new DeferEvent(FireFileCountChangedDeferred), 2);
    }

But the this.handle gives:

‘this.Handle’ threw an exception of type ‘System.ObjectDisposedException’ and
“Cannot access a disposed object.\r\nObject name: ‘AttachmentsControl’.”

Then I added a timeout of 10 seconds as the first line in the method, but the handle is still not created. Has the handle somehow been disposed when one of the window were closed? And what can be done about this? Any help is appreciated. I’m kind of stuck.

private void FireFileCountChangedDeferred(int repostCount) {
    if (FileCountChanged != null) {
        if (repostCount > 0) {
            //black magic is somehow involved in getting this event to fire *after* the filewatcher reports the change.
            System.Threading.Thread.Sleep(10);
            BeginInvoke(new DeferEvent(FireFileCountChangedDeferred), repostCount - 1);
        } else
            FileCountChanged(this, null);
    }
}

private void CopyFiles(string[] files, bool reload) {
    if (CreatePath()) {
        foreach (string src in files) {
            try {
                string dest = MakeSafeFilename(src);
                File.Copy(src, dest);
                FireFileCountChanged();
            } catch (Exception ex) {
                //Util.Print("Copy ex: {0}", ex.Message);
                ErrMsg("Error while copying:{1}{0}", ex.Message, environment.NewLine);
            }
        }
    }
}

private void Lstv_DragDrop(object sender, DragEventArgs ea) {
    if (m_CanAdd) {
        string[] files = GetDraggedFiles(ea);
        if (files != null)
            CopyFiles(files, true);
        else if (OutlookDataObject.HoldsOutlookData(ea) && CreatePath()) {
            try {
                OutlookDataObject.CopyDroppedFiles(ea, m_Path, OutlookFilenameCallback);
            } catch (Exception ex) {
                //Util.Print("Copy ex: {0}", ex.Message);
                ErrMsg("Error copying from Outlook:{1}{0}", ex.Message, Environment.NewLine);
            }
        }
    }
}

Solution

private void FireFileCountChanged() {
                while (!this.IsHandleCreated) // added
                          System.Threading.Thread.Sleep(100); //added

    if (FileCountChanged != null)
        BeginInvoke(new DeferEvent(FireFileCountChangedDeferred), 2);
  • 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-05-31T12:10:31+00:00Added an answer on May 31, 2026 at 12:10 pm

    You need to check the IsHandleCreated property, not compare the Handle to null. Reading the Handle property is considered a UI operation itself.

    private void FireFileCountChanged() {
        if (FileCountChanged != null && this.IsHandleCreated)
            BeginInvoke(new DeferEvent(FireFileCountChangedDeferred), 2);
    }
    

    However, based on the complex steps that you need to take to reproduce the bug, I suspect that there are some form instance re-use issues or other more complex issues at play here, and it isn’t just a matter of making this call to BeginInvoke work.

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

Sidebar

Related Questions

I am maintaining a program that reads records from a Access '97 table using
I am maintaining a program that uses the DevShock SMPP component for .NET. I
I have a C/ncurses program that I'm debugging/maintaining. This program does ripoffline twice: first,
I'm maintaining some code that uses a *= operator in a query to a
I have a function call in a program that I'm maintaining has 28 arguments
I'm maintaining a program that needs to parse out data that is present in
I am maintaining a small application that has some plugin-like functionality, which is implemented
I have an OLD mac C/C++ program that I'm maintaining. It's still based on
I am writing a program that will call a function from an external library,
I'm maintaining an old VB6 application, that uses some Sheridan 3D controls (SSPanel and

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.