Heey Guy's,

I'm having trouble understanding the Threading within an application.
I will try to explain the program:

2 forms
1 parent (Server)
1 child (Client)

both screens run on local computer (no network is needed)

The server generates a random number once I've pressed a button.
The number must be given to the Client
Witch will show the received value from the server within a label object.

So far I have tried the following:

Situation 1: (Not the first choice)

3 forms
1 parent (Server)
1 child (Client)
1 control (Control)

1 Extra screen from where I start the server and client, and an extra Start button.
This screen holds the Server object ant Client object, so i can call public methods (get randomnumber from server, SetRandomNumber on client)

here a code sample:

Controller (holding server + client object + start button)
Hereby the code from the Control.cs

public partial class Control : Form
    {
        private Thread oThreadServer;
        private Thread oThreadClient;
        private Thread oThreadWorker;
        private BingoServer oFormServer = new Server();
        private BingoClient oFormClient = new Client();

        public Control()
        {
            InitializeComponent();
        }

        private void btn_server_Click(object sender, EventArgs e)
        {
            oThreadServer = new Thread(new ThreadStart(this.m_P_NewServer));
            oThreadServer.Start();
        }

        private void btn_client_Click(object sender, EventArgs e)
        {
            oThreadClient = new Thread(new ThreadStart(this.m_P_NewClient));
            oThreadClient.Start();
        }

        private void btn_start_Click(object sender, EventArgs e)
        {
            oThreadWorker = new Thread(new ThreadStart(this.m_P_StartGame));
            oThreadWorker.Start();
        }

        private void btn_close_Click(object sender, EventArgs e)
        {
            oThreadServer.Abort();
            oThreadClient.Abort();
            oThreadWorker.Abort();
            Application.Exit();
        }

        private void m_P_NewServer()
        {
            oFormServer.ShowDialog();
        }

        private void m_P_NewClient()
        {
            oFormClient.ShowDialog();
        }

        private void m_P_StartGame()
        {
            while (true) // todo -> only while in_game
            {
               oFormClient.mSetNewRandom(oFormServer.mGetRandomNumber());
            }
        }
        
    }

here is the code for the Client.cs

public partial class Client : Form
    {        
        public Client()
        {
            InitializeComponent();
        }

        private void btn_close_Click(object sender, EventArgs e)
        {
            Application.ExitThread();
        }

        public void mSetNewRandom(int p_NewRandom)
        {
            this.lbl_number.Text = p_NewRandom.ToString();
        }
    }

as you can see I've got a thread for Client, a thread for the server, and a thread that gets the value from the server and gives it to the client.

the problem is when i want to give the client the value, the whole program crashes. Error is saying that Thread (oThreadWorker) cannot change a value within oThreadClient.
Getting a value from within oThreadServer using oThreadWorker is no problem.

Situation 2: (the first choice)

2 Screens:
1 Server
1 Client

this situation starts the client from within the server.
Same as situation 1 => client has public funcion -> but crashes deu to Tread problem.

The Question

Am I thinking in the wrong direction, trying to solve my problem, or am I missing something.

Most likely you'll need to invoke on the client form (Invoke)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.