[英]Start a new thread in application on startup windows using C# [duplicate]
我创建了 2 个应用程序:AppA.exe 和 AppB.exe。
App A 在 windows 启动时通过添加一个键来运行
HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run。
我的 AppA.exe 很简单,如下所示:
if (!IsLatestVersion())
{
if(MessageBox.Show("There is a new version. Do you want to update?", "Confirmation", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
{
Process.Start(AppB.exe)
}
}
但是当我在消息框对话框上单击 Yes 时。 什么都没有发生。
我尝试直接双击AppA.exe运行,然后AppB.exe正常运行。 我想也许当 windows onstartup 启动我的 AppA.exe 时,它阻塞了所有线程,所以我无法通过 AppA.exe 运行 AppB.exe。
有什么解决办法吗? 谢谢。
有一个例子。
在 WindowsFormsApp1.exe 上:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//set registry so App1 can run on windows startup
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rkApp.SetValue("App1", Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "WindowsFormsApp1.exe"));
//Confirmation open App2
if (MessageBox.Show("Do you want to run App2?", "Confirmation", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
{
Process.Start("WindowsFormsApp2.exe");
}
Close();
}
}
在 WindowsFormsApp2.exe 上:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
然后将引用 WindowsFormsApp2 添加到 WindowsFormsApp1,以便它们位于同一文件夹中
在做了一些研究之后,我添加了以下可能对您有所帮助的代码片段:
private void RegisterInStartup(bool isChecked)
{
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (isChecked)
{
registryKey.SetValue("AppA.exe", Application.ExecutablePath);
}
}
我从以下答案中获得灵感:如何在 Windows 启动时运行 C# 应用程序?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.