Thread: How to show the user that something is happening?

  1. #1
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90

    Question How to show the user that something is happening?

    I've written a small command line app and takes a really long time to run (process IIS logs into an SQL DB). How do I make one of those really neat thingys that spin ( \|/- ) while I'm waiting for it to complete so I know it's not locked up. And what are they even called?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You have to use platform dependent code to do that, so it depends on your platform. Basically what you'd do is print out the character, then move the cursor back and print out the next character in the sequence. You would do each character after a certain number of iterations if you're running through a loop, or if you are using threads you can put it in a separate thread and sleep for a half second or however long you want between the image changing.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Actually, something like this would generally be platform independent within a console type environment:
    Code:
    static const progressSigns[] = "/-\|";
    
    int updateProgress()
    {
       static int progress = 0;
       static const progressLen = sizeof(progressSigns) - 1; 
       printf("%c\r", progressSigns[progress]);
       fflush(stdout);
       progress = (progress + 1) % progressLen;
    }
    Just call updateProgress on regular intervals (e.g. every thousand records or some such).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    106
    I had the same problem, you could do this : http://cboard.cprogramming.com/showthread.php?t=104638

    , or multithreading if you want to be fancy.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    For something that is relatively simple like this case, you probably create more problems adding multithreading than just doing what I suggested above.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    But if the long operation is blocking, you don't have a choice.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CornedBee View Post
    But if the long operation is blocking, you don't have a choice.
    Yes, but in this particular example (putting IIS logs into a SQL database) should be progressing with fairly regular intervals, one would assume - of course, if the SQL database is seriously remote (or connected via really slow network) then it could take some time.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Plus, you might want to know when it's stuck. If you go with multiple threads, I'd consider using some kind of running record count that so you can tell if work is actually being done or if you status display is "lying".

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by medievalelks View Post
    Plus, you might want to know when it's stuck. If you go with multiple threads, I'd consider using some kind of running record count that so you can tell if work is actually being done or if you status display is "lying".
    Yes, not like my version control system that is currently showing a spinning hourglass, and has been doing so for the last xxx seconds... ;-) I suspect it's got stuck somewhere (and no, I'm not going to tell you which one it is).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by matsp View Post
    Yes, not like my version control system that is currently showing a spinning hourglass, and has been doing so for the last xxx seconds... ;-) I suspect it's got stuck somewhere (and no, I'm not going to tell you which one it is).
    Does it rhyme with Bisual Bource Bafe?

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by medievalelks View Post
    Does it rhyme with Bisual Bource Bafe?
    Not this time... I have used that one, it's pretty basic, but was OK until we tried to share the source code across the WAN to another quite distant (4 figure miles away) office...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    Yeah, I don't want to multi-thread because I wouldn't be able to tell if it's stuck or not. Turns out the program only took 6 hours to do what I wanted. :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. user menu
    By tat in forum C Programming
    Replies: 5
    Last Post: 11-12-2007, 01:03 AM
  2. Windows startup - must click user before continuing
    By ulillillia in forum Tech Board
    Replies: 2
    Last Post: 04-30-2007, 03:06 PM
  3. Replies: 4
    Last Post: 04-21-2004, 04:18 PM
  4. ~ User Input script help~
    By indy in forum C Programming
    Replies: 4
    Last Post: 12-02-2003, 06:01 AM
  5. Stopping a user from typeing.
    By knave in forum C++ Programming
    Replies: 4
    Last Post: 09-10-2001, 12:21 PM