API Support Forum
OEC API > API Support > OECClient in other thread
Author Topic: OECClient in other thread
(7 messages, Page 1 of 1)
Moderators: VPfau
VVadim41
Posts: 12
Joined: Mar 22, 2010


Posted: Aug 18, 2010 @ 02:07 PM             Msg. 1 of 7
Hello.
I'm writing my programm and I want to add OECClient in other thread.
I trying to do something like code on bottom, but callbacks not works...
I try use waitHandle.WaitOne and this not works too...
Can you help me with idea?
ps on Main thread all works good of caurse...


public void Thread9()
{
try
{
myDelegate = new AddListItem(AddListItemMethod);
myDelegate2 = new AddLabel(AddLabelMethod);
t9.Name = "Thread9";
if (client == null)
{
client = new OECClient();

client.OnLoginComplete += new OnLoginCompleteEvent(client_OnLoginComplete);
client.OnLoginFailed += new OnLoginFailedEvent(client_OnLoginFailed);
client.OnDisconnected += new OnDisconnectedEvent(client_OnDisconnected);
client.OnError += new OnErrorEvent(client_OnError);
client.OnContractsChanged += new OnContractsChangedEvent(client_OnContractsChanged);
client.OnTicksReceived += new OnTicksReceivedEvent(client_OnTicksReceived);
client.OnDOMChanged += new OnDOMChangedEvent(client_OnDOMChanged);
}

Run9();
}
catch (Exception ex)
{
SaveLog(t9.Name, ex.Message.ToString(), "");
}
}




public void Run9()
{
while (!m_stop9)
{
try
{
if (!OEC_isconnected)
{
client.Connect(host, port, username, password, true);
//Thread.Sleep(100);
}
//other code will be here
//waitHandle.WaitOne();
}
catch (System.Runtime.InteropServices.COMException e)
{
SaveLog(t9.Name, e.Message.ToString(), e.ErrorCode.ToString() + " " + GetError(e.ErrorCode));
}
catch (System.Exception e)
{
SaveLog(t9.Name, e.Message.ToString(), e.Source.ToString());
}
}
}


//callbacks
public void client_OnLoginComplete()
{
// waitHandle.Set();
SaveLog(Thread.CurrentThread.Name, "", "Client connected");
OEC_isconnected = true;
LoadContracts();
}
VictorV
Posts: 746
Joined: May 08, 2007


Posted: Aug 18, 2010 @ 05:07 PM             Msg. 2 of 7
Hello,

You need to add Windows message pump to your non-main thread with OECAPI.

Victor Vins
Software Developer
VVadim41
Posts: 12
Joined: Mar 22, 2010


Posted: Aug 18, 2010 @ 05:29 PM             Msg. 3 of 7
very strange method.
Are there any other way, more elegant?
VictorV
Posts: 746
Joined: May 08, 2007


Posted: Aug 18, 2010 @ 07:58 PM             Msg. 4 of 7
OECAPI is based on Windows messages.

Victor Vins
Software Developer
JBerrojalbiz381
Posts: 9
Joined: Apr 06, 2011


Posted: May 19, 2011 @ 06:00 PM             Msg. 5 of 7
Hi Victor,

Could you provide some advice or code sample for doing this? >> "You need to add Windows message pump to your non-main thread with OECAPI"

Thanks in advance,
Jon.

Jon Berrojalbiz
THarnett81
Posts: 78
Joined: Jan 13, 2011


Posted: May 20, 2011 @ 11:46 AM             Msg. 6 of 7
In C# WPF, the callbacks will occur on the UI event thread. You have to ensure that any UI event handlers do not block, else all your OEC events will queue until your UI event lets go.

See this thread:
http://www.openecry.com/cfbb/index.cfm?page=topic&topicID=497
VictorV
Posts: 746
Joined: May 08, 2007


Posted: May 23, 2011 @ 01:10 PM             Msg. 7 of 7
Hello,

message pump should look like:

class Program
{
static void Main(string[] args)
{
var thread = new Thread(OecApartment) { Name = "OECThread" };
thread.Start();
Console.ReadLine();
}

static void OecApartment(object state)
{
OECClient client = new OECClient();
client.OnLoginComplete += new OnLoginCompleteEvent(client_OnLoginComplete);
client.Connect("api.openecry.com", 9200, "usr", "pwd", false);
while (true)
{
System.Threading.Thread.Sleep(1);
System.Windows.Forms.Application.DoEvents();
}
}

static void client_OnLoginComplete()
{
Console.WriteLine("Hi there! Current thread: {0}", Thread.CurrentThread.Name);
}
}

Victor Vins
Software Developer