您的位置:首页 > 其它

window服务调用技巧

2012-06-14 16:59 225 查看
在使用windows服务时,安装能有些不太方便,本文就安装时就使用的技巧提出一点方式,以供大家参考:

windows服务的优点还是加以说明 的1、能够自动运行(需要预先设置运行模式); 2、不要求用户交互,(特别适合在服务器上进行运行);3、在后台运行,如果程序运行时,特别超耗费时间,如备分数据,删除大量数据,多线程捕获数据等等),都可能使用。

但是在安装时,经常会找不到这个关键的文件installutil.exe,因为此文件是用来支持安装过程的。

详细介绍见http://www.cnblogs.com/YanPSun/archive/2010/05/22/1741381.html,

这是就给出一种调用的方式方法。

try
            {
                System.Uri uri = new Uri(typeof(string).Assembly.CodeBase);
                string RuntimePath = System.IO.Path.GetDirectoryName(uri.LocalPath);
                string strInstallUtilPath = System.IO.Path.Combine(RuntimePath, "InstallUtil.exe");
                foreach (string arg in System.Environment.GetCommandLineArgs())
                {
                    Console.WriteLine(arg);
                    if (arg == "/install")
                    {
                        System.Diagnostics.Process.Start(strInstallUtilPath, "\"" + System.Windows.Forms.Application.ExecutablePath + "\"");
                        return;
                    }
                    else if (arg == "/uninstall")
                    {
                        System.Diagnostics.Process.Start(strInstallUtilPath, "/u \"" + System.Windows.Forms.Application.ExecutablePath + "\"");
                        return;
                    }
                    else if (arg == "/client")
                    {
                        // 启动客户端
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);

                        using (Form1 frm = new Form1())
                        {
                            Application.Run(frm);                           
                        }
                        return;
                    }                  
                }
            }
            catch (Exception ext)
            {
                Console.WriteLine(ext.ToString());
                return;
            }

还要进一步说明的,服务开始执行的语句放到

protected override void OnStart(string[] args)

{

’执行动作的开始

}



停止服务的语句放到

#region 停止服务线程.

protected override void OnStop()

{

'停止工作

base.OnStop();

}

#endregion

以上工作是受服务启动,和停止来执行的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: