Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8355267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:48:42+00:00 2026-06-09T09:48:42+00:00

I wrote a simple application in C#. It downloads a log file via ftp,

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:48:43+00:00Added an answer on June 9, 2026 at 9:48 am

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a simple drag-file-and-upload-automatically-to-ftp windows application and I'm using the MSDN code to
I have got a Wavecom Supreme GSM modem. I wrote a simple application that
I wrote a simple WinAPI application in C++ which embeds the Abode Flash ActiveX
I wrote some sort of console client for a simple application. To be more
I wrote a C# application that is a simple countdown timer. I use it
Lets write simple console application (debug mode): static void Main(string[] args) { Process p
Lets write simple console application: static void Main(string[] args) { IList<Thread> threads = new
I am trying to write a simple, in-house file delivery application for uploading files
I wrote simple application that using the cell phone camera. When i trying to
I'm learing LINQ-to-SQL right now and i have wrote a simple application that define

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.