Sleep thread from Main thread

I

ipramod

Hi,

I am having an application which inserts some data into a table. While
inserting table rows, I want to check CPU percentage and if it is >90%
then I want to sleep insertion for some time and then again resume
it.

For example:

-> Main execution starts
-> Main starts inserting rows into table
-> Check CPU %
-> while ( CPU % > 90 )
-> Sleep(10000)
-> End inserting
-> End Main

Now if I create one thead to insert table values and second thread to
check CPU % from the main thread, then can we sleep the insertion
thread from the main thead? Main thead waits till the insertion
completes.

Please let me know your thoughts or any other way to resolve this
issue.

Regards,
PI
 
A

Alberto Poblacion

[...]
Please let me know your thoughts or any other way to resolve this
issue.

If your purpose is to avoid hogging the CPU with your insertion process,
there is another alternative: You could create a thread to perform the
insertion and set the Thread Priority to "BelowNormal". In this way, when
other processes require the CPU, your thread will automatically "give way"
to them, and then immediately resume when the CPU is no longer busy at other
tasks.
 
Y

Yasir Zaheer

Hi,

Alberto, gave a better alternative solution, you also can use the same
code with Monitor involved, by using Monitor.Wait.... and
Monitor.Pulse.... you can control the thread execution.

Regards,

[...]
Please let me know your thoughts or any other way to resolve this
issue.

If your purpose is to avoid hogging the CPU with your insertion process,
there is another alternative: You could create a thread to perform the
insertion and set the Thread Priority to "BelowNormal". In this way, when
other processes require the CPU, your thread will automatically "give way"
to them, and then immediately resume when the CPU is no longer busy at other
tasks.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Do not try to do the job of Windows, if the OS schedule 90% of CPU for a
particular project it might be the case that no other process is using it in
the first place.

Not only that after you "restart" your thread it will be reach 90% again.
so at the end what you are achieving is that instead of a short burst of 90%
utilization you are having several chunks of high utilization.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


I think that this si a better solution. Still I do not think that the
original situation is a problem in the first place.

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
Alberto Poblacion said:
[...]
Please let me know your thoughts or any other way to resolve this
issue.

If your purpose is to avoid hogging the CPU with your insertion
process, there is another alternative: You could create a thread to
perform the insertion and set the Thread Priority to "BelowNormal". In
this way, when other processes require the CPU, your thread will
automatically "give way" to them, and then immediately resume when the CPU
is no longer busy at other tasks.
 
L

Lew

(Top-posting re-ordered for clarity)

(e-mail address removed) queried:
Do not try to do the job of Windows, if the OS schedule 90% of CPU for a
particular project it might be the case that no other process is using it in
the first place.

Not only that after you "restart" your thread it will be reach 90% again.
so at the end what you are achieving is that instead of a short burst of 90%
utilization you are having several chunks of high utilization.

Furthermore, ipramod, if your action "to insert table values" involves offline
storage, as many operations described as involving "tables" do, then your
thread is extremely unlikely to be CPU-bound in the first place. Other
threads will act while the IO-bound thread is waiting for external resources.
Any attempt to "optimize" (a.k.a, "mess up") the process is at best just
gilding the lily. At a great cost of program complexity, development time and
lifetime maintenance.

Are your "tables" in memory, or offline, as in a database?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top