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
Mini-Tools Timm - 29 Jun 2006 17:09 GMT
> 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
[quoted text clipped - 8 lines]
> 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.

Signature
Timm Martin
Mini-Tools
.NET Components and Windows Software
http://www.mini-tools.com
Alexander Ubillus - 29 Jun 2006 17:31 GMT
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.
> For my ASP.NET website , I have a winform application that runs once a
> day
[quoted text clipped - 15 lines]
>
> Andy
sune42@hotmail.com - 29 Jun 2006 17:46 GMT
> System.Windows.Forms.Application is as you can see, for WinForms
> applications. If you are in an ASP.NET context. You should probably use
[quoted text clipped - 19 lines]
> >
> > Andy
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.
Alexander Ubillus - 29 Jun 2006 18:24 GMT
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)
Alexander Ubillus wrote:
> System.Windows.Forms.Application is as you can see, for WinForms
> applications. If you are in an ASP.NET context. You should probably use
[quoted text clipped - 21 lines]
> >
> > Andy
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.
Jon Skeet [C# MVP] - 29 Jun 2006 18:57 GMT
> 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
[quoted text clipped - 9 lines]
> 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.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
sune42@hotmail.com - 29 Jun 2006 22:21 GMT
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?
Jon Skeet [C# MVP] - 29 Jun 2006 22:42 GMT
> what I do is generating search index, pre-generating data, collect
> statistics, cleanup of data..... So pretty DB and IO intensive.
[quoted text clipped - 6 lines]
> 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.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
William Sullivan - 30 Jun 2006 15:50 GMT
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.
> For my ASP.NET website , I have a winform application that runs once a
> day
[quoted text clipped - 15 lines]
>
> Andy