Home All Groups Group Topic Archive Search About

ActiveX Control Crashes - VB6

Author
10 Mar 2009 3:25 PM
Jonathan
I have an ActiveX control I custom made. It has an event that is handled by
the application that uses the ActiveX control. The application that uses the
ActiveX control uses END to stop the application if a certain condition
occurs and it crashes the application. Why does this happen and is there any
way to fix it?


Code in the ActiveX control

Sub Process1


If condition true then
   RasieEvent Event1
end if

End sub




Code in main application that uses ActiveX Control

Sub ActiveX_Event1

'this is where it crashes
end

end sub


I don't quite understand why it does that. Is there any way to prevent it?
Thanks for the help.

Author
10 Mar 2009 3:41 PM
Dave O.
Let me be the first - NEVER use End, it is a throwback and it stops the
program, dead, without really clearing up properly.
Get rid of the End, close the application properly and then see if the
problem persists.

Dave O.

Show quoteHide quote
"Jonathan" <Jonat***@discussions.microsoft.com> wrote in message
news:97FECABD-134E-468A-A7B0-37B3261F4101@microsoft.com...
>I have an ActiveX control I custom made. It has an event that is handled by
> the application that uses the ActiveX control. The application that uses
> the
> ActiveX control uses END to stop the application if a certain condition
> occurs and it crashes the application. Why does this happen and is there
> any
> way to fix it?
>
>
> Code in the ActiveX control
>
> Sub Process1
>
>
> If condition true then
>   RasieEvent Event1
> end if
>
> End sub
>
>
>
>
> Code in main application that uses ActiveX Control
>
> Sub ActiveX_Event1
>
> 'this is where it crashes
> end
>
> end sub
>
>
> I don't quite understand why it does that. Is there any way to prevent it?
> Thanks for the help.
Author
10 Mar 2009 4:48 PM
Jonathan
Dave,

I switched to unload me. Is that any better than end?

Here is my full code.It reproduces the error in the main application I have
thats mroe complex.


'---------------------- OCX ----------------------
Event OnRecieve()

Public Function OpenPort(Num As Integer) As Boolean

On Error GoTo EventHandler

OpenPort = False

With Com
    .CommPort = Num
    .PortOpen = True
End With

Exit Function

OpenPort = True

EventHandler:

End Function

Private Sub Com_OnComm()

If Com.CommEvent = 2 Then
    'Trigger an even when data is recieved
    RaiseEvent OnRecieve
End If

End Sub

Private Sub UserControl_Terminate()
'close the port
Com.PortOpen = False
End Sub




'--------------------- App ---------------------------
Private Sub Form_Load()
TestControl1.OpenPort (1)

End Sub

Private Sub TestControl1_onRecieve()

Unload Me
'crashes after the end sub
End Sub


Interestingly enough, the program does not crash when the close button is
pressed. The main app just have a standard form on it.




Show quoteHide quote
"Dave O." wrote:

> Let me be the first - NEVER use End, it is a throwback and it stops the
> program, dead, without really clearing up properly.
> Get rid of the End, close the application properly and then see if the
> problem persists.
>
> Dave O.
>
> "Jonathan" <Jonat***@discussions.microsoft.com> wrote in message
> news:97FECABD-134E-468A-A7B0-37B3261F4101@microsoft.com...
> >I have an ActiveX control I custom made. It has an event that is handled by
> > the application that uses the ActiveX control. The application that uses
> > the
> > ActiveX control uses END to stop the application if a certain condition
> > occurs and it crashes the application. Why does this happen and is there
> > any
> > way to fix it?
> >
> >
> > Code in the ActiveX control
> >
> > Sub Process1
> >
> >
> > If condition true then
> >   RasieEvent Event1
> > end if
> >
> > End sub
> >
> >
> >
> >
> > Code in main application that uses ActiveX Control
> >
> > Sub ActiveX_Event1
> >
> > 'this is where it crashes
> > end
> >
> > end sub
> >
> >
> > I don't quite understand why it does that. Is there any way to prevent it?
> > Thanks for the help.
>
>
>
Author
10 Mar 2009 7:45 PM
Larry Serflaten
"Jonathan" <Jonat***@discussions.microsoft.com> wrote

> I switched to unload me. Is that any better than end?

