A
A
armadillo-cld2020-06-04 08:48:25
C++ / C#
armadillo-cld, 2020-06-04 08:48:25

How to make multithreading so that the program does not hang?

Hello! How to do multithreading correctly so that the program does not hang? I made a list , put the threads there, and then stopped them all by clicking on the button by looping through them threads[i].Abort(). The thread function looked like this:List<Thread> threads = new List<threads>();

void foo(object obj) {
   while(true) {
       FooStruct fs = (FooStruct)obj;
       AddLog(fs.arg1);
       AddLog(fs.arg2);
       AddLog(fs.arg3;
  }
}

This function outputs text in richtextbox. But when the user launches a lot of such threads (1k each), the program freezes tightly. How to do that no matter how much you run - the program worked them out? Thanks everyone! If anything, I need this for the site parser, site urls are passed into the arguments, but I gave an example a little easier.

UPD: I tried to do Thread.Sleep. Same thing

Answer the question

In order to leave comments, you need to log in

4 answer(s)
F
freeExec, 2020-06-04
@freeExec

Threads need to be run at 1k, and a little less than the cores in the system, so that the system does not get up with a stake from 100% core load. For example, for 8 cores, 6 threads will be optimal, then the user will not notice the load at all, except for the buzzing of the fan.

P
Peter, 2020-06-04
@petermzg

There should be one UI thread and only it is responsible for updating controls such as richtextbox.
Base/standard controls usually have a check to see if their methods are called from the UI thread and this check will throw an exception.
For this reason, you must use synchronization mechanisms that allow you to access controls from the UI thread.
PS: Use synchronization objects like Event so that threads terminate normally instead of being aborted via Abort()

I
Ilya, 2020-06-04
@sarapinit

Read about how to properly use the TPL (Task Parallel Library) and the thread pool. Don't use pure Thread until you understand how threads work.

I
itgood, 2020-06-05
@itgood

read about asynchrony

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question