建了个Windows Services项目,在Windows Services中调用exe文件,在进程中可以看到,在前台不显示。采取了几种方式都不行,请教解决方法。
以下黏贴的是部分主要代码,请指教: public partial class Service1 : ServiceBase
{
private Thread MainThread; public Service1()
{
InitializeComponent();
// TODO: 在 InitComponent 调用后添加任何初始化
MainThread = new Thread(new ThreadStart(ThreadFunc));
MainThread.Priority = ThreadPriority.Normal;
} protected override void OnStart(string[] args)
{
// TODO: 在此处添加代码以启动服务。
MainThread.Start();
} protected override void OnStop()
{
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
MainThread.Abort(); } public static void ThreadFunc()
{
int LastMinute = DateTime.Now.Minute;
bool First = false;
while (!First)
{
System.Threading.Thread.Sleep(60);
if (DateTime.Now.Minute - 1 == LastMinute)
{
ShellExecute(IntPtr.Zero, new StringBuilder("Open"), new StringBuilder("notepad"), new StringBuilder(" "), new StringBuilder(@"C:\"), 1);
////string strpath = Application.StartupPath;
//Process ps = new Process(); //ps.StartInfo.FileName = @"cmd.exe";
//ps.StartInfo.UseShellExecute = false;
////ps.StartInfo.RedirectStandardInput = true;
////ps.StartInfo.RedirectStandardOutput = true;
////ps.StartInfo.RedirectStandardError = true;
//ps.StartInfo.CreateNoWindow = false;
//ps.Start();
////string runpath = "cd " + strpath;
////ps.StandardInput.WriteLine(runpath);
////ps.StandardInput.WriteLine("QQ.exe");
//////ps.StandardInput.WriteLine("exit");
//////ps.WaitForExit();
////ps.Dispose();
////ps.Close(); LastMinute = DateTime.Now.Minute;
First = true;
}
}
} [DllImport("shell32.dll ")]
public static extern int ShellExecute(IntPtr hwnd, StringBuilder lpszOp, StringBuilder lpszFile, StringBuilder lpszParams, StringBuilder lpszDir, int FsShowCmd);
}
以下黏贴的是部分主要代码,请指教: public partial class Service1 : ServiceBase
{
private Thread MainThread; public Service1()
{
InitializeComponent();
// TODO: 在 InitComponent 调用后添加任何初始化
MainThread = new Thread(new ThreadStart(ThreadFunc));
MainThread.Priority = ThreadPriority.Normal;
} protected override void OnStart(string[] args)
{
// TODO: 在此处添加代码以启动服务。
MainThread.Start();
} protected override void OnStop()
{
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
MainThread.Abort(); } public static void ThreadFunc()
{
int LastMinute = DateTime.Now.Minute;
bool First = false;
while (!First)
{
System.Threading.Thread.Sleep(60);
if (DateTime.Now.Minute - 1 == LastMinute)
{
ShellExecute(IntPtr.Zero, new StringBuilder("Open"), new StringBuilder("notepad"), new StringBuilder(" "), new StringBuilder(@"C:\"), 1);
////string strpath = Application.StartupPath;
//Process ps = new Process(); //ps.StartInfo.FileName = @"cmd.exe";
//ps.StartInfo.UseShellExecute = false;
////ps.StartInfo.RedirectStandardInput = true;
////ps.StartInfo.RedirectStandardOutput = true;
////ps.StartInfo.RedirectStandardError = true;
//ps.StartInfo.CreateNoWindow = false;
//ps.Start();
////string runpath = "cd " + strpath;
////ps.StandardInput.WriteLine(runpath);
////ps.StandardInput.WriteLine("QQ.exe");
//////ps.StandardInput.WriteLine("exit");
//////ps.WaitForExit();
////ps.Dispose();
////ps.Close(); LastMinute = DateTime.Now.Minute;
First = true;
}
}
} [DllImport("shell32.dll ")]
public static extern int ShellExecute(IntPtr hwnd, StringBuilder lpszOp, StringBuilder lpszFile, StringBuilder lpszParams, StringBuilder lpszDir, int FsShowCmd);
}
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\你的服务名]
"Type"=dword:00000010
记得value+256 是与桌面交互。
比如现在00000010是16+256=272
16进制就是00000110