API Support Forum
OEC API > API Support > API & Windows Service
Author Topic: API & Windows Service
(6 messages, Page 1 of 1)
Moderators: VPfau
FFred58
Posts: 8
Joined: May 19, 2009


Posted: Oct 22, 2009 @ 01:34 PM             Msg. 1 of 6
I'm hoping someone has come across this problem before or knows what I would need to do.

I have a very simple application sample for now that runs just fine as a windows form. It simply attempts to connect to the api server, logs that it connected from the OnLoginComplete event (or that it failed from the OnLoginFailed event) and then disconnects.

I take the same code (basically replacing msg boxes with event log entries) but my application is now a windows service (running as local system account).

I know the event writing works because the OnStart event writes to the event log. The problem is that I am not receiving any events after I call the Global.Connect method. Neither a OnLoginComplete or a OnLoginFailed event is fired.

Does anyone have any ideas?
Thanks.

Fred Baptiste
SergeK
-Developer-
Posts: 475
Joined: Jan 26, 2007


Posted: Oct 22, 2009 @ 02:02 PM             Msg. 2 of 6
You have to run windows message loop in the thread created OECClient instance in order to receive API events.
FFred58
Posts: 8
Joined: May 19, 2009


Posted: Oct 22, 2009 @ 02:33 PM             Msg. 3 of 6
Thanks, but I'm not entirely sure what you mean.

Do you mean I have to trap all windows messages and then pull the messages I need to capture and raise that back to the main thread...

Also, I am creating the OECClient object on the main thread, not a separate thread, just as I do it in my standard windows form...

Can you provide me more detail or maybe just a link to something relevant?

Thanks!!

Fred Baptiste
SergeK
-Developer-
Posts: 475
Joined: Jan 26, 2007


Posted: Oct 22, 2009 @ 04:15 PM             Msg. 4 of 6
the thread which creates OECClient should run standard message loop - either by calling Application.Run or by processing messages manually with PeekMessage/TranslateMessage/DispatchMessage.
FFred58
Posts: 8
Joined: May 19, 2009


Posted: Oct 22, 2009 @ 05:54 PM             Msg. 5 of 6
I am not creating the OEClient object on a different thread. I am creating it on the main thead.

There is no Application context since this is a windows service, not a WinForms application.



Here is a code snippet:

Private _OECClient As OEC.API.OECClient

Protected Overrides Sub OnStart(ByVal args() As String)
HookupEventLog()

WriteEvent("Service Starting...", EventLogEntryType.Information)

_OECClient = New OEC.API.OECClient()
AddHandler _OECClient.OnLoginComplete, AddressOf OECClient_OnLoginComplete
AddHandler _OECClient.OnLoginFailed, AddressOf OECClient_OnLoginFailed
AddHandler _OECClient.OnDisconnected, AddressOf OECClient_Disconnected

_OECClient.Connect(My.Settings.OECServer, My.Settings.OECServerPort, My.Settings.OECLoginID, My.Settings.OECPwd, False)
End Sub


Private Sub OECClient_OnLoginComplete()
WriteEvent("Connected...", EventLogEntryType.Information)
End Sub

Private Sub OECClient_OnLoginFailed(ByVal Reason As OEC.Data.FailReason)
WriteEvent(String.Format("Login Failed: {0}", Reason), EventLogEntryType.Error)
End Sub

Private Sub OECClient_Disconnected(ByVal Unexpected As Boolean)
WriteEvent(String.Format("Disconnected: Unexpected={0}", Unexpected), EventLogEntryType.Warning)
End Sub


Thanks.

Fred Baptiste
SergeK
-Developer-
Posts: 475
Joined: Jan 26, 2007


Posted: Oct 23, 2009 @ 08:21 AM             Msg. 6 of 6
Since you can't change service controller thread, which calls your onStart method, you need to create your own thread for OECClient, and have message loop in it. You can use application context even in service and console applications, if you do not want to create custom message loop.