Help in Recalling api in same session

Moderator: Moderator

Help in Recalling api in same session

Postby Mohamed20884 » Tue Jan 10, 2017 12:23 pm

Hi I am new to using this api and i used the samples provided in this link http://fxcodebase.com/bin/forexconnect/ ... 64-COM.exe
however i couldn't figure out how i can recall the api without having to repeat the whole process again.

Code: Select all
O2GSession session = null;

                try
                {
                    session = O2GTransport.createSession();
                    SessionStatusListener statusListener = new SessionStatusListener(session);
                    session.subscribeSessionStatus(statusListener);
                    statusListener.Reset();
                    session.login("id", "password", "http://www.fxcorporate.com/Hosts.jsp", "Real");
                    if (statusListener.WaitEvents() && statusListener.Connected)
                    {
                        ResponseListener responseListener = new ResponseListener(session);
                        session.subscribeResponse(responseListener);

                        O2GLoginRules loginRules = session.getLoginRules();
                        if (loginRules == null)
                        {
                            throw new Exception("Cannot get login rules");
                        }

                        O2GResponse response;
                        if (loginRules.isTableLoadedByDefault(O2GTableType.Offers))
                        {
                            response = loginRules.getTableRefreshResponse(O2GTableType.Offers);
                            if (response != null)
                            {
                                responseListener.PrintOffers(session, response, null);
                            }
                        }
                        else
                        {
                            O2GRequestFactory requestFactory = session.getRequestFactory();
                            if (requestFactory != null)
                            {
                                O2GRequest offersRequest = requestFactory.createRefreshTableRequest(O2GTableType.Offers);
                                responseListener.SetRequestID(offersRequest.RequestID);
                                session.sendRequest(offersRequest);
                                if (!responseListener.WaitEvents())
                                {
                                    throw new Exception("Response waiting timeout expired");
                                }
                                response = responseListener.GetResponse();
                                if (response != null)
                                {
                                    responseListener.PrintOffers(session, response, null);
                                }
                            }
                        }

                        // Do nothing 10 seconds, let offers print
                        Thread.Sleep(1000);
                        Console.WriteLine("Done!");

                        session.unsubscribeResponse(responseListener);
                        statusListener.Reset();
                        session.logout();
                        statusListener.WaitEvents();
                    }
                    session.unsubscribeSessionStatus(statusListener);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception: {0}", e.ToString());
                }
                finally
                {
                    if (session != null)
                    {
                        session.Dispose();
                    }
                }
Mohamed20884
 
Posts: 1
Joined: Mon Jan 09, 2017 9:54 pm

Re: Help in Recalling api in same session

Postby 80sKid » Fri Feb 10, 2017 4:13 pm

Most of the sample code for the API follows the same pattern.
1) Creates session objects
2) Logs in to account
3) Performs some function the sample is trying to demonstrate. (Like getting historical bars)
4) Log out and dispose session objects.

You just need to move the code to log out and dispose objects to a different sub or function somewhere else in your code. Then you call that sub/function later when you need to.

Looking at your posted example, put this code somewhere else in your application and call it when you would like to logout/exit/end your program.

session.unsubscribeResponse(responseListener);
statusListener.Reset();
session.logout();
statusListener.WaitEvents();
}
session.unsubscribeSessionStatus(statusListener);

if (session != null)
{
session.Dispose();
}
80sKid
 
Posts: 2
Joined: Fri Feb 10, 2017 3:56 pm


Return to ForexConnect API

Who is online

Users browsing this forum: No registered users and 7 guests