473,624 Members | 2,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

750 Recognized Expert Contributor
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.T imer 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 3365
joedeene
583 Contributor
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 Recognized Expert Contributor
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 Recognized Expert New Member
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
5012
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 PYQT. But memory use still big. But but but when I minimize the windows to taskbar, a fantasy something happened. memory use very very low when windows minimize.
2
2455
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. What else can i do? Regards, Mikael
1
3380
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 doesn't work (sorry, can't be more descriptive.. no errors, but no results). We've even taken the exact Windows Service project and put code in main() to instantiate the service and call OnStart() instead of going through the service base class. ...
6
8751
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 talk to the UPS over a special device driver. Through this device driver I can detect that the UPS is going to notify Windows 2000 server to shut down. So I start doing a graceful termination. But Windows shuts down pretty quickly and there...
0
1602
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 that when I ran it and looked at memory usage (in Windows task manager) it looked as if there was a very slow leak. To isolate the issue:
7
4030
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 creates a bunch of PDFs with Crystal Reports. Another service is a remote object which serves as our data access component -- basically it just executes stored procedures and returns datasets. If you watch the services in task manager, you can...
1
2037
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 error: The CLR has been unable to transition from COM context 0x1a2008 to COM context 0x1a2178 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long...
15
6713
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 OK, but how do I put in the timer so that I can write the file on schedule? The idea behind this project is to help me monitor remote computers in a production environment to make sure that they are up and running. I currently synchronize files on...
12
6524
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 combination to hide the console window at launch time. The application (for legacy reasons) hangs around by waiting on an old- fashioned Console.ReadLine() statement.
0
8179
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8685
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8633
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7176
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1493
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.