InvalidOperationException в XP, но не в Windows 10 - PullRequest
0 голосов
/ 15 февраля 2019

Я пытаюсь запустить приложение Winforms на компьютере с XP, но получаю следующую ошибку.Он отлично работает при работе в Windows 10. Форма 2 запускается, когда форма 1 свернута, но форма 2 не должна отображаться, если запущен процесс с именем SomeApplication.Может кто-нибудь, пожалуйста, помогите мне понять, что XP не нравится в этом коде?Кроме того, даже в Windows 10 эта форма вызывает высокую загрузку ЦП, возможно, из-за использования потока?Кто-нибудь есть лучший способ сделать это?

Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.InvalidOperationException
Stack:
   at System.Windows.Forms.Control.MarshaledInvoke(System.Windows.Forms.Control, System.Delegate, System.Object[], Boolean)
   at System.Windows.Forms.Control.Invoke(System.Delegate, System.Object[])
   at System.Windows.Forms.Control.Invoke(System.Delegate)
   at NotificationCenter.Form2.checkThread()
   at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Threading.ThreadHelper.ThreadStart()




 public Form2()
        {
            InitializeComponent();
            this.TopMost = true;
            this.ShowInTaskbar = false;
            Thread th = new Thread(checkThread);
            th.Start();
        }

           public void checkThread()
           {
           while (true)
           {
               var pname = Process.GetProcessesByName("SomeApplication");
               if (pname.Length > 0)
               {
                   Invoke(new Action(() =>
                   {
                       this.Visible = false;
                   }));
               }
           else
           {
              Invoke(new Action(() =>
              {
                  this.Visible = true;

              }));
             }}}

    private void button1_Click(object sender, EventArgs e)
            {   
                Form1 settingsForm = new Form1();
                settingsForm.Show();         
            }

А вот Form1, вызывающая Form2:

 private void button1_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            this.ShowInTaskbar = false;
            this.Hide();
            this.FormBorderStyle = FormBorderStyle.None;


            Form fc = Application.OpenForms["Form2"];
           if (fc != null)
            {

            }
           else
           {
               Form2 settingsForm = new Form2();
               settingsForm.Show();
               settingsForm.TopMost = true;
               settingsForm.ShowInTaskbar = false;
           }
        }

EDIT:
Solved the issue by changing the code below:

public partial class Form2 : Form
    {
        public Form2()
        {


           InitializeComponent();
                this.StartPosition = FormStartPosition.Manual;
                int intScreenHeight = 

           Convert.ToInt32(Screen.PrimaryScreen.Bounds.Height * .002);
                int intScreenWidth = 
           Convert.ToInt32(Screen.PrimaryScreen.Bounds.Width * .95);
                this.Location = new Point(intScreenWidth, 
           intScreenHeight);
                this.TopMost = true;
                this.ShowInTaskbar = false;
                this.MinimumSize = new Size(1, 1);
                this.Size = new Size(54, 44);
                int intButtonYSize = ClientSize.Height;
                int intButtonXSize = ClientSize.Width;
                button1.Size = new Size(intButtonXSize, intButtonYSize);
          //      Thread th = new Thread(checkThread);
          //      th.Start();


                Task.Factory.StartNew(() =>
                {
                    while (true)
                    {
                        var pname = 
               Process.GetProcessesByName("SomeApplication");
                        if (pname.Length > 0)
                        {
                            Invoke(new Action(() =>
                            {
                                this.Visible = false;
                            }));
                        }
                        else
                        {
                            Invoke(new Action(() =>
                            {
                                this.Visible = true;
                            }));
                        }
                    }
                });           
            }

            private void button1_Click(object sender, EventArgs e)
            {   
                Form1 settingsForm = new Form1();
                settingsForm.Show();                                          
            }

            private void label2_Click(object sender, EventArgs e)
            {
                Form1 settingsForm = new Form1();
                settingsForm.Show();
            }
        }
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...