Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Windows service + COM STA

Windows service + COM STA

Scheduled Pinned Locked Moved C#
csharpcomdesigndata-structuresquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SteveL12234
    wrote on last edited by
    #1

    How does one receive events in a C# windows service from a COM object that has been instantiated from a background thread whose ApartmentState == ApartmentState.STA? I’ve been able to received events from the COM object in a WinForm app when the thread sits in a loop sleeping and invoking System.Windows.Forms.Application.DoEvents(), i.e. pumping the message queue. The thread that accesses the COM object is responsible for responding to requests by other objects to access the COM object, done through a queue + AutoResetEvent, and for handling events raised by the COM object in response to those requests. Other posts/web sites have mentioned that STA threads require a message loop/pump. It seems like if the thread that accesses the COM object were able to call a method that could sleep + message pump + respond to requests to wake up, I’d be set. Is there such a beast/can such a thing be created? Is this the right kind of design? Thanks, Steve

    K S 2 Replies Last reply
    0
    • S SteveL12234

      How does one receive events in a C# windows service from a COM object that has been instantiated from a background thread whose ApartmentState == ApartmentState.STA? I’ve been able to received events from the COM object in a WinForm app when the thread sits in a loop sleeping and invoking System.Windows.Forms.Application.DoEvents(), i.e. pumping the message queue. The thread that accesses the COM object is responsible for responding to requests by other objects to access the COM object, done through a queue + AutoResetEvent, and for handling events raised by the COM object in response to those requests. Other posts/web sites have mentioned that STA threads require a message loop/pump. It seems like if the thread that accesses the COM object were able to call a method that could sleep + message pump + respond to requests to wake up, I’d be set. Is there such a beast/can such a thing be created? Is this the right kind of design? Thanks, Steve

      K Offline
      K Offline
      kubben
      wrote on last edited by
      #2

      My understanding of windows services is they are not on the windows message queue, so it is doubtful you would ever be able to receive those messages. Ben

      1 Reply Last reply
      0
      • S SteveL12234

        How does one receive events in a C# windows service from a COM object that has been instantiated from a background thread whose ApartmentState == ApartmentState.STA? I’ve been able to received events from the COM object in a WinForm app when the thread sits in a loop sleeping and invoking System.Windows.Forms.Application.DoEvents(), i.e. pumping the message queue. The thread that accesses the COM object is responsible for responding to requests by other objects to access the COM object, done through a queue + AutoResetEvent, and for handling events raised by the COM object in response to those requests. Other posts/web sites have mentioned that STA threads require a message loop/pump. It seems like if the thread that accesses the COM object were able to call a method that could sleep + message pump + respond to requests to wake up, I’d be set. Is there such a beast/can such a thing be created? Is this the right kind of design? Thanks, Steve

        S Offline
        S Offline
        SteveL12234
        wrote on last edited by
        #3

        With some help from a coworker, I figured out how to do this. The message loop can be run on the same thread as the COM object using ApplicationContext and Application.Run. Other objects can communicate to the thread via Invoke on a dummy Control created on the message loop thread. using System; using System.Diagnostics; using System.Collections.Generic; using System.Text; using System.Threading; using System.Windows.Forms; namespace TestMessageLoopWindowsService { internal class TestMessageLoop { internal void Start(EventLog eventLog) { if (eventLog == null) throw new ArgumentNullException("eventLog"); this.eventLog = eventLog; Thread messageLoopThread = new Thread(MessageLoopThread); messageLoopThread.Name = "Message Loop Thread"; messageLoopThread.SetApartmentState(ApartmentState.STA); messageLoopThread.Start(); Thread invokeOnToMessageLoopThread = new Thread(Invoker); invokeOnToMessageLoopThread.Name = "Invoker"; invokeOnToMessageLoopThread.IsBackground = false; invokeOnToMessageLoopThread.Start(); } private EventLog eventLog; private void MessageLoopThread() { ApplicationContext context = new ApplicationContext(); control = new Control(); control.HandleCreated += new EventHandler(control_HandleCreated); control.CreateControl(); Application.Run(context); } private Control control; private void control_HandleCreated(object sender, EventArgs e) { okToInvoke = true; } private volatile bool okToInvoke; private void Invoker() { while (true) { if (okToInvoke) { EventHandler method = new EventHandler(InvokedOnMessageLoop); control.Invoke(method); } Thread.Sleep(1000); } } private void InvokedOnMessageLoop(object sender, EventArgs e) { eventLog.WriteEntry("InvokedOnMessageLoop runs on " + Thread.CurrentThread.Name); } } }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups