Program Flow, pausing a function?
Hi All,
I have a strange situation, im not sure if its even possible what i have to do hence the post. I cannot think of any way this problem can be solved however, i've been wrong before so best to check with you all. :)
I have a function "Connect" which calls another function "MakeConnection". MakeConnection returns imeditaly and its success or failure is determined by a callback to "OnConnected" or "OnDisconnected".
This is were things start to get tricky, the Connect function needs to return true or false. If MakeConnection returns imeditaly how can i determine what the result should be before i return from the Connect function.
Code:
BOOL g_bConnected = FALSE;
BOOL g_bDisconnected = FALSE;
BOOL Connect(...)
{
MakeConnection(...);
// Need to "pause" for either the OnConnected or OnDisconnected
// callback so i can get the result of MakeConnection
return TRUE/FALSE??
}
callback void OnConnected(...)
{
g_bConnected = TRUE;
}
callback void OnDisconnected(...)
{
g_bDisconnected = TRUE;
}
The problem is pausing after MakeConnection means the callbacks themselves cant execute anyway, so its stuck..
I've thought about differnt threadding ideas but non of them actully solve the problem.. :S
Any ideas or is this impossible as i currently think? :)
Re: Program Flow, pausing a function?
Depending on who is invoking your callback you need one or two threads in your code.
Either way your callback function should set a result flag ( the result of the connect operation) and set an event ( or any other synchronization object). The Connect function should be waiting for the event to be set and read the flag set by the callback.
Re: Program Flow, pausing a function?
It's early in the morning but anyway...
My first thought is that Connect() shouldn't really return anything, instead connect status could be read when needed by other calls. Don't really know what signalling that is done by your connection code but even if you wait on first OnConnected callback is there a guarantee that OnDisconnected haven't been called (due to some other event that produce an error) before you actually do something with the status returned from Connect()?
I must say that I have a feeling that some redesign (using proper synchronization objects) could be beneficial but anyway:
Adding a poll loop isn't the most elegant solution but maybe sufficient for your needs.
Code:
DWORD start = GetTickCount();
do {
if( g_bConnected ) return TRUE;
Sleep(0); // Give up the remainder of the time slice
} while( GetTickCount() - start < TIMEOUT_IN_MS );
return FALSE;
Why have two BOOL variables by the way? You would expect them to always be the inverse of each other don't you?
Re: Program Flow, pausing a function?
Thank you both for your replys :)
Quote:
Depending on who is invoking your callback you need one or two threads in your code.
I have a third party dll that is loaded in the application, initilised etc, this has the "MakeConnection" and the "SetCallback" functions for connection and disconnection.
Quote:
instead connect status could be read when needed by other calls.
I must say that I have a feeling that some redesign (using proper synchronization objects) could be beneficial
It is possible to get the connection status however, the programe flow is out of my control, i am simply implimenting an interface that is provided to me, hence the need to return correctly if the connection was successfull or not.
I hope thats clear? If not ill try and expand my explanation
Thanks for the help so far :)