[英]How to launch a windows form in an application from Windows service?
I create a windows form application and a windows service, I can start my service from my windows form but if I try too launch the windows form application it doesn't work.
要啟動我的應用程序,我使用System.Process.Start
,我在選項卡中的任務管理器中看到我的 windows 表單應用程序名稱,但它沒有顯示我的表單。
public partial class testService: ServiceBase
{
Timer tm = new Timer();
int n = 0;
public testService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
tm.Interval = 10000;
tm.Elapsed += Tm_Elapsed;
tm.Start();
}
private void Tm_Elapsed(object sender, ElapsedEventArgs e)
{
if (n == 2)
{
try
{
ProcessStartInfo info = new ProcessStartInfo(@"pathName\appName.exe");
info.WorkingDirectory = Path.GetDirectoryName(@"pathName");
Process.Start(info);
}
catch (Exception exception)
{
Log.writeEventLog(exception.Message);
}
var service = new ServiceController("testService");
service.Stop();
}
n++;
}
protected override void OnStop()
{
tm.Stop();
}
}
有人知道為什么它不起作用嗎?
更新
我需要這樣做,因為如果用戶處於活動狀態,我必須每 10 分鍾詢問一次 windows 表格。
Windows 服務不允許與桌面交互, 因為 Windows Vista。 這有很多原因。 復選框是遺留的東西。
那么你如何從服務中啟動東西呢? 你沒有。 這不是服務應該如何工作的。
另一方面,您可以以用戶身份運行后台進程來完成同樣的事情。
您可以通過注冊表、啟動文件夾或任務計划程序啟動“服務”來完成此操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.