C#, Win Service And COM (OPC Server) --> two instances of the process
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: C#, Win Service And COM (OPC Server) --> two instances of the process

  1. #1
    Join Date
    Jan 2004
    Posts
    51

    C#, Win Service And COM (OPC Server) --> two instances of the process

    Hello!

    I have a COM application (it is OPC Server actually - OPCServer.exe) that works just fine.
    It collects data from external device connected to serial port, ethernet, ...

    This COM app also have a Windows Form for adding/previewing the current data. Basically:
    - If there is no clients connected to OPCServer.exe then the app shuts down.
    - When first client tries to get the information from OPCServer.exe, COM starts application and OPCServer.exe starts serving client requests.
    - When another client connects, he just joins to existing OPCServer.exe.


    Now. I am developing another application that gets all data from OPC Server (app described above). It all works fine until I want to write my app as Windows Service.

    The problem is that Win service is running under Local System account.
    When I tried to connect to OPCServer.exe, server starts just fine (but under another account --> Local System).
    I have no problem with that, but when user starts his own desktop application (another client) to connect to OPCServer.exe --> ANOTHER INSTANCE of OPCServer.exe is started.
    - first instance runs under Local System account (started by windows service)
    - second instance runs under User account (THAT IS A PROBLEM --> 2 instances using same serial port!!!)

    (Additional info:
    If user tries to run another OPCServer.exe it is smart enough that figures it out that another instance is already running --> so nothing happens)
    So there should be no second instance, right?

    I was thinking...

    1.
    If I am not mistaken, the nature of Windows Services is that "by default" they cannot interact with user desktop (which makes perfect sense by the way).
    I said: let's crash this rule and make Windows Service interactive.
    I checked Windows Service's flag "Allow service to interact with desktop".
    Still the same. I get two instances.

    2. Run win service as user account (the same account as user currently logged-in).
    Nope. That didn't help either.
    Although I get 2 instances running under same (user) account.



    I was talking to my friends (not really windows service programming experts). Funny. They all suggested singleton pattern for OPCServer.exe.
    But the way I see this, that is not a problem (see bold text above - that is a sort of singleton right).

    The problem is that user's application (OPC Client) couldn't find running instance. So it starts another.

    I hope you understand what I wrote.

    Any ideas?

    Thanks,
    [Beer]

  2. #2
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    422

    Re: C#, Win Service And COM (OPC Server) --> two instances of the process

    though I could not understand exactly what is your problem if 2 instances of opcserver is running , when you start a non-window service client ?

    I have one question.. in the title you have mentioned C# , what does that fit in your this big system ?

  3. #3
    Join Date
    Jan 2004
    Posts
    51

    Re: C#, Win Service And COM (OPC Server) --> two instances of the process

    Sorry. I was focused on explaining a problem, but I guess I failed that.
    I forgot to put some tiny but important details.

    Quote Originally Posted by vcdebugger View Post
    I have one question.. in the title you have mentioned C# , what does that fit in your this big system ?
    Windows Service is written in C#. Since I am programming Windows Service Application, I assumed the problem is in my code (or I chose wrong approach).


    Quote Originally Posted by vcdebugger View Post
    though I could not understand exactly what is your problem if 2 instances of opcserver is running , when you start a non-window service client ?
    Ok. I hope I could simplify this.
    Forget about windows service for a second.
    Let's say we have registered COM app (OPCServer.exe). Let's also say we have 2 desktop applications (Client1 and Client2). Assume OPC Server is not running at the moment. OPC Server is identified by its name (for example: "Matrikon.SimulationServer").

    1. Client1 want's to connect to "Matrikon" OPC Server (it's not running yet - remember). Windows is capable to understand such requests and find the application it needs to start (in this case OPCServer.exe).
    Client1 is now running and gets data from OPC server("Matrikon").

    2. Now. Client2 also want's data from "Matrikon.SimulationServer". So it tries to connect and because the OPC server is already running, it just joins to the existing instance of server (does not start its own instance - NEVER). There should be only 1 instance of OPC Server application (it is written that way).
    I could have 100 applications connecting (locally or remotely) to the same OPC Server (and there will always be only one instance of OPC server).

    Now. Back to Windows Services.
    Client2 was simple Windows Forms application. I reprogramed this Client2 as windows service app. The problem is that now it starts its own instance of OPC Server. That is the problem.

    Who's to blame?
    1. Windows COM implementation
    2. OPC Server "singleton patter" implementation
    3. My Client2 Windows Service implementation


    I hope it's more clear now.

    Thanks

  4. #4
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    422

    Re: C#, Win Service And COM (OPC Server) --> two instances of the process

    thanks for the crystal clear explanation .. things are far clear now...
    though I dont have much knowledge on this COM stuff...i can suggest some things...

    who has developed the OPCServer ? If at all you have developed it using COM you can program such that there will always be one process ( instance) running of its own instance - opcserver.exe

    Try to see if you can change in your client application not to start one more OPCServer instance.

  5. #5
    Join Date
    Jan 2004
    Posts
    51

    Re: C#, Win Service And COM (OPC Server) --> two instances of the process

    Hi again,

    I found a source of my problem. Haven't found a solution yet, but I am getting closer.

    I started form those 3 questions I'd mentioned before.

    1. Is Windows COM implementation?
    My answer: Not likely!

    2. Is My Windows service App responsible?
    Well. It's possible, but let's say I believe in myself and I did everything right (optimistic, I know)

    3. OPC Server implementation?
    This is what I can check. I have tried 3 more demo servers and this is what I found out.
    Just 1 server works as it should (As I would expect). Fortunately it is a server from a large company that is trustworthy (they are Alpha and Omega of OPC Servers).

    I turned out that I should always check number 2, before I give up and ask questions on forums.

    Thanks for your help.
    I appreciate it.

    [Beer]

  6. #6
    Join Date
    Dec 2013
    Posts
    1

    Re: C#, Win Service And COM (OPC Server) --> two instances of the process

    Hi, I have same problem. I have a windows form application which read opc tags, make some processing and write in other opc tag. I'm using OPC DA .Net API and test in Graybox OPC Server. That app works good but when I made a windows service with same idea, it read opc server but I don't know if it write. I have another app for plot opc tags which show me values when the windows form is running but don't when de windows service is. Have you any idea what a problem?

  7. #7
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,959

    Re: C#, Win Service And COM (OPC Server) --> two instances of the process

    You'd have to re-write the SERVER to be able to accept MULTIPLE clients at once, or else create multiple VERSIONS of the server (on different ports) and have the client connect to their own
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center