Restarting a stream with different inputs

First of all, I apologize if this or a similar question was asked earlier. It seemed to be, but I could not find it.

I have a third-party assembly with the method I call, this method has parameters containing a list of objects (in concept, not List<object>) and returns the result after a potentially long algorithm.

I call this method on another thread to keep the interface responsive. And it is called quite regularly and with a different list of values. This means that it often works when I want to restart it. The problem is that creating a new thread is pretty naive, so every time it starts with a new one - this leads to the fact that several threads work with different input parameters. I really want all existing threads to die, as I am no longer interested in exiting them.

I am not very familiar with the advanced methods of multi-threaded processing, so I would really like some suggestions regarding the best approach to this problem.

(I use 3.5, so there is no TPL)

EDIT I split Reflector to see what is going on inside and most of the code is marked protected, so I'm pretty sure I can inherit and terminate the call in the check to exit earlier, in accordance with the answers below.

+3
source share
3 answers

You can consider this as a manufacturer / consumer sample.

Instead of starting Thread, send (create) a new Datapacket.

The consuming thread must monitor the queue and start a new packet when it arrives.

Note that stopping the Thread is your main problem. Don't even look at Thread.Abort (). You need to collect this logic into stream code.

+4
source

. , , . , - , . , "" (, ). .

+2

:

  • ?
  • , ? (, , )

, . - , , . ( , VM .)

- , . , Windows , - . . - TCP (.NET - WCF). , , , .

+1
source

Source: https://habr.com/ru/post/1795235/


All Articles