Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.4 kB
1
Indexable
Never
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Threading;

class Program
{
    static ConcurrentQueue<string> dataQueue = new ConcurrentQueue<string>();
    static object lockObject = new object();
    static event Action<string> DataProduced;
    static int dataId = 0;

    static void Main(string[] args)
    {
        var producerThread = new Thread(ProducerThread);
        producerThread.Start();

        DataProduced += ConsumerMethod;

        Console.WriteLine("Press Enter to stop the program.");
        Console.ReadLine();

        producerThread.Join();
    }

    static void ProducerThread()
    {
        var random = new Random();
        while (true)
        {
            var data = GenerateRandomData(50 * 1024); // 50 KB
            DataProduced?.Invoke(data);
            Thread.Sleep(50000); // Sleep for 50 seconds
        }
    }

    static void ConsumerMethod(string data)
    {
        lock (lockObject)
        {
            var id = Interlocked.Increment(ref dataId);
            dataQueue.Enqueue($"{id},{data}");
            Console.WriteLine($"Data with ID {id} added to the queue.");
        }
    }

    static string GenerateRandomData(int size)
    {
        var buffer = new byte[size];
        var random = new Random();
        random.NextBytes(buffer);
        return Convert.ToBase64String(buffer);
    }
}