Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

main thread is lagging...

Posted on 2004-11-27
5
Medium Priority
?
280 Views
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.

Thanks,
 Alloc
0
Comment
Question by:alloc
  • 2
  • 2
5 Comments
 
LVL 7

Expert Comment

by:peterchen092700
ID: 12689215
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
 
LVL 11

Expert Comment

by:pratap_r
ID: 12689561
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
 

Author Comment

by:alloc
ID: 12691307
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
 
LVL 11

Accepted Solution

by:
pratap_r earned 2000 total points
ID: 12694584
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
 

Author Comment

by:alloc
ID: 12695890
Thanks, I was thinking the same thing. Ive researched CreateThread and BeginThread.

Alloc
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Six Sigma Control Plans

578 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