API Support Forum
OEC API > API Support > OECapi inside a DLL (No UI thread)
Author Topic: OECapi inside a DLL (No UI thread)
(7 messages, Page 1 of 1)
Moderators: VPfau
MFrasson3344
Posts: 45
Joined: Nov 09, 2015


Posted: Jan 26, 2016 @ 12:53 PM             Msg. 1 of 7
Hello,

i'm tring to use OECapi client inside a DLL on a different thread (not the UI), but it does not work.
It works only if i use the API in the UI (user interface) thread. It is correct? or where could be the problem?

Thanks

Mauro frasson
VPfau
Moderator
Posts: 164
Joined:


Posted: Jan 26, 2016 @ 01:42 PM             Msg. 2 of 7
Hello Mauro,

our documentation (http://futures.gaincapital.com/api/api/index.html) describes your situation in FAQ section

Quote: I’ve started OECAPI in console application and do not receive any events. Why?

Thread that created OEC.API.OECClient should have Windows message pump. WinForms applications have the pump implicitly in the main UI thread.


Also there Getting Started\Connect to OEC section with console sample which could be helpful for you.

Vitaliy Pfau
MFrasson3344
Posts: 45
Joined: Nov 09, 2015


Posted: Jan 26, 2016 @ 01:50 PM             Msg. 3 of 7
Really is there no way to bypass the UI thread? if no i have to build a separate exe and then use a pipe to comunicate with main program.. and it is horrible!



Mauro frasson
Edited by MFrasson3344 on Jan 26, 2016 at 13:51:05
Edited by MFrasson3344 on Jan 26, 2016 at 13:51:36
VPfau
Moderator
Posts: 164
Joined:


Posted: Jan 26, 2016 @ 01:56 PM             Msg. 4 of 7
You don't have to "build a separate exe" and communicate with main program. OECAPI uses standard windows message pump for events delivery. You can start it without any UI thread.

Please follow links I posted.

Vitaliy Pfau
MFrasson3344
Posts: 45
Joined: Nov 09, 2015


Posted: Jan 26, 2016 @ 02:28 PM             Msg. 5 of 7
Ok, sorry. I try to explain better. I used the wrong word "UI", but "UI" means "the main thread".

So the situation is this:

1) I have a main thread
2) I have a lot of secondary tasks/threads
3) one of these threads create an instance using a class placed on a separate DLL.
4) into this class i create the OECapi client.

The problem is that the thread where i create the client is not the main thread so i do not receive the events.

Mauro frasson
VPfau
Moderator
Posts: 164
Joined:


Posted: Jan 26, 2016 @ 03:22 PM             Msg. 6 of 7
Here is couple ways to solve your issue:

    1. you are going to run windows message pump and receive messages there, then, if needed, post them to your thread.

    For example from main thread:

    oecapi.OnLoginComplete += new OEC.API.OnLoginCompleteEvent(oecapi_OnLoginComplete);
    oecapi.OnLoginFailed += new OEC.API.OnLoginFailedEvent(oecapi_OnLoginFailed);
    oecapi.OnDisconnected += new OEC.API.OnDisconnectedEvent(oecapi_OnDisconnected);
    System.Windows.Forms.Application.Run();

    2. Create OECClient with ThreadPolicy

    [EditorBrowsable(EditorBrowsableState.Never)]
    public OECClient(ThreadingPolicy threadingPolicy)

    this is undocumented ability and you accept risks using this. All incoming events will go thru Send\Post functions of threading policy. Be careful, blocking the thread from Send\Post functions for a long time will result slow connection disconnect.


So, to avoid slow connection disconnects you need pump messages whatever way you choose. I think it is better use a standard supported method.

Vitaliy Pfau
MFrasson3344
Posts: 45
Joined: Nov 09, 2015


Posted: Jan 26, 2016 @ 05:03 PM             Msg. 7 of 7
Ok thank you.

I think i'll run a secondary thread from my class using your first solution.

Mauro frasson