例外:System.Windows.Forms.dll中的“System.InvalidOperationException”

时间:2016-06-28 00:27:52

标签: 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的线程一起工作,所以我不知道。请帮帮我!

1 个答案:

答案 0 :(得分:4)

您无法直接从线程访问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"));
}