1

Here is where the error isI have no clue what the problem is.

Here is my code:

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;
        }


    }
}

It runs for a couple of seconds, then crashes. I am newish to C# and visual studio. Like I said, I have no clue what the problem is. I read that it might be caused by the threads changing the UI, but another version worked with the threads changing the UI, so I have no clue. Please help me!

LUKER
  • 540
  • 1
  • 5
  • 23
  • We can't read your mind - try a little harder, here. At least tell us what line the exception happens on... – J... Jun 28 '16 at 00:32
  • 1
    Debug your code, Place a Break point and trace the line that throws the exception. Then come with that line of code – sujith karivelil Jun 28 '16 at 00:41
  • 1
    from http://stackoverflow.com/questions/17432261/how-to-know-details-of-system-invalidoperationexception-in-system-windows-forms, go to the Debug menu, choosing Exceptions... and checking off the Thrown option next to Common Language Runtime Exceptions. This will break anytime a .NET exception is thrown, regardless of where or whether it's handled. This won't fix your problem, but it'll help identify it. – Ash Jun 28 '16 at 00:50
  • 1
    For a start, don't call functions by running them as separate threads unless you have a specific need to do so. Probably, you have 2 threads modifying the same item at the same time. Also, move as much initialization as you can from the constructor to the Load event of the form -- double-click on the title bar of the form to create the Load function. (I'm guessing you called threads in the constructor because you were getting errors because the form was not initialized yet.) – Eli Jun 28 '16 at 01:09
  • @Eli How should I have the two textboxes update then? I tried what you recommended, but with Grant Winney's answer and the form wont load at all. I used a while loop – LUKER Jun 28 '16 at 01:39

1 Answers1

5

You can't access the UI directly from your threads like you're doing.

Call Invoke on your controls first:

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"));
}
Grant Winney
  • 65,241
  • 13
  • 115
  • 165