Click here to Skip to main content
15,895,709 members

How to convert a new windows console application into a windows service ?

vbtoan asked:

Open original thread
C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;

namespace SpeedTest
{
   
    class Program
    {
        
        static void Main(string[] args)
        {
            Console.Title = "A simple speedtest app";

            // the URL to download a file from
            Uri URL = new Uri("http://tv.vnn.vn");
            WebClient wc = new WebClient();

            Console.WriteLine("Welcome to this simple speedtest,\nwhich will test your download rate.");
            Console.WriteLine("Press any key to begin.");
            Console.ReadKey();

            Console.WriteLine("\nDownloading file: 1024kb.txt...");
            Console.WriteLine("From http://tv.vnn.vn");
            Console.WriteLine("Note: This file will automatically be deleted after the test.");
            
            // get current tickcount 
            double starttime = Environment.TickCount;

            // download file from the specified URL, and save it to C:\speedtest.txt
            
            wc.DownloadFile(URL, @"C:\SpeedTest\SpeedTest\speedtest.txt");

            // get current tickcount
            double endtime = Environment.TickCount;

            // how many seconds did it take?
            // we are calculating this by subtracting starttime from endtime
            // and dividing by 1000 (since the tickcount is in miliseconds.. 1000 ms = 1 sec)
            double secs = Math.Floor(endtime - starttime) / 1000;

            // round the number of secs and remove the decimal point
            double secs2 = Math.Round(secs, 0);

            // calculate download rate in kb per sec.
            // this is done by dividing 1024 by the number of seconds it
            // took to download the file (1024 bytes = 1 kilobyte)
            double kbsec = Math.Round(1024 / secs);

            Console.WriteLine("\nCompleted. Statistics:\n");

            Console.WriteLine("1mb download: \t{0} secs ({1} secs)", secs2, secs);
            Console.WriteLine("Download rate: \t{0} kb/sec", kbsec);

            Console.WriteLine("\nPress any key to exit...");
            Console.Read();
            Console.WriteLine("Cleaning up... (deleting downloaded file)");
            try
            {
                // delete downloaded file
                System.IO.File.Delete(@"C:\SpeedTest\SpeedTest\speedtest.txt");
                Console.WriteLine("Done.");
            }
            catch
            {
                Console.WriteLine("Couldn't delete download file.");
                Console.WriteLine("To delete the file yourself, go to your C-drive and look for the file 'speedtest.txt'.");
                Console.ReadKey();
            } 
        }

    }
}
[Edit]Code block added, 'C#' and 'services' tag added, 'to' tag removed[/Edit]
Tags: C#, Convert, Service, Console

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900