I am fighting with a tutorial from here:
https://www.geeksforgeeks.org/c-sharp-multithreading/
I try to reuse this code with windows forms, but with no success. I have modified the code to this form, using two richtextboxes:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
namespace Multithreading
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
Thread thr1 = new Thread(method1);
Thread thr2 = new Thread(method2);
thr1.Start();
thr2.Start();
}
public void method1()
{
// It prints numbers from 0 to 10
for (int I = 0; I <= 10; I++)
{
richTextBox1.Text += "Method1 is: "+ I + Environment.NewLine;
if (I == 5)
{
Thread.Sleep(6000);
}
}
}
public void method2()
{
// It prints numbers from 0 to 10
for (int J = 0; J <= 10; J++)
{
richTextBox2.Text += "Method2 is: "+ J + Environment.NewLine;
}
}
}
}
...and I get an error after 6 seconds:
Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on
I can't get why the first five numbers can be read (method 1) and all numbers from method 2 too, and after Thread.Sleep(6000)
there is an error :(
It's my first time with multithreading, any help will be appreciated.