SoFunction
Updated on 2025-03-06

C# uses queues to solve simple concurrency problems

This article explains queues more specifically through examples. Queue represents a first-in-first-out object collection. When you need first-in-first out access to each item, use the queue. When you add an item to a list, it is called enqueue, and when you remove an item from the list, it is called dequeue.

There is a scenario: a rush to buy a project. Assume there are 5 items, whoever grabs it first can buy it. But if at this moment (assume it is the same time here), 100 people will grab this product, what will happen if they use the usual method? You know, what is said here is a question about concurrency.

Usually, when we go shopping in supermarkets and check out, we queue up. Here we first ask the buyers to queue up. According to the time, whoever clicks the buy button first will be ranked first. This will form a queue, and then we will deal with this queue so that there will be no concurrency problems. (At least it can handle such simple concurrency, and too complex concurrency is not discussed here)

Case:

Requirements: There is an interface for publishing articles. For each article published, call the interface. (No need to publish in batches here, to explain this)

Create a handler class like this.

namespace MyNameSpace  
 
{ 
  // Queue Temporary Class  public class QueueInfo 
  { 
    public string medias { get; set; } 
    public string proids { get; set; } 
    public string host { get; set; } 
    public string userid { get; set; } 
    public string feedid { get; set; } 
  } 
 
  public class BusinessInfoHelper 
  { 
    #region Solve the phenomenon of the front desk page stuck when the release contains high-quality media    //Principle: Use the producer and consumer model to perform listing operations 
    public readonly static BusinessInfoHelper Instance = new BusinessInfoHelper(); 
    private BusinessInfoHelper() 
    { } 
 
    private Queue<QueueInfo> ListQueue = new Queue<QueueInfo>(); 
 
    public void AddQueue(string medias, string proids, string host, string userid, string feedid) //Add to the list    { 
      QueueInfo queueinfo = new QueueInfo(); 
 
       = medias; 
       = proids; 
       = host; 
       = userid; 
       = feedid; 
      (queueinfo); 
    } 
 
    public void Start()//start up    { 
      Thread thread = new Thread(threadStart); 
       = true; 
      (); 
    } 
 
    private void threadStart() 
    { 
      while (true) 
      { 
        if ( > 0) 
        { 
          try 
          { 
            ScanQueue(); 
          } 
          catch (Exception ex) 
          { 
            LO_LogInfo.WLlog(()); 
          } 
        } 
        else 
        { 
          //No task, rest for 3 seconds          (3000); 
        } 
      } 
    } 
 
    // Method to be executed    private void ScanQueue() 
    { 
      while ( > 0) 
      { 
        try 
        { 
          //Fetch from the queue          QueueInfo queueinfo = (); 
 
          //The queueinfo you took out can be used, there is what you want.          //The following is the process          //。。。。。。 
 
        } 
        catch (Exception ex) 
        { 
          throw; 
        } 
      } 
    } 
 
 
    #endregion 
  } 
} 
 

After the above page is written, the thread must be started to process tasks continuously when the program starts running. Then we can write it in Global Application_Start as follows:

//Start and release high-quality media programs(); 

A problem arises. If I want to return the ID of this record after processing a record in the queue, the program seems to be unable to complete, so I use another method, Lock method, to lock the method, the specific one is as follows.

Define global locks in the page:

private static object lockObject= new Object(); 

In the method, call it like this:

lock(lockObject) 
 
{ 
 
//........ 
 
} 

The above is all content of this article, I hope it will be helpful for you to further study the queue.