开始一个过程

扫码查看
本文介绍了开始一个过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个小程序,当用户登录时总是必须运行。 因为用户可以关闭这个程序我必须创建一个程序,总是检查 如果这个程序正在运行。 所以我用C#创建了一个程序(我的第一个!!),它适用于一个主要的 问题。 该程序检测桌面程序何时未运行并再次启动它b / b $ b。所有工作都按预期工作 但是 桌面程序作为SYSTEM用户运行。 这不是我想要的...... br /> 所以我需要找到一种方法来启动这个桌面程序,因为用户当前记录了 (它可以是不同的用户) 这可以在C#中完成吗? GTi 使用系统; 使用System.Collections; 使用System.ComponentModel; 使用System.Data; 使用System.Diagnostics; 使用System.ServiceProcess; 使用System.Runtime.InteropServices; 使用System.Windows.Forms; 使用System.Xml; 公共类Win32 { public const int WM_SYSCOMMAND = 0x0112; public const int SC_CLOSE = 0xF060; [DllImport(" user32.dll")] public static extern int FindWindow(string lpClassName,string lpWindowName); [DllImport(" user32.dll& quot;)] public static extern int SendMessage(int hWnd, uint Msg,int wParam,int lParam); [DllImport(" user32.dll")] public static extern int GetDesktopWindow(); } namespace attenWat { public class Service1:System。 ServiceProcess.ServiceBase { private System.Timers.Timer timer1 = null; private System.Timers.Timer newFileVersionChecker = null; private System.ComponentModel.IContainer components = null; public Service1() { InitializeComponent (); } //流程的主要入口点 static void Main() { System.ServiceProcess.ServiceBase [] ServicesToRun; ServicesToRun = new System.ServiceProcess.ServiceBase [] {new Service1()}; System.ServiceProcess.ServiceBase.Run(ServicesToRu n); } ///< summary> /// Re设计器支持的请求方法 - 不要使用代码编辑器修改 ///此方法的内容。 ///< / summary> private void InitializeComponent() { this.timer1 = new System.Timers.Timer(); this.newFileVersionChecker = new System.Timers.Timer(); ((System.ComponentModel.ISupportInitialize)(this.t imer1))。BeginInit(); ((系统。 ComponentModel.ISupportInitialize)(this.n ewFileVersionChecker))。BeginInit(); // timer1 this.timer1.Enabled = true; this.timer1.Interval = 10000; this.timer1.Elapsed + = new System.Timers.ElapsedEventHandler(this.timer1_Elap sed_1); // newFileVersionChecker this.newFileVersionChecker.Enabled = true; this.newFileVersionChecker.Interval = 60000; this.newFileVersionChecker。 Elapsed + = new System.Timers.ElapsedEventHandler(this.newFileVers ionChecker_Elapsed ); // Service1 this.ServiceName =" AttendanceService"; ((System.ComponentModel.ISupportInitialize)(this.t imer1))。EndInit(); ((System.ComponentModel.ISupportInitialize)(this.n ewFileVersionChecker))。EndInit(); } ///< summary> ///清理正在使用的所有资源。 ///< / summary> protected override void Dispose(bool disposing) { if(disposing) { if(components!= null) { components.Dispose(); } } base.Dispose(disposing); } ///< summary> ///设置动态以便您的服务可以正常工作。 ///< / summary> protected override void OnStart(string [] args) { // TODO:在这里添加代码以开始服务。 } ///< summary> / //停止此服务。 ///< / summary> protected override void OnStop() { // TODO:在这里添加代码以执行任何必要的拆卸以停止 服务。 } ///////////////////////////////////////////////// /////////////////////////////////////// private void timer1_Elapsed_1 (对象发送者,System.Timers.ElapsedEventArgs e) { if((Win32.GetDesktopWindow()!= 0)&& ; (Win32.FindWindow(" TUN.Attendance.Windows.Class",n ull)== 0)) { 处理myProcess = new Process(); try { string myDocumentsPath = Environment.GetFolderPath(Environment .SpecialFolde r.ProgramFiles); //需要找到在当前用户环境中启动此程序的方法 myProcess.StartInfo.Verb =" Open" ;; myProcess.StartInfo.UseShellExecute = true; // myProcess.StartInfo.FileName = myDocumentsPath + \\tun\\attendance.exe; // myProcess.Start(); } catch(Win32Exception错误) { string dummyString = err.Message; //避免警告消息 // MessageBox.Show(err.Message +" \\\\ n" + myProcess.Start Info.FileName," Check 路径。,MessageBoxButtons.OK); } } } private void newFileVersionChecker_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { // Application.StartupPath; } } }I have small program that always must be running when a user is logged on.Since users can close this program I must create a program that always checkif this program is running.So I created a program in C# (my first!!) and it works with one majorproblem.This program detects when the desktop program is not running and starts itagain. All works as expectedBUTThe desktop program is running as a SYSTEM user.That is not what I wanted....So I need to find a way to start this desktop program as the current loggedon user (it can be different users)Can this be done in C#?GTiusing System;using System.Collections;using System.ComponentModel;using System.Data;using System.Diagnostics;using System.ServiceProcess;using System.Runtime.InteropServices;using System.Windows.Forms;using System.Xml;public class Win32{public const int WM_SYSCOMMAND = 0x0112;public const int SC_CLOSE = 0xF060;[DllImport("user32.dll")] public static extern int FindWindow(stringlpClassName, string lpWindowName);[DllImport("user32.dll")] public static extern int SendMessage(int hWnd,uint Msg, int wParam, int lParam);[DllImport("user32.dll")] public static extern int GetDesktopWindow();}namespace attenWat{public class Service1 : System.ServiceProcess.ServiceBase{private System.Timers.Timer timer1=null;private System.Timers.Timer newFileVersionChecker=null;private System.ComponentModel.IContainer components=null;public Service1(){InitializeComponent();}// The main entry point for the processstatic void Main(){System.ServiceProcess.ServiceBase[] ServicesToRun;ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };System.ServiceProcess.ServiceBase.Run(ServicesToRu n);}/// <summary>/// Required method for Designer support - do not modify/// the contents of this method with the code editor./// </summary>private void InitializeComponent(){this.timer1 = new System.Timers.Timer();this.newFileVersionChecker = new System.Timers.Timer();((System.ComponentModel.ISupportInitialize)(this.t imer1)).BeginInit();((System.ComponentModel.ISupportInitialize)(this.n ewFileVersionChecker)).BeginInit();// timer1this.timer1.Enabled = true;this.timer1.Interval = 10000;this.timer1.Elapsed += newSystem.Timers.ElapsedEventHandler(this.timer1_Elap sed_1);// newFileVersionCheckerthis.newFileVersionChecker.Enabled = true;this.newFileVersionChecker.Interval = 60000;this.newFileVersionChecker.Elapsed += newSystem.Timers.ElapsedEventHandler(this.newFileVers ionChecker_Elapsed);// Service1this.ServiceName = "AttendanceService";((System.ComponentModel.ISupportInitialize)(this.t imer1)).EndInit();((System.ComponentModel.ISupportInitialize)(this.n ewFileVersionChecker)).EndInit();}/// <summary>/// Clean up any resources being used./// </summary>protected override void Dispose( bool disposing ){if( disposing ){if(components != null){components.Dispose();}}base.Dispose( disposing );}/// <summary>/// Set things in motion so your service can do its work./// </summary>protected override void OnStart(string[] args){// TODO: Add code here to start your service.}/// <summary>/// Stop this service./// </summary>protected override void OnStop(){// TODO: Add code here to perform any tear-down necessary to stop yourservice.}////////////////////////////////////////////////////////////////////////////////////////private void timer1_Elapsed_1(object sender, System.Timers.ElapsedEventArgse){if((Win32.GetDesktopWindow()!=0) &&(Win32.FindWindow("TUN.Attendance.Windows.Class",n ull)==0)){Process myProcess = new Process();try{string myDocumentsPath =Environment.GetFolderPath(Environment.SpecialFolde r.ProgramFiles);// need to find a way to start this program in the current user environmentmyProcess.StartInfo.Verb = "Open";myProcess.StartInfo.UseShellExecute = true;// myProcess.StartInfo.FileName = myDocumentsPath + \\tun\\attendance.exe;// myProcess.Start();}catch(Win32Exception err){string dummyString = err.Message; // Avoid warning message// MessageBox.Show(err.Message+"\r\n"+myProcess.Start Info.FileName, "Checkthe path.", MessageBoxButtons.OK);}}}private void newFileVersionChecker_Elapsed(object sender,System.Timers.ElapsedEventArgs e){// Application.StartupPath;}}}推荐答案 这篇关于开始一个过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 12:43
查看更多