1

I am using a concurrent queue in my C# Web API. I want to handle the situation where in case of a graceful shutdown, I should be able to empty my queue, process it and dispose it off.

Below is my code snippet where I am trying to dispose and write last message to the file but I am unable debug or get dispose file created when I manually stop IIS App pool.

Any suggestions?

 public static void SendToDisposeFile()
    {
        File.AppendAllText(Path.Combine(Path.Combine(@"D:\", "Input"), "dispose-input-file-" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".txt"), "Disposed text" + Environment.NewLine);          

    }
 #region IDisposable implementation

    ~RequestQueue()
    {
        Dispose(false);
    }

    public void Dispose()
    {            
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (_disposed)
            return;

        _disposed = true;

        if (!disposing)
            return;
        if(_queue.Count > 0)
            SendToDisposeFile();           
      
    }

    #endregion
  • This might be helpful https://social.msdn.microsoft.com/Forums/en-US/accf4254-ee81-4059-9251-619bc6bbeadf/clear-a-concurrentqueue?forum=rx – David Tansey Jul 09 '20 at 18:34

0 Answers0