Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't close a window

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Why can't I close a window with
Code:
// close progress
        public override void CloseProgress()
        {
            this.Close();
        }

This is delegated via
Code:
        private delegate void CloseProgressMsg();
        private CloseProgressMsg CloseMsg;

       this.CloseMsg = this.Frm.CloseProgress;

        public void CloseProgress()
        {
            this.CloseMsg();
            this.Frm = null;
        }

this.Close() errors with...
{"The calling thread cannot access this object because a different thread owns it."}

I'm using exactly the same paradigm to update the forms GU control with no problems (ok there are as it doesn't do it in real time (in VBA it's resolved with DoEvents!, but that's another issue!).

Currently I can't get rid of the progress window when I'm finished with it.

The code that calls the close method via the destructor is in the same class which created it ...

Code:
        // destructor
        ~Email()
        {
            Utils.Msg("Close = " + this.ProgressMsg.Close.ToString());
    
            // Close progress window
            if(this.ProgressMsg.Close)
            {
                Utils.Msg("Trying to close poxy window");
                this.ProgressMsg.CloseProgress();
                this.ProgressMsg = null;
            }

I don't understand the error message or any forum I have found regarding it.

What thread is what? Who's the owning thread? What does this mean.

Thanks,
1DMF





"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
 
Well I don't know if I'm on the right track but I feel you cannot auto close a window via the destructor as I assume the garbage collector that calls the destructor is considered another thread and isn't the owning thread that created the window (form) object in the first place?

I'm not sure if it is possible to allow this behavior or if it is considered desirable so I decided to implement the IDisposable interface / paradigm and force my client to call the Dispose() method when it has finished with the main Email object...

Code:
        // Public implementation of Dispose pattern callable by consumers. 
        public void Dispose()
        {            
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        // Protected implementation of Dispose pattern. 
        protected virtual void Dispose(bool disposing)
        {
            if (disposed)
             return; 

            if (disposing) 
            {
                // Close progress window
                if (this.ProgressMsg.Close)
                {
                    this.ProgressMsg.CloseProgress();
                }
            }

            this.oStatus = null;
            this.ErrorCodes = null;
            this.ProgressMsg = null;
            this.ClearOutlook();

            disposed = true;

       }

       ~Email()
       {
           Dispose(false);
       }

It appears to have resolved the thread ownership problem when trying to close the form, but I assume any old method that was called from the client would suffice, so is it just good practice to use the IDisposable interface and expect clients to call the Dispose method when you need to clean up objects such as closing windows in a class?

Or am I way off the mark and doing something wrong?

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top