如何在此代码中添加lock

如何在此代码中添加lock

本文介绍了如何在此代码中添加lock / async / await或线程概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想致电



(1)GetMessages()这将继续将消息带入单独的线程



(2)GetOrderData()这将获取订单详细信息并将其插入到datagrid中,其任务在此处完成



(3)UpdateOrderBook()现在,这将更新现有的datagrid(已经有来自第2步的一些数据)。在这个方法中,我想检查队列,并且必须迭代concurrentqueue然后进行一些处理,然后在现有网格中插入该记录。



这两个过程( 1,3)会以异步方式运行,但是第一次它应该按照上面的顺序进行处理。



我被困在让它们异步运行。





这是第一次完美无缺。但是当我改变产品并重新连接东西时,数据处理不当。



我是多线程的新手,所以不确定我是否需要使用 lock async / await 或其他。我在上面的代码中做错了什么?



我尝试了什么:



I want to call

(1) GetMessages() this will keep bringing the messages in separate thread

(2) GetOrderData() this will fetch the order details and insert them in datagrid and the its task is finished here

(3) UpdateOrderBook() this will now update the existing datagrid (which already has some data from step 2). Here in this method I want to check the queue and have to iterate the concurrentqueue then do some processing and then insert that record in the existing grid.

The two process (1,3) would run asynchronously but for the first time it should process in above order.

I got stuck in making them run asynchronously.


This works perfectly for the first time. But when I change the product and reconnect the things mess up and the data is not processed properly.

I am new to multi-threading so not sure if I need to use lock or async/await or something else. What am I doing wrong in the above code?

What I have tried:

void OnReceivingFeedMessage(message)
    {
         concurrentQueue.Enqueue(message);

         if (!messageStreamStarted)   // only first time get order book
         {
             messageStreamStarted = true;
             GetOrderBookData();
         }
    }

    private void GetOrderBookData()
    {
        MarketData m = new MarketData();
        ProductOrderBook p = m.GetProductOrderBook(productId);
        bidsList = p.bids;
        asksList = p.asks;
        isOrderBookUpdated = true;

        Task task3 = Task.Run(() => KickStartToProcessQueue());
    }

    private void KickStartToProcessQueue()
    {
        while (threadProcessQueueExist)
        {
            int recordCountNew = concurrentQueue.Count();
            if (recordCountNew != 0)
            {
                if (isOrderBookUpdated)
                {
                    ProcessQueueMessages();
                }
            }
        }
    }

    private void ProcessQueueMessages()
    {
        if (!concurrentQueue.IsEmpty)
        {
            string jsonString;
            while (concurrentQueue.TryDequeue(out jsonString))
            {
              // have to insert the record in existing order book
            }
         }
    }

// The code written on product selectedindex change

    private void CloseAndReconnectToGetWebsocketFeed()
    {
        w.CloseWebsocketConnection();
        messageStreamStarted = false;
        isOrderBookUpdated = false;
        ConcurrentQueue<string> wssMessagesQueue = new ConcurrentQueue<string>();
        concurrentQueue = wssMessagesQueue;

        ConnectAndGetWebsocketFeedMessages(); // this calls OnReceivingFeedMessage
    }

推荐答案



这篇关于如何在此代码中添加lock / async / await或线程概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:00