Serial Port UnauthorizedAccess exception

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bassem
    Contributor
    • Dec 2008
    • 344

    Serial Port UnauthorizedAccess exception

    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, UnauthorizedAcc ess 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.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    We don't 'close' applications we 'exit' them.

    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".J ust 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
    Code:
    this.close();
    that is going to close the form, not exit the application. You want to use something like
    Code:
    application.exit();
    instead.

    Comment

    • Bassem
      Contributor
      • Dec 2008
      • 344

      #3
      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

      Comment

      Working...