c# - 异常 : 'System.InvalidOperationException' in System. Windows.Forms.dll

标签 c#

<分区>

Here is where the error is我不知道问题出在哪里。

这是我的代码:

namespace TimeClock
{
    public partial class Form1 : Form
    {
        String Code;
        String Name;
        String InOut;
        String allTextIn;
        String allTextOut;
        Boolean Joshua = false;
        int JoshuaInt = 1;
        Boolean Alec = false;
        int AlecInt = 3;
        Boolean Kyle = false;
        int KyleInt = 5;
        Boolean Jeffrey = false;
        int JeffreyInt = 7;
        Boolean Carl = false;
        int CarlInt = 9;
        Boolean Angela = false;
        int AngelaInt = 11;
        Boolean Kendra = false;
        int KendraInt = 13;
        Boolean Susan = false;
        int SusanInt = 15;
        Boolean Kory = false;
        int KoryInt = 17;
        Boolean Jeanine = false;
        int JeanineInt = 19;
        Boolean Daniel = false;
        int DanielInt = 21;
        Boolean Steven = false;
        int StevenInt = 23;
        Boolean Jacob = false;
        int JacobInt = 25;
        Boolean Mario = false;
        int MarioInt = 27;
        int Index;






        String csvPath = "C:/Users/Public/TimeClockData.csv";
        StringBuilder Header = new StringBuilder();
        StringBuilder csvData = new StringBuilder();
        private static SpeechSynthesizer synth = new SpeechSynthesizer();
        Thread saveData;
        Thread clock;
        Thread location;



        public Form1()
        {
            InitializeComponent();
            FormBorderStyle = FormBorderStyle.None;
            WindowState = FormWindowState.Maximized;
            TopMost = true;

            Header.AppendLine("Timestamp, Name");
            File.AppendAllText(csvPath, Header.ToString());
            textBox1.Font = new Font("Arial", 30, FontStyle.Bold);

            clock = new Thread(new ThreadStart(Clock));
            clock.Start();

            location = new Thread(new ThreadStart(GetPugsleyLocation));
            location.Start();

        }

        private void Clock()
        {
            DateTime timeStamp = DateTime.Now;
            textBox2.Text = timeStamp.ToString();

        }

        private void GetPugsleyLocation()
        {
            Ping ping = new Ping();
            string ip = "";
            IPAddress address = IPAddress.Parse(ip);
            PingReply pong = ping.Send(address);
            if (pong.Status == IPStatus.Success)
            {
                textBox3.Text = "Pugsley is here now";
            }
            else
            {
                textBox3.Text = "Pugsley is not here right now";
            }
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                FormBorderStyle = FormBorderStyle.Sizable;
                WindowState = FormWindowState.Normal;
                TopMost = false;
            }
        }

        public static void SpeakNow(string message)
        {
            synth.SelectVoiceByHints(VoiceGender.Male);
            synth.Speak(message);
            synth.Rate = -2;
        }

        public void saveDataThread()
        {
            DateTime timeStamp = DateTime.Now;
            csvData.AppendLine(timeStamp + "," + Name + "," + InOut);
            File.AppendAllText(csvPath, csvData.ToString());
            csvData.Clear();
            InOut = null;
        }

