0

i do have an embedded board (mini/tiny 210) and i did try building an application which uses system.socket and system.socket.net in C# ( vs2005 - wince5 device application but i do have wince 6 on my device!! )

my purpose is to send and receive data bytes using Ethernet. for sending , well there is no problem but receive does need multitasking.

i should start a task so it can listen to the port if there is any data upcoming but once i add thread part to my code the whole program crashes (dose not do that in pc x64 core i5) but in embedded board it will crash.

if i eliminate multi tasking i might lose some data when networkstream.read is not online or when networkstream.read is online and i'm waiting for data,which ends in program will not respond up until the timeout. here is some of my codes . hope it helps.

this code does work :

private void button_connect_Click(object sender, EventArgs e)
    {
        try
        {
            //Thread TH = new Thread(new ThreadStart(con));
            //TH.Start();
            con();
        }
        catch
        {
            MessageBox.Show("THREAD ERROR");
        }
    }
    public void con()
    {
        try
        {
            cli = null;
            cli = new TcpClient();
            cli.Connect(IPAddress.Parse(textBox_ip.Text),Convert.ToInt32(textBox_port.Text));
            st = null;
            st = cli.GetStream();
            button_connect.Enabled = false;
            button_Close.Enabled = false;
            button_disconnect.Enabled = true;
            statusBar1.Text = "Connected to server";
            //Thread rec = new Thread(new ThreadStart(recf));
            //rec.Start();
        }
        catch
        {
            MessageBox.Show("Connection faild","Error");
            statusBar1.Text = "Faild to connect to server";
        }

this code dose not work :

private void button_connect_Click(object sender, EventArgs e)
    {
        try
        {
            Thread TH = new Thread(new ThreadStart(con));
            TH.Start();
            //con();
        }
        catch
        {
            MessageBox.Show("THREAD ERROR");
        }
    }
    public void con()
    {
        try
        {
            cli = null;
            cli = new TcpClient();
            cli.Connect(IPAddress.Parse(textBox_ip.Text),Convert.ToInt32(textBox_port.Text));
            st = null;
            st = cli.GetStream();
            button_connect.Enabled = false;
            button_Close.Enabled = false;
            button_disconnect.Enabled = true;
            statusBar1.Text = "Connected to server";
            //Thread rec = new Thread(new ThreadStart(recf));
            //rec.Start();
        }
        catch
        {
            MessageBox.Show("Connection faild","Error");
            statusBar1.Text = "Faild to connect to server";
        }
    }

as you can see i'm getting this crash while just one of my task is in the code the second task has been commented

i appreciate your help in advance

S2G
  • 121
  • 7

1 Answers1

0

The crash is probably caused by updating UI elements from a secondary thread.

The con method reads a textbox and updates several buttons as well as a status bar, and this should only happen on the main application thread.

To verify whether this is the issue, you could simply comment out the lines in the con method that access UI elements (copy the textbox value to a member string before starting the thread, or just hard-code the IP address) and re-run the application.

Then to actually fix the problem you'll have to look into Control.Invoke.

Community
  • 1
  • 1
Carsten Hansen
  • 1,508
  • 2
  • 21
  • 27