A
A
Alexey G2022-03-05 15:39:43
C++ / C#
Alexey G, 2022-03-05 15:39:43

[c#] How to rewrite a multi-threaded application into an asynchronous one?

There is a server - a multi-threaded application, in which there are worker threads that constantly obtain information, and the main thread with networking and waiting for a shutdown signal.

Question: what needs to be changed to rewrite the program using asynchrony (async/await)?

below is the simplified application code.

using System;
using System.Threading;
namespace testthread;

class WorkClass {
    int Data;
    readonly ManualResetEvent _mainExitEvent;
    readonly ManualResetEvent _workEndedEvent;
    public WorkClass(int data, ManualResetEvent mainExitEvent, ManualResetEvent workEndedEvent) {
        Data = data;
        _mainExitEvent = mainExitEvent;
        _workEndedEvent = workEndedEvent;
    }
    public void ThreadProc() {
        Console.WriteLine($"thread {Data} started");
        var r = new Random();
        do {
            Thread.Sleep(2000); // очень важная работа          
        }
        while (!_mainExitEvent.WaitOne(r.Next(2000)));
        Console.WriteLine($"thread {Data} stopped");
        _workEndedEvent.Set(); // событие того, что работа зкончена
    }
}

public class Program { 
    public static void Main(string[] args) {
        ManualResetEvent mainExitEvent = new(false);
        const int THREAD_COUNT = 10;
        ManualResetEvent [] workEndEvents = new ManualResetEvent[THREAD_COUNT];
        for (int i = 0; i < THREAD_COUNT; i++) {
            workEndEvents[i] = new ManualResetEvent(false);
            WorkClass work = new WorkClass(i, mainExitEvent, workEndEvents[i]);
            Thread workThread = new Thread(new ThreadStart(work.ThreadProc));
            workThread.Start();
        }
        Thread.Sleep(10);
        Console.WriteLine("press any key to exit from main thread");

        // tcpListener.Start();
        // modbus_network.ListenAsync();

        Console.ReadKey();  // по какому-то условию заканчиваем работу
        Console.WriteLine("trying to exit:");
        mainExitEvent.Set();
        Thread.Sleep(10);
        WaitHandle.WaitAll(workEndEvents);
        Console.WriteLine("All exited!");
    }
}


I read about asynchrony, but I don’t understand how and how it will help here.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rPman, 2022-03-05
@rumactep

the purpose of translating generic thread-based code to asynchronous code, whose normal operation is allowed with a bunch of reservations, is not clear.
Maybe it's better to leave it as it is?
The code shows that the thread has its own event loop, which is good, it means that the work is already divided into pieces
, so you have to scatter these pieces of important work into events (i.e., for example, calling each new piece is a new event, so you need to formulate the condition is the creation of the next event, for example, if it's just 'calculations' then you create events in turn for a piece of work from each thread so that they are executed in turn)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question