Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# threading question: Invoke() fails with ObjectDisposedException

C# threading question: Invoke() fails with ObjectDisposedException

Scheduled Pinned Locked Moved C#
questioncsharpdebugginghelpannouncement
21 Posts 13 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Keith Vitali

    Hello, I have been struggling with some threading issues over the last couple of days. I am getting close to getting this thing to work but there are still a few niggles. So, I have a thread that is called as follows and after the thread starts, my application shows a modal dialog box. So far so good:

    t = new System.Threading.Thread
    (delegate()
    {
    result = Init();
    });
    t.Start();
    dialog.ShowDialog();

    This works fine and there are no problems. Now, the user can hide this dialog box and when that happens the subsequent code gets executed as expected and this is not a problem. Also, I use ShowDialog(), so hiding or calling Close() should not call dispose as indicated in the docs. Also, the dialog box is a singleton and lives for the duration of the application. Now, my dialog box has a progress bar which gets updated by the calling thread and the update method that gets executed is as follows:

    delegate void ProgressValueDelegate(int value);
    public void SetProgressValue(int value)
    {
    if (this.InvokeRequired)
    {
    ProgressValueDelegate pvd = new ProgressValueDelegate(SetProgressValue);
    this.Invoke(pvd, new object[] { value });
    }
    else
    {
    m_progressBar.Value = value;
    }
    }

    So, as soon as the dialog box is hidden, the subsequent call crashes at the Invoke() call. I think there is some race condition going on somewhere because in the debugger the InvoleRequired value is fasle. However, the code has already entered the 'if' condition. Does anyone know how I can handle this sort of situation? Thanks, Keith

    J Offline
    J Offline
    Jason J Chase
    wrote on last edited by
    #21

    When developing apps that use worker threads which like to report back a status, a good technique I have used on projects is to set up a data structure (class or struct) that holds member variables for all the "stats" that the threads must update. Each thread updates the fields in the class/struct, without caring who may be "listening". For dialogs that like to report the status of these threads (i.e. using progress bars, updating text boxes etc), they query the class / struct member variables. This way the relationship is decoupled and state-free. This is a technique we used a lot in the Win32 programming model, and it works just as well in .Net.

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups