开发者

C# - Multithreading with one thread running endless loop

I'm new to multithreading (and a somewhat beginner / intermediate programmer), and so I may be missing something very obvious. I'm currently working on an application that has to pull data off a server (a bloomberg server to be exact) and also run business logic.

To pull data off the server, you basically have to run an endless loop to constantly refresh the data. Currently, all the data pull code / logic is in one seperate class, and has a number of public objects (dataDict in code below) that get updated with the newest data.

My thought was to run the data pull part of the program on a seperate thread, and have the rest of the business logic run on the main thread. Then, whenever the business logic required the most recent data, it could just call the data object from the other thread (a dictionary). In regards to the code below, I would like that class to run in the background and have the main business logic pick up the Dictionary, Object> dataDict when required. I'm really not sure how to best do this though.

I tried using a BackgroundWorker, but the issue that I ran into was that because the loop was endless I could never fire the RunWorkerCompleted event handler, and the DoWork handler would be called too soon (it takes some time for the data to fully download).

Any help would be much appreciated!!

To illustrate part of the program, the code for the datapull is (note, I had to do some editing so the braces/brackets may not be perfect):

class BloombergSync
{
    private Session mainSession = new Session();
    private List<String> contracts = new List<string>();
    private List<String> fields = new List<string>();
    public List<String> safeContracts = new List<string>();
    public List<String> safeFields = new List<string>();
    public Dictionary<Tuple<string, string>, Object> dataDict = new Dictionary<Tuple<string, string>, object>();
    private BackgroundWorker worker = new BackgroundWorker();


    {

        while (true)
        {

            try
            {
                Event eventObj = mainSession.NextEvent();

                foreach (Message msg in eventObj.GetMessages())
                {
                    if (eventObj.Type == Event.EventType.SUBSCRIPTION_DATA)
                    {
                        Element dataElement = msg.AsElement;

                        //Figures out which contract the data is in reference to.
                        string topic = msg.TopicName;

                        // Then must add the data to the appropriate contract and field. 
                        foreach (string field in fields)
                        {
                            if (dataElement.HasElement(field, true))
                            {
                                // Collects data for the field
                                Element elm = dataElement.GetElement(field);
                                var dataPoint = elm.GetValue();

                                // Have to figure out how to select first topic, and then the field within that topic. Has
                                // two keys (first topic, then field).
                                Tuple<string, string> tuple = new Tuple<string, string>(topic, field);
                                dataDict[tuple] = dataPoint;
                                worker.ReportProgress(1);
                            }

                        }

                     }
                    else
                    {
                        //Do Nothing if not data
                    }
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("Got Exception:" + ex);
            }开发者_如何学运维

        }
    }


Sounds like a producer-consumer scenario. You need a thread-safe object (in your case a queue) for transferring data.

In .NET 4.0 there's a System.Collections.Concurrent which is thread-safe and you can use in your project. Then you background worker can put data in CuncurrentQueue and consumer(s) can dequeue data items.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