.NET

.NET

Reply
Distinguished Contributor
VB_Autocad_guy
Posts: 136
Registered: ‎07-24-2009
Message 1 of 4 (1,100 Views)
Accepted Solution

Error: "Cannot access a disposed object. Objectname: 'frmBatchProcess'.

1100 Views, 3 Replies
02-02-2012 09:59 AM
  <CommandMethod("crane")> _
    Sub Crane()

             Application.ShowModelessDialog(myForm)

    End Sub

 

Show it only happens after I've open the form once. When I close it and try to re-open it it fails to open.

 

How do I dispose of VB.NET objects... and what am I dealing with here? 

 

Okay So I solved it I think?

 

By placing the Constructor inside the procedure

It use to be Public Shared myForm as new frmBatchProcess

 

then 

 

Sub Crane()

 

 

Public Class Crawler
<CommandMethod("crane")> _
Sub Crane()
dim myForm As New frmBatchProcess 'The Instance of the Batch Crawler Window
If myForm.IsDisposed = True Then
MsgBox("Form is Disposed of, Please Restart Autocad and Try Again")
Else
Application.ShowModelessDialog(myForm)
End If
End Sub

 

 

Well, whether you solve it or not would be dependnet on how do you want to use the modeless form and if you know windows form's closing behaviour when being shown as modeless and modal form.

 

1. Windows form closing behaves differently when being shown as modeless and modal. When as modelsss form, calling Form.Close() or clicking "x" on the form, actually disposes the form (truly closed), while whne bing shown as modal form (dialog box, usually), calling Form.Close()/clicking "x" is equal to calling Form.Hide() or Form.Visible=False. The form is not disposed.

 

So, your previous error must be due to your code is calling Form.Close() or user clicking "x", which results in the form being disposed.

 

2. Yes, when you "New" the form each time your comment method is used as your code showing, a new instance of the form is created, regardless if you have closed previously opened form or not. It is completely not necessary to test

 

If myForm.IsDisposed Then...

 

because the form is just created (unless you have complicated code in the form constructor that may dispose itself due to some initializing condition not being met).

 

3. With your code, if you run your commend method more than once without explicitly closing opened form, you would end up with multiple modeless form opening, which may or may not be what you want to. Each time you close a form, it is disposed, data shown on the form is lost. When you "New" the form again, you need to reload data to the form, or even worse, you may have to try to restore the data shown on a closed form. Your code alos has the potential risk of shown mutiple forms with different data from drawing when calling your command method more than once against the same document in different time, because the drawing could be changed by user.

 

4. Usually, it is desired to use a singleton modeless form. That is why you may want to declare Shared myForm As frmBacthprocess at class level. To make it singleton, you handle form's Closing event and set e.Cancel=True, and set Me.Visible=False, so that the form.Close() method (or clicking "x" would not close the form, but hide the form instead.

 

5. Your form is named as frmBatchProcess, which implies that you donot want to run multiple forms. if the batch process means processing multiple drawing files, you would want to set CommandFlags.Session flag in your command method and you certainly want to use a singleton modeless form.

 

Distinguished Contributor
VB_Autocad_guy
Posts: 136
Registered: ‎07-24-2009
Message 2 of 4 (1,097 Views)

Re: Error: "Cannot access a disposed object. Objectname: 'frmBatchProcess'.

02-02-2012 10:20 AM in reply to: VB_Autocad_guy

Okay So I solved it I think?

 

By placing the Constructor inside the procedure

It use to be Public Shared myForm as new frmBatchProcess

 

then 

 

Sub Crane()

 

 

Public Class Crawler

    

    <CommandMethod("crane")> _
    Sub Crane()
       dim myForm As New frmBatchProcess 'The Instance of the Batch Crawler Window 

        If myForm.IsDisposed = True Then
           MsgBox("Form is Disposed of, Please Restart Autocad and Try Again")
        Else
             Application.ShowModelessDialog(myForm)
        End If
    End Sub

 

 

*Expert Elite*
norman.yuan
Posts: 1,063
Registered: ‎04-27-2009
Message 3 of 4 (1,074 Views)

Re: Error: "Cannot access a disposed object. Objectname: 'frmBatchProcess'.

02-02-2012 01:43 PM in reply to: VB_Autocad_guy

Well, whether you solve it or not would be dependnet on how do you want to use the modeless form and if you know windows form's closing behaviour when being shown as modeless and modal form.

 

1. Windows form closing behaves differently when being shown as modeless and modal. When as modelsss form, calling Form.Close() or clicking "x" on the form, actually disposes the form (truly closed), while whne bing shown as modal form (dialog box, usually), calling Form.Close()/clicking "x" is equal to calling Form.Hide() or Form.Visible=False. The form is not disposed.

 

So, your previous error must be due to your code is calling Form.Close() or user clicking "x", which results in the form being disposed.

 

2. Yes, when you "New" the form each time your comment method is used as your code showing, a new instance of the form is created, regardless if you have closed previously opened form or not. It is completely not necessary to test

 

If myForm.IsDisposed Then...

 

because the form is just created (unless you have complicated code in the form constructor that may dispose itself due to some initializing condition not being met).

 

3. With your code, if you run your commend method more than once without explicitly closing opened form, you would end up with multiple modeless form opening, which may or may not be what you want to. Each time you close a form, it is disposed, data shown on the form is lost. When you "New" the form again, you need to reload data to the form, or even worse, you may have to try to restore the data shown on a closed form. Your code alos has the potential risk of shown mutiple forms with different data from drawing when calling your command method more than once against the same document in different time, because the drawing could be changed by user.

 

4. Usually, it is desired to use a singleton modeless form. That is why you may want to declare Shared myForm As frmBacthprocess at class level. To make it singleton, you handle form's Closing event and set e.Cancel=True, and set Me.Visible=False, so that the form.Close() method (or clicking "x" would not close the form, but hide the form instead.

 

5. Your form is named as frmBatchProcess, which implies that you donot want to run multiple forms. if the batch process means processing multiple drawing files, you would want to set CommandFlags.Session flag in your command method and you certainly want to use a singleton modeless form.

 

Distinguished Contributor
VB_Autocad_guy
Posts: 136
Registered: ‎07-24-2009
Message 4 of 4 (1,072 Views)

Re: Error: "Cannot access a disposed object. Objectname: 'frmBatchProcess'.

02-02-2012 02:38 PM in reply to: norman.yuan

AWESOME! Thanks for the clarification.

 

Norman you're a true programmer I can tell Thanks!

 

Appreciate your blog as well.

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.