Question

This is a discussion on Question within the C++ Programming forums, part of the General Programming Boards category; All this confusion over sleep or Sleep. Just use mesleep Code: #include <ctime> void mesleep ( long m ) { ...

  1. #16
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    All this confusion over sleep or Sleep. Just use mesleep
    Code:
    #include <ctime>
    void mesleep ( long m )
    {
      clock_t limit, 
              c = clock();
      limit = c + m;
      while ( limit > c )
        c = clock();
    }
    -Prelude
    My best code is written with the delete key.

  2. #17
    Registered User
    Join Date
    Mar 2002
    Posts
    51
    I've tried your code using "console application"...

    The problem is still there.....

    I don't get the cout "My display is late" until after the Sleep command...hmm..weird huh?

    When I run the program, the dos screen is blank,..then some secs later, two display of "My display is late" is printed instantaneously to the screen....
    Which is the master, which is the student?

  3. #18
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I don't get the cout "My display is late" until after the Sleep command...hmm..weird huh
    Not really, buffered [edit]output[/edit] is only flushed by the system when it's convenient. If you want to flush right away then tell the compiler to do so.
    cout<<"My message"<<flush;

    -Prelude
    My best code is written with the delete key.

  4. #19
    Registered User
    Join Date
    Mar 2002
    Posts
    51
    Hay Prelude:

    Which Japanese animation is that good looking chic next to your name from? I'm a big fan of Japanimation...my favorite is Rurouni Kenshin..or Sarmuri X (is what they call it in the US)...
    Which is the master, which is the student?

  5. #20
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Which Japanese animation is that good looking chic next to your name from?
    http://www.polykarbon.com/
    I can't draw to save my life, but I can admire other people's work, right?

    -Prelude
    My best code is written with the delete key.

  6. #21
    Registered User
    Join Date
    Mar 2002
    Posts
    51
    AH...Prelude's THE MAN!

    That flush command was what I was looking for ...
    Now cout prints to the screen before going to sleep and my problem is solved!

    btw,..I have another question...

    If i was to just print "hello world" on a "Win32" application..what files do I have to include??

    Can you give me the exact code that I'll need...because i'm getting errors when I use the code for "console application" that would print "hello world"..
    Which is the master, which is the student?

  7. #22
    Registered User
    Join Date
    Mar 2002
    Posts
    51
    One more question if you don't mind

    Let say you've started a project on "console application",...can you change it to win32 application later on?? Is there any reason why someone would want to change it and would the code be compatible??

    Curiousity saved the cat...
    Last edited by Jez_Master; 04-07-2002 at 08:22 PM.
    Which is the master, which is the student?

  8. #23
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Can you give me the exact code that I'll need
    It depends on how complex you want the code to be. If all you want to do is print "Hello, World!" then it's almost as simple as a console app.
    Code:
    #include <windows.h> 
    
    int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nShow) 
    { 
      MessageBox (NULL, "Hello, World!", "Hello", MB_OK); 
      return 0; 
    }
    If you want to create a window and print something in the client area then it is considerably more difficult.
    Code:
    #include <windows.h>
    
    const char g_szClassName[] = "window one";
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        HDC         hdc;
        PAINTSTRUCT ps;
        RECT        rect;
        switch(msg)
        {
        case WM_PAINT:
            hdc = BeginPaint ( hwnd, &ps );
            GetClientRect ( hwnd, &rect );
            DrawText ( hdc, TEXT ( "Hello, World!" ), -1, &rect, DT_SINGLELINE );
            EndPaint ( hwnd, &ps );
            break;
        case WM_CLOSE:
            DestroyWindow(hwnd);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
        LPSTR lpCmdLine, int nCmdShow)
    {
        WNDCLASSEX wc;
        HWND hwnd;
        MSG Msg;
    
        wc.cbSize        = sizeof(WNDCLASSEX);
        wc.style         = 0;
        wc.lpfnWndProc   = WndProc;
        wc.cbClsExtra    = 0;
        wc.cbWndExtra    = 0;
        wc.hInstance     = hInstance;
        wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
        wc.lpszMenuName  = NULL;
        wc.lpszClassName = g_szClassName;
        wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
        if(!RegisterClassEx(&wc))
        {
            MessageBox(NULL, "Window Registration Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        hwnd = CreateWindowEx(
            WS_EX_CLIENTEDGE,
            g_szClassName,
            "Some Title",
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
            NULL, NULL, hInstance, NULL);
    
        if(hwnd == NULL)
        {
            MessageBox(NULL, "Window Creation Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
    
        while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        return Msg.wParam;
    }
    >can you change it to win32 application later on??
    Perhaps in the .NET framework, but as far as I know you have to keep Win32 applications and console applications separate or there will be linker errors.

    -Prelude
    My best code is written with the delete key.

  9. #24
    Registered User heat511's Avatar
    Join Date
    Dec 2001
    Posts
    169

    ummmmmm, this works too :)

    Code:
    main()
    {
    //this should work
    cout << "This should show this then sleep << endl;
    Sleep(1000);
    cout << "It waited, yay!" << endl;
    }
    ummmmm, tell me if this doesn't work
    it should i think

  10. #25
    Registered User
    Join Date
    Mar 2002
    Posts
    51
    Yes, those code works...

    but my code calls a function in main,..and that's where all the timing problem come into play...

    like prelude suggested, using "flush" works like a charm
    Which is the master, which is the student?

  11. #26
    Registered User
    Join Date
    Sep 2001
    Posts
    305
    when you use cout or whatver, its put into stdout(the buffer) so when you fflush(stdout), the buffer is "flushed". if you use C, you can also flush the file buffer.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21