• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

How do I timeout a line in my code?

Hi,

I have a line of code that connects to a DDE service/topic.  Most of the time, the connection is instant however sometimes it can hang forever.  I don't know why it's hanging and don't want to spend hours figuring out why either.  I wanted to know if there is a way that I can wait for a maximum of 5 seconds for this line of code to execute.  After 5 seconds, it should timeout and I'll handle the timeout event appropriately.

I was thinking of using events and WaitForSingleObject but don't know how to go about doing it.

Appreciate your help.

Cheers
0
StanChart
Asked:
StanChart
  • 2
  • 2
1 Solution
 
jkrCommented:
This would indeed be an idea, however a thread would be required for the DDE connection, e.g.

LONG WINAPI DDEThread ( LPVOID p) {

HANDLE hEvent = (HANDLE) p;

HCONV hconv = DDEConnect ( ...);

SetEvent(hEvent);

//...

return 0;
}


//..................

HANDLE hEvent = CreateEvent ( NULL, FALSE, FALSE, NULL);

DWORD dwTID;
HANDLE hThread = CreateThread(NULL, 0, DDEThread, (LPVOID) hEvent, 0, &dwTID);

DWORD dwResult = WaitForSingleObject ( hEvent, 5000); // wait 5s

if ( WAIT_TIMEOUT == dwResult) TerminateThread ( hThread); // kill thread after 5s w/o success

CloseHandle ( hEvent);
0
 
StanChartAuthor Commented:
Hi jkr,

Thanks for your reply.  Is there any way of doing this without creating a new thread?

Thanks
0
 
jkrCommented:
No. Since there is no way to establish a connection asynchronously wit DDE, you'll have to go that route.
0
 
StanChartAuthor Commented:
Thanks again.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now