How do I timeout a line in my code?

Posted on 2005-05-03
Last Modified: 2013-11-20

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.

Question by:StanChart
    LVL 86

    Accepted Solution

    This would indeed be an idea, however a thread would be required for the DDE connection, e.g.


    HANDLE hEvent = (HANDLE) p;

    HCONV hconv = DDEConnect ( ...);



    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);

    Author Comment

    Hi jkr,

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

    LVL 86

    Expert Comment

    No. Since there is no way to establish a connection asynchronously wit DDE, you'll have to go that route.

    Author Comment

    Thanks again.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
    If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now