如何将新的Windows控制台应用程序转换为Windows服务? [英] How to convert a new windows console application into a windows service ?
本文介绍了如何将新的Windows控制台应用程序转换为Windows服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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();
}
}
}
}
已添加代码块,''C#''和''services''标签添加,'''''标签已删除[/编辑]
Code block added, ''C#'' and ''services'' tag added, ''to'' tag removed[/Edit]
推荐答案
您好,
有一个看看这篇关于如何创建Windows服务的文章:
在C#中创建基本Windows服务 [ ^ ]
http://www.switchonthecode.com/tutorials/creating-a-simple-windows-service-in-csharp [ ^ ]
http://www.c-sharpcorner.com/UploadFile/mahesh/window_service11262005045007AM/window_service.aspx [ ^ ]
Hi,
Have a look at this articles about how to create a Windows Service:
Creating a Basic Windows Service in C#[^]
http://www.switchonthecode.com/tutorials/creating-a-simple-windows-service-in-csharp[^]
http://www.c-sharpcorner.com/UploadFile/mahesh/window_service11262005045007AM/window_service.aspx[^]
我做的如上,但是出现以下错误信息
在单个线程上启动第二个消息循环不是有效操作。改为使用Form.ShowDialog。
在位置
Application.Run(new Form1());
I did as above but the following error message
Starting a second message loop on a single thread is not a valid operation. Use Form.ShowDialog instead.
in position
Application.Run(new Form1());
这篇关于如何将新的Windows控制台应用程序转换为Windows服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文