'How to sync shared variables using Locks accessed by two Net6 Worker services?


IHost host = Host.CreateDefaultBuilder(args)
    .ConfigureServices(services =>
    {
        services.AddHostedService<Worker1>();
        services.AddHostedService<Worker2>();
    })
    .Build();

await host.RunAsync();

In VS2022, inside Program.cs(the above is its whole content), I couldn't even declare the gloabal static variables (to be shared and updated by the two Worker), how to do that so that I can use Locks to sync updates between the two Workers. Any help will be appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source