473,320 Members | 2,094 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

C#: console Program/ windows service: low CPU n Memory usage

750 Expert 512MB
I have a console program.. which i need to convert to a windows service...
My basic requirement is that the program needs to run in background and consume as less memory n CPU time as possible..I am not a expert in threading .. my guess was to create threads n execute them
to give an example :
Expand|Select|Wrap|Line Numbers
  1. public static Thread t2;
  2. public static Thread t11;
  3.  
i declared 2 static threads : which will call 2 function on start of service or in main of comsole Program..
Expand|Select|Wrap|Line Numbers
  1. t2 = new Thread(new ThreadStart(this.functionname));          
  2.  
  3. t2.Priority = ThreadPriority.Lowest;//BelowNormal;
  4.  
  5. t2.Start();
  6.  
Secondly i got a: public static System.Timers.Timer t1;
and a static bool variable
Expand|Select|Wrap|Line Numbers
  1. public static bool timerOn = true;  // To see to it that  "function" called completes before called again
  2.  
  3. public void fun1(object source, ElapsedEventArgs e)
  4. {
  5.             if (timerOn) //static variable to see to that only when one 
  6. // thread comepltes the second thread starts. 
  7.             {
  8.                 Thread tt = new Thread(new ThreadStart(this.fun11));
  9.                 timerOn = false;
  10.                 //CreateFolders();
  11.                 //tt.Priority = ThreadPriority.BelowNormal.;// which is beetter
  12.                 tt.Start();
  13.             }    
  14. }
  15.  
  16. public void fun11()
  17. {
  18.     fun2();
  19.         fun3();
  20.         timeron=true; 
  21. }
  22.  
is this right? am i goin in right direction? I still get my program consuming a good CPU time : sometimes around 20 percent for 4 seconds...

main thing is low cpu usage n low memory consumption...
Aug 29 '08 #1
3 3347
joedeene
583 512MB
how about also making the threads background....

Expand|Select|Wrap|Line Numbers
  1. t2.background = true
does that help ?

and did you figure out how to make it a service, i didnt read it all..

joedeene
Aug 29 '08 #2
PRR
750 Expert 512MB
how about also making the threads background....

Expand|Select|Wrap|Line Numbers
  1. t2.background = true
does that help ?

and did you figure out how to make it a service, i didnt read it all..

joedeene
Thanks for that.. i had forgotten to add that .... guess it may make a difference... i wanna know how to add threading to console/service program to optimize performance....(given the fact that the code is fairly optimized)
Making a console to service is not a issue... i have already done that before..
Sep 1 '08 #3
vekipeki
229 Expert 100+
Setting a thread's Background property to true does not affect its priority, it only tells CLR to kill it if is still running after your entry thread has ended.

The CPU time will depend on your fun2 and fun3 methods, which are not listed so it is hard to tell what is actually going on. If their priority is low or high, the CPU will still execute them if there is nothing else to do.

If you want to pause your low-priority threads to have low CPU during all times, you can add
Expand|Select|Wrap|Line Numbers
  1. System.Threading.Thread.Sleep(ms); // where ms is the minimum number of milliseconds to wait before continuing
inside your worker loops.
Sep 1 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: ulysses | last post by:
hi, I'm working in python 5 months. I think it's very cool language. I do a p2p python program GUI. First I make a software by wxpython. But I find wxpython use many many memory. Second I use...
2
by: Mikael Sorensen | last post by:
I've written a windows service (VB.NET 2003). When running, it consumes between 20-40 MB memory. How do I optimize the memory usage? I've set all objects to Nothing when I'm done using them. ...
1
by: Samuel R. Neff | last post by:
We're using a 3rd party C DLL in a project that we don't have source for. When we call the DLL from a console app everything works fine. However, when we call it from a Windows Service, the DLL...
6
by: carbon_dragon | last post by:
Ok, so here is the problem. I'm working on a headless server program implemented as a .NET C# Console project. There is a UPS mounted to this server (though not a windows compliant UPS). I can only...
0
by: Maxwell | last post by:
Hello, I recently completed a MC++ (VS2003) DLL that wraps a non MFC C++ DLL and need to use it in a MC++ Console Application (no forms/guis of any kind just output to console). Trouble is...
7
by: HeatherS | last post by:
We are having issues with our windows services using memory and never releasing it. We have one service that has a file watcher which takes an xml file, inserts some records into a database, and...
1
by: John Wright | last post by:
I am running a console application that connects to an Access database (8 million rows) and converts it to a text file and then cleans and compacts the database. When it runs I get the following...
15
by: Scott M. | last post by:
Hi, I'm an old VB6 guy just starting out in VB.Net using Visual Studio Express. I want to build a simple console app that will create a simple text file every 10 minutes. I can create the file...
12
by: Dilip | last post by:
Hi All I have a server based C# console application. This application must hide its console window when its launched out on the field. So I dutifully P/Invoke'd FindWindow/ShowWindow...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.