main thread is lagging...

Posted on 2004-11-27
Last Modified: 2010-04-17
Hello experts,
I have a generic question for you. I recently set up a timer to capture my desktop and then outout it to a file, and in the same procuedure I'm also converting the old format to a newer format then deleting the old formatted file. This heavily weighs down my main VCL thread and I've tried both the sleep function and have even written a delay function using MsgWaitForMultipleObjects. Both seem to not help a whole lot and I was wondering if I should consider possibly creating a seperate thread after the screen capture is taken then add the convert function and delete function in the execution section of the newly created thread? Do you think this would help in solving this system lag? The mouse cursor doesnt skip a beat, but when you're typing for example it halts the keys for a brief number of milliseconds, sometimes even for a second or more. Application.ProcessMessages() doesn't seem to solve it either.

 I'm open to any coding ideas or suggestions.

Question by:alloc
    LVL 7

    Expert Comment

    It lags because the main thread does not process any input messages while your timer routine is executed.

    However, saving a screen shot to disk hsould be possible without notable delay on a reasonably good box. A separate thread could be the final solution, but for a regular background service you should first try to trim it's CPU load.

    Take a time measure of the routine at the timer. Split up the times you need for acquiring the image, converting the format (whatever that is - please provide details), and deleting the file, and post the results here. Take time measures for a release build. I assume it's the format conversion.

    LVL 11

    Expert Comment

    you have to go for mutliple threads. if you are just using a single thread then there will be a lag. especially because you are dealing with image processing ( i guess ). May be you should start a new thread whenever a snapshot is to be taken or adjust your timer. This way you will have overlapping threads (number of threads will depend upon your timer interval). So you will have to take care of synchronization issues also. But there will not be any lag because the main app thread is not locked up waiting for resources. Make sure that your main thread doesnt use a sleep or one of the msgwaitfor functions, that will basically lock your main thread ( leading to app not responding ).

    Have Fun!

    Author Comment

    Yes, whats locking-up the main thread is the actual image conversion, from BMP to any other format etc... It makes sense, you may have a ~3mb bitmap image and then it's converted on-the-fly downto an image with better quality and is only 50 kb or less. The actual screenshot process isn't suspending the main thread whatsoever, nor is the older image format deletion (once converted). I imagine I'll create a seperate thread when the timer meets the OnTimer event and have it handle the conversion process in the execute portion of the thread. Are there any decent thread examples online in which splitting up tasks and making them independent of the main thread, which is what their purpose is? I'd only need to create 1 thread seperate from my main VCL thread.

    LVL 11

    Accepted Solution

    heres a pseudo code

    case WM_TIMER:

    exit thread

    come to think of it, its a fairly simple logic. reference to CreateThread can be found in MSDN. Thats the only thing that you would probably need to look up for this. Make sure you are not using any static or global variables inside your SpawnThread functions.

    Let me know if you face any problems, ill see if i can come up with a skeleton code.

    Have Fun!

    Author Comment

    Thanks, I was thinking the same thing. Ive researched CreateThread and BeginThread.


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    java ^ examples 8 51
    wordsWithout 49 64
    mapShare challenge 13 42
    base64 decode encode 12 55
    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    754 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

    16 Experts available now in Live!

    Get 1:1 Help Now