What is the correct way to slow down a Winform app?

S

sune42

For my ASP.NET website , I have a winform application that runs once a
day
doing various household tasks. As these tasks are quite DB/CPU
intensive I
use

Thread.Sleep(XX);
System.Windows.Forms.Application.DoEvents();

To slow it down. Why? I don't want this application to affect the
performance
of my ASP.NET website or other application that might be running
.. Also I don't care how long it takes.

But I suspect that the above is perhaps not the best way of slowing it
down,
so I wonder if anyone got any better ideas?


Andy
 
G

Guest

For my ASP.NET website , I have a winform application that runs once a day
doing various household tasks. As these tasks are quite DB/CPU intensive I use

Thread.Sleep(XX);
System.Windows.Forms.Application.DoEvents();

To slow it down. Why? I don't want this application to affect the performance
of my ASP.NET website or other application that might be running
.. Also I don't care how long it takes.

But I suspect that the above is perhaps not the best way of slowing it
down, so I wonder if anyone got any better ideas?

Perhaps you could lower the application's priority via the Windows Task
Manager.
 
A

Alexander Ubillus

System.Windows.Forms.Application is as you can see, for WinForms
applications. If you are in an ASP.NET context. You should probably use
Threads and define them as Low Priority or Sleeping the thread.
 
S

sune42

Alexander said:
System.Windows.Forms.Application is as you can see, for WinForms
applications. If you are in an ASP.NET context. You should probably use
Threads and define them as Low Priority or Sleeping the thread.

I am not using System.Windows.Forms.Application.DoEvents(); in a
ASP.NET context, its a winform app running once a day to do cleanup
tasks.

But I don't want the WinformApp to hurt the performance of my ASP.NET
site. That's why I want to slow it down.
 
A

Alexander Ubillus

Well, in fact there isn't a way to tell a Thread to consume only XX percent
of the CPU. The most you can do is to set the worker thread priority to Low
and eventually
Sleep the thread (although lowing its priority must be enough).

On the other hand, calling the DoEvents method is useless if you don't have
a UI.
(That's what i can understand of your winapp that happens to sound like a
WinService)


<[email protected]> escribió en el mensaje

Alexander said:
System.Windows.Forms.Application is as you can see, for WinForms
applications. If you are in an ASP.NET context. You should probably use
Threads and define them as Low Priority or Sleeping the thread.

I am not using System.Windows.Forms.Application.DoEvents(); in a
ASP.NET context, its a winform app running once a day to do cleanup
tasks.

But I don't want the WinformApp to hurt the performance of my ASP.NET
site. That's why I want to slow it down.
 
J

Jon Skeet [C# MVP]

For my ASP.NET website , I have a winform application that runs once a
day doing various household tasks. As these tasks are quite DB/CPU
intensive I use

Thread.Sleep(XX);
System.Windows.Forms.Application.DoEvents();

To slow it down. Why? I don't want this application to affect the
performance of my ASP.NET website or other application that might be running
. Also I don't care how long it takes.

But I suspect that the above is perhaps not the best way of slowing it
down, so I wonder if anyone got any better ideas?

Could you give us a bit more information about what the application
does? Is it a case of pressing a button to start it, and then it just
runs through all the tasks? If so, I'd suggest doing the tasks
synchronously in another thread with a call to Sleep between each task.
 
S

sune42

what I do is generating search index, pre-generating data, collect
statistics, cleanup of data..... So pretty DB and IO intensive.

So, I do
Appplication.DoEvents()
to get a resposive UI and
thread.sleep to introduce a delay.

Is this combination the best way to introduce a delay in a winform
application?

Or are there better ways or more correct ways?

Creating a separate thread and giving it a low priority could perhaps
be a good idea?
 
J

Jon Skeet [C# MVP]

what I do is generating search index, pre-generating data, collect
statistics, cleanup of data..... So pretty DB and IO intensive.

So, I do
Appplication.DoEvents()
to get a resposive UI and
thread.sleep to introduce a delay.

Is this combination the best way to introduce a delay in a winform
application?

No. You won't actually have a responsive UI when it's either sleeping
or executing a long-running task.
Or are there better ways or more correct ways?

Yes, use multithreading.
Creating a separate thread and giving it a low priority could perhaps
be a good idea?

Giving it a low priority won't really help when it's hammering your
database in a separate process.

Regularly calling Sleep will spread the load out a bit (into more lumps
instead of one big one), but that's all, really. It'll still be lumpy
rather than smoothly using a small amount of your machine for the whole
day.

It sounds like what you *really* want is some way of telling the
database and the rest of the machine "I'm not important - don't waste
too much energy on me." There's no way I know of to reduce your IO
priority, or tell the database to make your requests low priority. (The
latter *may* have a way of doing it, admittedly - worth looking into.)

Using task manager, check how much CPU time your app consumes over the
course of a run. That would give us some idea about whether priorities
will actually make any difference.
 
G

Guest

I'd suggest creating a windows service with a System.Threading.Timer (NOT!!!
a System.Windows.Forms.Timer!!! It will not work without a message pump
running behind a form!) rather than a form app. In the service's OnStart
event, instantiate your timer and set it to fire when you want to do the
cleanup.

When the timer fires and runs the TimerCallback method you gave it, execute
your clean up using an instance of Thread that is configured to be
low-priority. In the clean-up method, if you enter into any tight loops, use
Thread.Sleep() to allow the CPU to block your clean-up thread and do some
other processing.

If your clean-up code needs to block your ASP.NET webpage while it updates
files, use a Mutex or similar object, which can synchronize threads across
processes.

Async operations are pretty complex; if you are serious about doing this
properly, you should grab a book that goes into detail about .NET
multithreading and read up on it. You can get yourself into some bad trouble
if you don't do it right. I'd suggest 'CLR Via C#', which is a great
resource for most everything .NET related.
 

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