Home > Blockchain >  Execute threads parallel to the execution of the main thread
Execute threads parallel to the execution of the main thread

Time:12-23

I have a form in C#, said form only has a multiline text box.

In the load method of this form I need to create 3 worker threads. These threads will call the Counter class where they will execute a method called count.

This method is just a counter that will repeat the task 10000 times and its only task is to call a function in the Form main class which should print a message in the text box.

The message must be the number of the thread that executes the task and the number of the counter.

It is supposed that with the invoke method this would be done in the main thread leaving the other threads working in the background, but what happens is that my form stays frozen and I don't understand why.

I leave you my code, I hope you can help me.

Form Main Class

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

    private void Form1_Load(object sender, EventArgs e)
    {
      Contador contadorClassOne = new(1, this);
      Contador contadorClassTwo = new(2, this);
      // Crea y arranca 2 hilos

      Task t1 = Task.Run(() => {
        contadorClassOne.contar();
      });

      Task t2 = Task.Run(() => {
        contadorClassTwo.contar();
      });
    }

    public void showMessage(String Mensaje)
    {
      this.Invoke(()=>
      {
        txtMultiLineTextBox.Text = Mensaje   "\r\n"   txtMultiLineTextBox.Text;
      });
    }
  }
}

Counter Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestMultiHiloCSharp
{
  internal class Contador
  {
    int numHilo;
    Form1 thisForm;

    public Contador(int numHilo,Form1 thisForm)
    {
      this.numHilo = numHilo;
      this.thisForm = thisForm;
    }

    public void contar(){
      int cont = 0;
      while (cont < 100)
      {
        thisForm.showMessage("this is the theread: "   this.numHilo   " and the number: "   cont);
        cont  ;
      }
    }
  }
}

CodePudding user response:

The reason your code is running slow is this line:

txtMultiLineTextBox.Text = Mensaje   "\r\n"   txtMultiLineTextBox.Text;

When you set the constant in your while loop to any sizeable number - I found 1000 or more to be sizeable - then there there is a lot of work to build the string and then for the TextBox to display that string.

It will run fast if you just write this:

txtMultiLineTextBox.Text = Mensaje;

Your problem is the massive string.

CodePudding user response:

I don't know if it helps but don't call directly the showMessage from task. If you want to update the main form raise Event.

CodePudding user response:

Your program works correctly.

Maybe some small adjustments can be made like,

  • change this.Invoke in this.txtMultiLineTextBox.Invoke
  • use something like txtMultiLineTextBox.AppendText

Further the tasks started in Form_Load messes up your form initialization a bit. For testing use a button. But there is no freez at this side. I tested your exact code on .NET 7, output gives me:


this is the theread: 2 and the number: 99
this is the theread: 1 and the number: 99
this is the theread: 2 and the number: 98
this is the theread: 1 and the number: 98
this is the theread: 2 and the number: 97
this is the theread: 1 and the number: 97
this is the theread: 2 and the number: 96
this is the theread: 1 and the number: 96
.....
this is the theread: 2 and the number: 1
this is the theread: 1 and the number: 1
this is the theread: 2 and the number: 0
this is the theread: 1 and the number: 0
  • Related