"I'm leaving now, would you like me to close the door on
my way out...   ...or destroy the house as I go?"

The difference between the two is about that drastic.

At first glance, it would seem you've got yourself into a
situation that has no easy solution.  You want to unload
the form that contains a control, from within the control's
event handler.  But in some (not all) circumstances, VB
is expecting to return to code located in the control, so
how can it be unloaded?

When VB gets into a situation like that, it throws an error,
(Error 365) but that error isn't trappable, so you can't test
for it....

To see the different conditions VB handles like that, paste
this into a new form, run it, and press the Help button on
the Error dialog:

Private Sub Form_Paint()
  Unload Me
End Sub

Their solution is to remove the Unload command from the
offending routine.   That is about all you can do in your own
situation....

LFS
Author
10 Mar 2009 8:31 PM
Karl E. Peterson
Larry Serflaten wrote:
> "Jonathan" <Jonat***@discussions.microsoft.com> wrote
>
>> I switched to unload me. Is that any better than end?
>
> "I'm leaving now, would you like me to close the door on
> my way out...   ...or destroy the house as I go?"
>
> The difference between the two is about that drastic.

Best language-car analogy ever: Using End to stop your application is like using a
telephone pole to stop your car.  Highly effective, both!
--
..NET: It's About Trust!
http://vfred.mvps.org
Author
10 Mar 2009 11:38 PM
Bill McCarthy
Hi Jonathan,

Try closing the com port before closing the form.


Show quoteHide quote
"Jonathan" <Jonat***@discussions.microsoft.com> wrote in message
news:6F184338-CFB1-4123-976F-566A95BDDACB@microsoft.com...
> Dave,
>
> I switched to unload me. Is that any better than end?
>
> Here is my full code.It reproduces the error in the main application I
> have
> thats mroe complex.
>
>
> '---------------------- OCX ----------------------
> Event OnRecieve()
>
> Public Function OpenPort(Num As Integer) As Boolean
>
> On Error GoTo EventHandler
>
> OpenPort = False
>
> With Com
>    .CommPort = Num
>    .PortOpen = True
> End With
>
> Exit Function
>
> OpenPort = True
>
> EventHandler:
>
> End Function
>
> Private Sub Com_OnComm()
>
> If Com.CommEvent = 2 Then
>    'Trigger an even when data is recieved
>    RaiseEvent OnRecieve
> End If
>
> End Sub
>
> Private Sub UserControl_Terminate()
> 'close the port
> Com.PortOpen = False
> End Sub
>
>
>
>
> '--------------------- App ---------------------------
> Private Sub Form_Load()
> TestControl1.OpenPort (1)
>
> End Sub
>
> Private Sub TestControl1_onRecieve()
>
> Unload Me
> 'crashes after the end sub
> End Sub
>
>
> Interestingly enough, the program does not crash when the close button is
> pressed. The main app just have a standard form on it.
>
>
>
>
> "Dave O." wrote:
>
>> Let me be the first - NEVER use End, it is a throwback and it stops the
>> program, dead, without really clearing up properly.
>> Get rid of the End, close the application properly and then see if the
>> problem persists.
>>
>> Dave O.
>>
>> "Jonathan" <Jonat***@discussions.microsoft.com> wrote in message
>> news:97FECABD-134E-468A-A7B0-37B3261F4101@microsoft.com...
>> >I have an ActiveX control I custom made. It has an event that is handled
>> >by
>> > the application that uses the ActiveX control. The application that
>> > uses
>> > the
>> > ActiveX control uses END to stop the application if a certain condition
>> > occurs and it crashes the application. Why does this happen and is
>> > there
>> > any
>> > way to fix it?
>> >
>> >
>> > Code in the ActiveX control
>> >
>> > Sub Process1
>> >
>> >
>> > If condition true then
>> >   RasieEvent Event1
>> > end if
>> >
>> > End sub
>> >
>> >
>> >
>> >
>> > Code in main application that uses ActiveX Control
>> >
>> > Sub ActiveX_Event1
>> >
>> > 'this is where it crashes
>> > end
>> >
>> > end sub
>> >
>> >
>> > I don't quite understand why it does that. Is there any way to prevent
>> > it?
>> > Thanks for the help.
>>
>>
>>