Skip to content

ruchanb/csharp_queue

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C# Queue Application

This application demonstrates the implementation of a queue data structure in C#. It provides basic operations such as enqueue and dequeue

Features

  • Enqueue: Add an element to the end of the queue.
  • Dequeue: Remove and return the element at the front of the queue.
  • enqueued actions will run as fire and forget tasks

Requirements

  • .NET Core 7+ (change TargetFramework on .csproj file)

Getting Started

  1. Clone the repository:
    git clone https://github.com/yourusername/csharp_queue.git
  2. Navigate to the project directory:
    cd csharp_queue
  3. Build the project:
    dotnet build
  4. Run the application:
    dotnet run

Usage

The application provides a web api interface to interact with the queue. Follow the on-screen instructions to perform various queue operations.

How it works

  1. Uses QueuedBackgroundService HostedService from Microsoft. It runs on a loop to wait for any queue items.
  2. Create a queue using IBackgroundTaskQueue.QueueBackgroundWorkItem
    private readonly IBackgroundTaskQueue _taskQueue;

    public ExampleController(IBackgroundTaskQueue taskQueue)
    {
        _taskQueue = taskQueue;
    }

    [HttpPost("fire-and-forget")]
    public IActionResult FireAndForget([FromQuery] string message)
    {
        _taskQueue.QueueBackgroundWorkItem(async token =>
        {
            await Task.Delay(5000, token); // Simulated work
            Console.WriteLine($"Background task completed: {message}");
        });

        return Accepted(new { Message = "Background task queued" });
    }
  1. It uses SemaphoreSlim to handle access to the queue. Once it gives access the QueuedBackgroundService will run the action given to it.

Note: do not use any top level objects on the queued action. This will throw exception as the object will be disposed.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages