471,072 Members | 1,378 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,072 software developers and data experts.

Serial Port UnauthorizedAccess exception

344 100+
I'm working on simple chat program via serial port. It works fine, but i face this problem.
When i try to open the exe, UnauthorizedAccess exception occurs, i can't use COM1 becuase someone else has it. I checked it and found that another copy of the program still work (by task manager). That's mean closing the program will not close the serial port. I tried these :
Note: I read it using a separate thread.

1)override OnClose to, close the serial port.
But this evaluates an exception, where the thread is accessing the port and i terminate it.
2)override OnClose to, abort the thread then close the port.
A message that information cloud be lost appears.
3)override OnClose to, suspend the thread then close the port.
exception occurs just like (1).

I don't want to open the port and close it evey time i read or write, it'll be very ugly.
Feb 6 '09 #1
2 2998
tlhintoq
3,525 Expert 2GB
Please don't take offense at the title of the post. I'm not being picky about your grammar - But computers are VERY picky about it. Forms are closed. Applications are exited. It's important that you as the coder know the difference because you are only going to find what you look for. If you look for a means to close that is what you will find. If you know you need to look for a means by which to 'exit' then that is what you will find. Intellisense in Visual Studio will help you get what you ask for, not what you intend.
I checked it and found that another copy of the program still work (by task manager). That's mean closing the program will not close the serial port.
I don't think that's what it means. If you found the program still running via Task Manager, then it means you didn't really exit the application which is why the port was still in use. Once you used Task Manager to exit the still running application your serial port was closed and available to you, right? So exiting the application really does close the serial port. I'm going to guess that you coded what you described in your post "closing" not "exiting".Just because you don't have a form open doesn't mean a program isn't running.

I deal with serial ports all the time: Laser sensors, contact closure switches, custom USB->serial controllers etc. I can assure you that if all the threads of your application actually exit, then the serial port will close.

I'm going to guess that when your code think's its exiting the application it really isn't. Instead it may be only closing a form.
Could you show your code for when you exit the application?
If you have something like
Expand|Select|Wrap|Line Numbers
  1. this.close();
that is going to close the form, not exit the application. You want to use something like
Expand|Select|Wrap|Line Numbers
  1. application.exit();
instead.
Feb 7 '09 #2
Bassem
344 100+
Thanks a lot,
I only had to add an event handler to form_closing to close the port, and the thread try to access the port if exception it abort.
Thanks again for your help.
Regrads,
Bassem
Feb 8 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by mimisam | last post: by
13 posts views Thread by Rob | last post: by
2 posts views Thread by joaquimfpinto | last post: by
4 posts views Thread by cmdolcet69 | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.