Naveen

Naveen

  • 1.5k
  • 134
  • 6.8k

I need to create a multiple threads in windows forms application.

Jan 29 2024 10:52 AM

These are my 2 classes. i am getting error is 

Error:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll

Additional information: Invoke or BeginInvoke cannot be called on a control until the window handle has been created.

 

My 2 classes code: 


using System;
using System.Windows.Forms;
using canlibCLSNET;
using System.Threading;

namespace CanDataThread
{
    public partial class Form1 : Form
    {
        private static int handle;
        private static int handleWrite;
        private static int handleWrite1;
        private static int handleWrite2;

        private static Thread thread1;
        private static Thread thread2;
        private static Thread thread3;

        private CanData dataHandler;
        public Form1()
        {
            InitializeComponent();
            dataHandler = new CanData();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Canlib.canInitializeLibrary();

            int channel = 0;
            int channel1 = 1;
            int channel2 = 2;
            int channel3 = 3;

            handle = Canlib.canOpenChannel(channel, Canlib.canOPEN_ACCEPT_VIRTUAL);
            handleWrite = Canlib.canOpenChannel(channel1, Canlib.canOPEN_ACCEPT_VIRTUAL);
            handleWrite1 = Canlib.canOpenChannel(channel2, Canlib.canOPEN_ACCEPT_VIRTUAL);
            handleWrite2 = Canlib.canOpenChannel(channel3, Canlib.canOPEN_ACCEPT_VIRTUAL);

            Canlib.canSetBusParams(handle, Canlib.canBITRATE_250K, 0, 0, 0, 0, 0);
            Canlib.canSetBusParams(handleWrite, Canlib.canBITRATE_250K, 0, 0, 0, 0, 0);
            Canlib.canSetBusParams(handleWrite1, Canlib.canBITRATE_250K, 0, 0, 0, 0, 0);
            Canlib.canSetBusParams(handleWrite2, Canlib.canBITRATE_250K, 0, 0, 0, 0, 0);

            Canlib.canBusOn(handle);
            Canlib.canBusOn(handleWrite);
            Canlib.canBusOn(handleWrite1);
            Canlib.canBusOn(handleWrite2);

            thread1 = new Thread(() => dataHandler.ReadCanData(handle, handleWrite, richTextBox1));
            thread2 = new Thread(() => dataHandler.ReadCanData(handle, handleWrite1, richTextBox2));
            thread3 = new Thread(() => dataHandler.ReadCanData(handle, handleWrite2, richTextBox3));

            thread1.Start();
            thread2.Start();
            thread3.Start();
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        { }
    }
}         

using System;
using canlibCLSNET;
using System.Windows.Forms;

namespace CanDataThread
{
    public class CanData 
    {
        private static int HexConvert(string z)
        {
            int m = Convert.ToInt32(z, 2);
            return m;
        }
        public void ReadCanData(int readHandle, int handleWrite, RichTextBox richTextBox)
        {
            Canlib.canStatus status;
            Canlib.canStatus status1;

            byte[] data = new byte[8];
            int id, flags, dlc;
            long timestamp;

            try
            {
                while (true)
                {
                    status = Canlib.canReadWait(readHandle, out id, data, out dlc, out flags, out timestamp, 100);

                    string idBinary = Convert.ToString(id, 2).PadLeft(29, '0');

                    string hex1 = idBinary.Substring(0, 3);
                    string hex2 = idBinary.Substring(3, 18);
                    string hex3 = idBinary.Substring(21, 8);

                    int h1 = HexConvert(hex1);
                    int h2 = HexConvert(hex2) + 1;
                    int h3 = HexConvert(hex3);

                    int combinedID = (h1 << 26) | (h2 << 8) | (h3);

                    status1 = Canlib.canWrite(handleWrite, combinedID, data, data.Length, Canlib.canMSG_EXT);

                    if (status == Canlib.canStatus.canOK && status1 == Canlib.canStatus.canOK)
                    {
                        richTextBox.Invoke(new Action(() =>
                        {
                            richTextBox.AppendText($" {id}       {data[0]:x2}      {data[1]:x2}      {data[2]:x2}      {data[3]:x2}      {data[4]:x2}      {data[5]:x2}      {data[6]:x2}      {data[7]:x2}\n");
                            richTextBox.AppendText($"   {h1:X}   {h2:X}   {h3:X}          {data[0]:x2}      {data[1]:x2}      {data[2]:x2}      {data[3]:x2}      {data[4]:x2}      {data[5]:x2}      {data[6]:x2}      {data[7]:x2}\n");
                            richTextBox.SelectionStart = richTextBox.Text.Length;
                            richTextBox.ScrollToCaret();
                        }));
                    }
                    else if (status != Canlib.canStatus.canERR_NOMSG)
                    {
                        MessageBox.Show($"Error reading or writing CAN message");
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error: {ex.Message}");
            }
        }
    }
}

 

Thanks!


Answers (1)