main thread is lagging...

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.

Thanks,
 Alloc
allocAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

peterchen092700Commented:
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.

0
pratap_rCommented:
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!
Pratap
0
allocAuthor Commented:
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.


  Thanks,
Alloc
0
pratap_rCommented:
heres a pseudo code

case WM_TIMER:
        CreateThread(SpawnThread)
         break;

SpawnThread()
{
GetWindowDC
TakeSpanshot
ConvertImage
DeleteFile
SaveImage
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!
Pratap
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
allocAuthor Commented:
Thanks, I was thinking the same thing. Ive researched CreateThread and BeginThread.

Alloc
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.