0

I have been learning before about threading but in C++. Now for the first time I'm trying to configure this code to work corectly but without CheckIllegal ... = false. I have been trying to put delegates in there and lots of other stuff but the I getting the same problem. Both threads are entering the methof WriteInLog and I can't really see how to make this work. Anyone has an idea and explanation?

namespace viseNitniRad
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();


        }
        private int firstNum = 0;
        private int secondNum = 0;
        public Thread firstThread;
        public Thread secondThread;

        public  void WriteInLog(string message)
        {
            lock (textBox3)
            {
                textBox3.Text += message + Environment.NewLine;
            }
        }

        private void CheckInput()
        {
            int pom = 0;
            firstNum = int.Parse(textBox1.Text);
            secondNum = int.Parse(textBox2.Text);
            if (firstNum > secondNum) {
                pom = secondNum;
                secondNum = firstNum;
                firstNum = pom; }

            WriteInLog("Prvi broj: " + firstNum.ToString());
            WriteInLog("Drugi broj: " + secondNum.ToString());
        }


        private void button1_Click(object sender, EventArgs e)
        {
            CheckInput();

        }

        public delegate void ThreadSum();
        public delegate void ThreadUmn();
        public void Threadsumm()
        {
            int suma = 0;
            for (int i = firstNum; i < secondNum; i++)
                suma += i;
            WriteInLog("Suma= " + suma.ToString() + " kraj: " + DateTime.Now.ToString());

        }

        public  void ThreadUmno()
        {
            int umnozak = 1;
            for (int i = firstNum; i < secondNum; i++)
                umnozak*= i;
            WriteInLog("Umnozak= " + umnozak.ToString() + " kraj: " + DateTime.Now.ToString());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            WriteInLog("Pocetak svih izracuna u: " + DateTime.Now.ToString());

            firstThread = new Thread(new ThreadStart(Threadsumm));
          secondThread = new Thread(new ThreadStart(ThreadUmno));


            firstThread.Start();
            secondThread.Start();


        }
    }
}
Shelby115
  • 2,816
  • 3
  • 36
  • 52
iMajna
  • 489
  • 4
  • 28

2 Answers2

1

To access control from non-UI thread you have to use Control.Invoke method https://msdn.microsoft.com/en-us/library/a1hetckb(v=vs.110).aspx

Instead of Threads you can use BackgroundWorker https://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx

Also: WinForm Multithreading. Use backgroundWorker or not?

Community
  • 1
  • 1
Dmitry Kolchev
  • 2,116
  • 14
  • 16
0

If anyone comes here and want to know exactly the answer on this question, here it is. I didn't want to change anything so I learn something about invoke. Furthermore, here's the code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;


namespace viseNitniRad
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();


        }
        private int firstNum = 0;
        private int secondNum = 0;
        public Thread firstThread;
        public Thread secondThread;

        public  void WriteInLog(string message)
        {
            if (this.textBox3.InvokeRequired)
            {
                ThreadSum ts = new ThreadSum(WriteInLog);
                this.Invoke(ts, new object[] { message });

            }
            else
            {
                this.textBox3.Text += message + Environment.NewLine;
            }
        }

        private void CheckInput()
        {
            int pom = 0;
            firstNum = int.Parse(textBox1.Text);
            secondNum = int.Parse(textBox2.Text);
            if (firstNum > secondNum) {
                pom = secondNum;
                secondNum = firstNum;
                firstNum = pom; }

            WriteInLog("Prvi broj: " + firstNum.ToString());
            WriteInLog("Drugi broj: " + secondNum.ToString());
        }


        private void button1_Click(object sender, EventArgs e)
        {
            CheckInput();

    }
        public delegate void ThreadSum(string message);
        public delegate void ThreadUmn();
        public void Threadsumm()
        {
            int suma = 0;
            for (int i = firstNum; i < secondNum; i++)
                suma += i;
            WriteInLog("Suma= " + suma.ToString() + " kraj: " + DateTime.Now.ToString());

        }
        public  void ThreadUmno()
        {
            int umnozak = 1;
            for (int i = firstNum; i < secondNum; i++)
                umnozak*= i;
            WriteInLog("Umnozak= " + umnozak.ToString() + " kraj: " + DateTime.Now.ToString());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            WriteInLog("Pocetak svih izracuna u: " + DateTime.Now.ToString());

            firstThread = new Thread(new ThreadStart(Threadsumm));
          secondThread = new Thread(new ThreadStart(ThreadUmno));


            firstThread.Start();

            secondThread.Start();


        }
    }
}

As you can see I made little change in function WriteInLog, where I deleted lock method and put an condition if invokeRequired to initialize Delegate to run the same method and than Invoke him, if not, just to update the same textbox3. Thank you all! :)

iMajna
  • 489
  • 4
  • 28