-1

I have created 2 application: AppA.exe and AppB.exe.

App A is running on windows startup by adding a key in

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run.

My AppA.exe is simple like below:

if (!IsLatestVersion())
{
    if(MessageBox.Show("There is a new version. Do you want to update?", "Confirmation", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
    {
        Process.Start(AppB.exe)
    }       
}

But when I click Yes on messagebox dialog. Nothing happened.

I try to run directly by double click to AppA.exe, then AppB.exe runs normally. I think maybe when windows onstartup start my AppA.exe, it blocked all thread so I cannot run AppB.exe through AppA.exe.

Is there any solution? Thanks.

There is an example.

On 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();
    }
}

On WindowsFormsApp2.exe:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
}

Then add a reference WindowsFormsApp2 to WindowsFormsApp1 so they will be at the same folder

Shai Nguyễn
  • 85
  • 1
  • 7
  • 1
    The code as posted is not valid, can you post your actual code? Also, make sure you specify the full path to the executable, you may not be in control over what "working folder" is at the time your program is started in this manner. – Lasse V. Karlsen Mar 09 '20 at 11:49
  • It is most likely failing with an exception. Please add exception handling and, if needed, post the exception message. – Emond Mar 09 '20 at 12:54
  • sorry for making you confused. I have edit with an example. – Shai Nguyễn Mar 09 '20 at 15:03

1 Answers1

0

After doing a bit f research I am adding the following code snippet which might help you out:

private void RegisterInStartup(bool isChecked)
{
    RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
    if (isChecked)
    {
        registryKey.SetValue("AppA.exe", Application.ExecutablePath);
    }
}

I got the inspiration from the following answer: How to run a C# application at Windows startup?

vasilisdmr
  • 554
  • 8
  • 15
  • I think I can run AppA.exe successfull. The problem is happen when run AppB.exe. You can see the example I have just edited. – Shai Nguyễn Mar 09 '20 at 15:05