Visual Studio 程序在IDE中运行良好,但不能作为.exe工作

p8ekf7hl  于 2023-11-21  发布在  其他
关注(0)|答案(1)|浏览(248)

我写了一个程序,开始最小化到系统托盘。双击图标,弹出一个需要密码的登录表单,如果密码正确,另一个表单将打开,登录表单将关闭。
使用regular:

  1. Settings f = new Settings();
  2. f.Show();
  3. this.Dispose();
  4. Application.Exit();

字符串
我尝试的变化导致了很多bug。有时应用程序会关闭,有时系统托盘中会有两个iconNotify和两个应用程序示例,等等。
因此,我求助于:

  1. private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
  2. {
  3. ThreadPool.QueueUserWorkItem((state) => {
  4. new System.Threading.Thread(() => {
  5. new Password().ShowDialog();
  6. }).Start();
  7. });
  8. }


这是在设置表单中,它在ThreadPool上启动密码表单(因此可以重用)。
我不知道为什么,但每次密码线程是处置本身(自动发生时,正确的密码已输入)-设置'形式激活FormClosing function.因此,我诉诸:

  1. private void Settings_FormClosing(object sender, FormClosingEventArgs e)
  2. {
  3. try
  4. {
  5. e.Cancel = true;
  6. this.Hide();
  7. this.WindowState = FormWindowState.Minimized;
  8. }
  9. catch (Exception ex)
  10. {
  11. this.Invoke(new Action(() => {
  12. this.WindowState = FormWindowState.Normal;
  13. this.Show();
  14. }));
  15. }
  16. }


下面是它在Password表单视图中的外观:

  1. private void btn_access_Click(object sender, EventArgs e)
  2. {
  3. if (tb_password.Text == Regex.Replace(GithubAPI.ReadGithubFile("master", "Password"), @"^\s*$\n|\r", string.Empty, RegexOptions.Multiline).TrimEnd())
  4. {
  5. //Settings form = new Settings();
  6. //form.WindowState = FormWindowState.Normal;
  7. //form.Show();
  8. this.Dispose();
  9. Application.Exit();
  10. }
  11. else
  12. {
  13. MessageBox.Show("Wrong password, try again!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  14. }
  15. }


在Visual-Studio中运行时,注解掉的三行没有什么区别,尽管作为.exe运行时,它会在一瞬间显示Settings的表单,然后消失。
这段代码现在运行得很好,在Visual-Studio 2022中也是如此。但是,每当我试图将它作为一个独立的应用程序运行时,它就会出错。
想法将不胜感激!

tzxcd3kk

tzxcd3kk1#

不知道是什么原因导致它不能作为.exe工作,所以我最终采取了不同的方法。
以前Application.Exit();应该触发private void Settings_FormClosing(object sender, FormClosingEventArgs e),其中包含激活表单的代码。密码表单中的this.Dispose();是为了摆脱显示多个表单打开。
这就是代码的结尾:

  1. private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
  2. {
  3. Password form = new Password();
  4. if(form.ShowDialog() == DialogResult.OK)
  5. {
  6. this.WindowState = FormWindowState.Normal;
  7. this.Show();
  8. }
  9. }
  10. private void Settings_FormClosing(object sender, FormClosingEventArgs e)
  11. {
  12. e.Cancel = true;
  13. this.Hide();
  14. this.WindowState = FormWindowState.Minimized;
  15. }

字符串
在密码表单中:

  1. private void btn_access_Click(object sender, EventArgs e)
  2. {
  3. if (tb_password.Text == Regex.Replace(GithubAPI.ReadGithubFile("master", "Password"), @"^\s*$\n|\r", string.Empty, RegexOptions.Multiline).TrimEnd())
  4. DialogResult = DialogResult.OK;
  5. else
  6. MessageBox.Show("Wrong password, try again!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  7. }


感谢所有花时间阅读的人,即使你没有提出解决方案。希望这对其他人有用:)

展开查看全部

相关问题