INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Can't close a window

Can't close a window

(OP)
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...

Quote:

{"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

RE: Can't close a window

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close