        private void Number_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            Code = Code + button.Text;
            textBox1.Text = Code;
        }

        private void Enter_Click(object sender, EventArgs e)
        {
            //in or out
            Button button = (Button)sender;
            Name = "";
            var list = new List<string>();
            list.Add("107");
            list.Add("104");
            list.Add("115");
            list.Add("131");
            list.Add("113");
            list.Add("112");
            list.Add("126");
            list.Add("117");
            list.Add("130");
            list.Add("116");
            list.Add("108");
            list.Add("133");
            list.Add("122");
            list.Add("106");

            if (list.Contains(Code))
            {
                if (Code == "107")
                {
                    Name = "Joshua";
                    Index = JoshuaInt;
                    if (Joshua == false)
                    {
                        InOut = "In";
                        Joshua = true;
                        goto done;
                    }
                    if (Joshua == true)
                    {
                        InOut = "Out";
                        Joshua = false;
                        goto done;
                    }
                }
                if (Code == "104")
                {
                    Name = "Alec";
                    Index = AlecInt;
                    if (Alec == false)
                    {
                        InOut = "In";
                        Alec = true;
                        goto done;
                    }
                    if (Alec == true)
                    {
                        InOut = "Out";
                        Alec = false;
                        goto done;
                    }
                }
                if (Code == "115")
                {
                    Name = "Kyle";
                    Index = KyleInt;
                    if (Kyle == false)
                    {
                        InOut = "In";
                        Kyle = true;
                        goto done;
                    }
                    if (Kyle == true)
                    {
                        InOut = "Out";
                        Kyle = false;
                        goto done;
                    }
                }
                if (Code == "131")
                {
                    Name = "Jeffrey";
                    Index = JeffreyInt;
                    if (Jeffrey == false)
                    {
                        InOut = "In";
                        Jeffrey = true;
                        goto done;
                     }
                    if (Jeffrey == true)
                    {
                        InOut = "Out";
                        Jeffrey = false;
                        goto done;
                    }
                }
                if (Code == "113")
                {
                    Name = "Carl";
                    Index = CarlInt;
                    if (Carl == false)
                    {
                        InOut = "In";
                        Carl = true;
                        goto done;
                     }
                    if (Carl == true)
                    {
                        InOut = "Out";
                        Carl = false;
                        goto done;
                    }
                }
                if (Code == "112")
                {
                    Name = "Angela";
                    Index = AngelaInt;
                    if (Angela == false)
                    {
                        InOut = "In";
                        Angela = true;
                        goto done;
                     }
                    if (Angela == true)
                    {
                        InOut = "Out";
                        Angela = false;
                        goto done;
                    }
                }
                if (Code == "126")
                {
                    Name = "Kendra";
                    Index = KendraInt;
                    if (Kendra == false)
                    {
                        InOut = "In";
                        Kendra = true;
                        goto done;
                     }
                    if (Kendra == true)
                    {
                        InOut = "Out";
                        Kendra = false;
                        goto done;
                    }
                }
                if (Code == "117")
                {
                    Name = "Susan";
                    Index = SusanInt;
                    if (Susan == false)
                    {
                        InOut = "In";
                        Susan = true;
                        goto done;
                     }
                    if (Susan == true)
                    {
                        InOut = "Out";
                        Susan = false;
                        goto done;
                    }
                }
                if (Code == "130")
                {
                    Name = "Kory";
                    Index = KoryInt;
                    if (Kory == false)
                    {
                        InOut = "In";
                        Kory = true;
                        goto done;
                     }
                    if (Kory == true)
                    {
                        InOut = "Out";
                        Kory = false;
                        goto done;
                    }
                }
                if (Code == "116")
                {
                    Name = "Jeanine";
                    Index = JeanineInt;
                    if (Jeanine == false)
                    {
                        InOut = "In";
                        Jeanine = true;
                        goto done;
                     }
                    if (Jeanine == true)
                    {
                        InOut = "Out";
                        Jeanine = false;
                        goto done;
                    }
                }
                if (Code == "108")
                {
                    Name = "Daniel";
                    Index = DanielInt;
                    if (Daniel == false)
                    {
                        InOut = "In";
                        Daniel = true;
                        goto done;
                     }
                    if (Daniel == true)
                    {
                        InOut = "Out";
                        Daniel = false;
                        goto done;
                    }
                }
                if (Code == "133")
                {
                    Name = "Steven";
                    Index = StevenInt;
                    if (Steven == false)
                    {
                        InOut = "In";
                        Steven = true;
                        goto done;
                     }
                    if (Steven == true)
                    {
                        InOut = "Out";
                        Steven = false;
                        goto done;
                    }
                }
                if (Code == "122")
                {
                    Name = "Jacob";
                    Index = JacobInt;
                    if (Jacob == false)
                    {
                        InOut = "In";
                        Jacob = true;
                        goto done;
                     }
                    if (Jacob == true)
                    {
                        InOut = "Out";
                        Jacob = false;
                        goto done;
                    }
                }
                if (Code == "106")
                {
                    Name = "Mario";
                    Index = MarioInt;
                    if (Mario == false)
                    {
                        InOut = "In";
                        Mario = true;
                        goto done;
                    }
                    if (Mario == true)
                    {
                        InOut = "Out";
                        Mario = false;
                        goto done;
                    }
                }

            }
            else
            {
                goto clear;
            }
        done:
            InOut = button.Text;
            saveData = new Thread(new ThreadStart(saveDataThread));
            saveData.Start();
            String[] textIn = System.IO.File.ReadAllLines(@"C:\Users\Public\textIn.txt");
            String[] textOut = System.IO.File.ReadAllLines(@"C:\Users\Public\textOut.txt");
            allTextIn = textIn[Index];
            allTextOut = textOut[Index];
            if (InOut == "In")
            {
                if (allTextIn != "/")
                {
                    SpeakNow(allTextIn);
                }
                else
                {
                    SpeakNow("Hello" + Name);
                }
            }
            else
            {
                if (allTextOut != "/")
                {
                    SpeakNow(allTextOut);
                }
                else
                {
                    SpeakNow("Goodbye" + Name);
                }
            }
        clear:
            Code = null;
            textBox1.Text = Code;

        }

        private void Backspace_Click(object sender, EventArgs e)
        {
            //Backspace
            if (Code.Length >= 1)
            {
                Code = Code.Substring(0, Code.Length - 1);
                textBox1.Text = Code;
            }
        }

        private void Clear_Click(object sender, EventArgs e)
        {
            //Clear
            Code = null;
            textBox1.Text = Code;
        }


    }
}

它运行了几秒钟,然后崩溃了。我是 C# 和 visual studio 的新手。就像我说的,我不知道问题出在哪里。我读到这可能是由线程改变 UI 引起的,但另一个版本与改变 UI 的线程一起工作,所以我不知道。请帮助我!

最佳答案

您不能像现在这样直接从您的线程访问 UI。

首先在您的控件上调用Invoke:

private void Clock()
{
    textBox2.Invoke(new Action(() => textBox2.Text = DateTime.Now.ToString()));
}

private void GetPugsleyLocation()
{
    ...
    ...
    textBox3.Invoke(new Action(() =>
        textBox3.Text = "Pugsley is " + (pong.Status == IPStatus.Success ? "" : "not") + " here now"));
}

关于c# - 异常 : 'System.InvalidOperationException' in System. Windows.Forms.dll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38065238/

相关文章:

c# - 为什么 C# 会这样舍入?

c# - 继承 - 目标特定的继承类 C#

c# - 根据函数的表达式参数推断第二个泛型参数

c# - 从 Web 浏览器页面获取数据到 wpf 应用程序

c# - 在 UpdatePanel 中回发后添加用户控件,然后是另一个回发 - 如何在用户控件中获取控件的输入?

c# - 在 C# winforms 中覆盖存储在 app.config 中的连接字符串

c# - 如何使Windows窗体应用程序响应式?

c# - 登录控制台应用程序(带有 DI 的 .NET Core)

c# - gridview 列的总和,跳过重复项

c# - 序列化和反序列化多种类型的对象