当前位置:编程学习 > C#/ASP.NET >>

关于用c#写后台服务程序的问题

对c#学的不深,只知道c#作界面比较好,容易且好看,但作后台服务类(比如故障处理类服务,作为动态库由界面调用),一直用vc做后台程序,不知道c#怎么样,与vc比较,难易程度,执行效率等方面? --------------------编程问答-------------------- C#做服务也很简单

新建项目-windows-windows服务


using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Http;
using RemotingLib;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.ServiceProcess;
using System.IO;
using System;

namespace EdnolandService
{
    public partial class Service1 : ServiceBase
    {
     


        private System.Timers.Timer _Timer = null;

        private static string LogFileName = System.AppDomain.CurrentDomain.BaseDirectory + "ServiceLog.txt";
      
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            try
            {
                WriteFiles("Windows Services 开始工作:\n 定时器启动:" + DateTime.Now.ToString());
                _TimedEvent(null, null);
                StartTimer();
            }
            catch { }
        }

        protected override void OnStop()
        {
            try
            {
                if (_Timer != null)
                {
                    _Timer.Enabled = false;
                    WriteFiles("Timer定时器停止:" + DateTime.Now.ToString());
                    _Timer.Dispose();
                }
            }
            catch
            {

            }
        }

        //启动定时器
        private void StartTimer()
        {
            try
            {
                _Timer = new System.Timers.Timer();
                _Timer.Interval = 3000;
                _Timer.Elapsed += new System.Timers.ElapsedEventHandler(_TimedEvent);
                _Timer.Enabled = true;
            }
            catch
            { }
        }


        public static void WriteFiles(string str)
        {
            try
            {
                using (FileStream fs = new FileStream(LogFileName, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    StreamWriter m_streamWriter = new StreamWriter(fs);
                    m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
                    m_streamWriter.WriteLine(str);
                    m_streamWriter.Flush();
                    m_streamWriter.Close();
                    fs.Close();
                }
            }
            catch { }
        }

        private static void _TimedEvent(object source, System.Timers.ElapsedEventArgs e)
        {
            WriteFiles(string.Format("测试Windows Services服务!当前时间{0}",DateTime.Now.ToLongTimeString()));
        }
    }
}

--------------------编程问答--------------------
引用楼主 jixiangbird 的回复:
作后台服务类(.. 作为动态库由界面调用),

???

做动态库调用 也能叫做后台服务类? 晕

ServiceBase 可做 windows服务

或者就开webservice,用IIS作为服务运行者,程序实现webService也可以 --------------------编程问答-------------------- 做Windows服务和WebService都没有问题,
但是做动态库比较郁闷,要.net框架才能运行的。 --------------------编程问答-------------------- --------------------编程问答-------------------- up
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,