I wrote a simple application in C#. It downloads a log file via ftp, checked whether firefox is running on the PC, changes the log string, uploads the log back to server.
I am running it every 10 seconds using a Timer.
When the service starts, its memory usage is 10Mb and CPU usage <1%. After about two minutes, its memory usage is ~12Mb, but the CPU usage jumps to over 90%!
This is what my app does every 10 seconds:
1) Download log via ftp and store in string log.
2) Go through a list of processes running on the PC, and if there if a firefox.exe process, appropriately change the log string to indicate firefox running.
3) Save the log string to as a txt file, read the file to send id via ftp back to the server.
I doubt saving/reading a couple of lines of text onto hard-drive requires so much CPU power.
Any guesses on what might be going on? Thanks!!
EDIT: Here is my whole class
class Program : System.ServiceProcess.ServiceBase
{
private static System.Timers.Timer timer;
static string myIP = "";
static void start()
{
string strHostName = Dns.GetHostName();
IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
int i = 0;
foreach (IPAddress address in addr)
{
if (("" + addr[i].AddressFamily).Equals("InterNetwork"))
myIP = "" + addr[i];
i++;
}
timer = new System.Timers.Timer();
timer.Elapsed += new ElapsedEventHandler(firefoxChecker); // Everytime timer ticks, timer_Tick will be called
timer.Interval = (1000) * (5);
timer.Enabled = true; // Enable the timer
timer.Start();
}
protected override void OnStart(string[] args)
{
start();
}
public static void Main()
{
System.ServiceProcess.ServiceBase.Run(new Program());
}
static string downloadLog()
{
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://server/log.txt"));
// Provide the WebPermission Credintials
reqFTP.Credentials = new NetworkCredential("username", "password");
reqFTP.Proxy = null;
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string log = reader.ReadToEnd();
reader.Close();
reader.Dispose();
return log;
}
static void sendLogThroughFTP(string log)
{
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://server/log.txt"));
reqFTP.Credentials = new NetworkCredential("username", "password");
reqFTP.Proxy = null;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
StreamWriter wr = new StreamWriter(@"C:\logs\temp.txt");
wr.Write(log);
wr.Close();
StreamReader sourceStream = new StreamReader(@"C:\logs\temp.txt");
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
reqFTP.ContentLength = fileContents.Length;
Stream requestStream = reqFTP.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
sourceStream.Dispose();
}
static void firefoxChecker(object sender, EventArgs e)
{
string firefoxOwner = "----------";
TerminalServicesManager manager = new TerminalServicesManager();
ITerminalServer server = null;
string log = downloadLog();
bool diceFirefoxRunning = false;
bool monsterFirefoxRunning = false;
bool careerbuilderFirefoxRunning = false;
try
{
server = manager.GetLocalServer();
server.Open();
foreach (ITerminalServicesSession session in server.GetSessions())
{
if (session.ConnectionState == ConnectionState.Active)
{
firefoxOwner = session.UserAccount.ToString();
string ip = session.ClientIPAddress.ToString();
string user = session.UserAccount.ToString();
System.Collections.Generic.IList<Cassia.ITerminalServicesProcess> list = session.GetProcesses();
foreach (ITerminalServicesProcess process in list)
{
if (Equals(process.ProcessName, "firefox.exe"))
{
// change firefoxOwner string appropriately
log = updateLog(log, user, firefoxOwner);
}
}
}
}
server.Close();
sendLogThroughFTP(log);
}
catch
{
// do nothing
}
}
static string updateLog(string log, string username, string ffOwner)
{
// make some changes to log string
return log;
}
}
}
Thanks for all the inputs!
Disable the timer when you start doing your work and re-enable it when you are done.
You are downloading and uploading via FTP which could take more than the 5 seconds you have set fro your timer. If you disable the timer before you start and re-enable it at the end, you will poll 5 seconds after the last upload completed.
You may also want to consider upping your polling time to something a little more reasonable. Do you really need to poll every 5 seconds to make sure firefox is still running?