Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / September 2005

Tip: Looking for answers? Try searching our database.

How to turn-off Timer that calls MSCOMM32.ocx on Form's closing event

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
VR - 19 Sep 2005 19:30 GMT
Hi, I have an WinForm app that is described bellow:

1. The main app
2. A Form that's shown as .ShowDialog()
3. A User Control inside this dialog form
4. An instance of MSCOMM32.ocx inside this user control

The scenario is:

1. When the dialog form is loaded, it starts a timer (that has an interval
of 60miliseconds) that keeps sending data to a device and getting response
from it.
2. When the dialog form is unloaded I get an error "object reference not set
to an instance of an object" because the timer has elapsed during the unload
process an tryed to do another call using MSCOMM32.ocx
3. I made a "TurnOff()" routine that stops the timer, disable it and clean
de COMM buffer
4. I call TurnOff() in the "Closing()" event of the form that contains the
control

The problem is that the issue remains unsolved, that is, even with this
TurnOff() routine I get the same error.

Is there something I'm missing? Is there any other event (in the form that
contains the control) that I should call TurnOff()?

It seems to me that what's happening is this:

When I intercep the .Closing() event and call turnOff, the OCX has already
been destroyed, that is, it's useless to call TurnOff from there, so, my
question is: Is there any other way to intercept the form's closing event
before the built-in .Closing() event? Like, is there any way to be notified
if the user clicked the "red X" button on the form's title bar?

Regards,
Thanks a lot
Victor Reboucas
Charles Law - 20 Sep 2005 00:06 GMT
Hi VR

My first thought is that there must be something else going on in the form
to cause this problem. The reason being that the MSCOMM control should not
have been destroyed when the Closing event fires because it is possible to
cancel the form close from within the closing event. In that case, the
MSCOMM would have to be recreated somehow, and that clearly never happens.

Therefore, can you be sure that you are really shutting down the timer in
the Closing event? Set the debugger to break on all exceptions, and when the
exception occurs check the stack to see what had happened up to that point.

HTH

Charles

> Hi, I have an WinForm app that is described bellow:
>
[quoted text clipped - 33 lines]
> Thanks a lot
> Victor Reboucas
VR - 20 Sep 2005 18:37 GMT
Hi Charles,

I set the debugger to break on all exceptions and the result is the same,
that is, the timer elapses and tries to send a command to MSCOMM that does
not exist anymore.

I thought really interesting the fact that the timer and the ocx are in the
same user control (inside the form) and the instance for the ocx does not
exists anymore, but the timer more than exists, it's still running and
processing, something like I did not set tmr.Stop() and tmr.Enabled = False,
but I did.

By the way, after a lot of tests for use this ocx I realized that the
framework handles all the necessary clean-up in the interop layer
automatically, that is, I do not have to set comm.PortOpen = False, just
closing the form the port is automatically closed.

That means that I just turn off the timer, and did nothing about the ocx.
And this is what's getting me crazy: how can an object that I do nothing has
it's existence shorter than an object that I shut down explicitly?

Well, I'll get your clue about cancel the form's close event, make all the
shutdown process and call the close method explicitly by code.

thanks a lot,
Regards,
Victor Reboucas

> Hi VR
>
[quoted text clipped - 51 lines]
>> Thanks a lot
>> Victor Reboucas

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.