简体   繁体   中英

Async with Infinite Loop

I have a list of data sets that need to be updated real time. I would like to process 10 out of 100+ at a time and then once one is done grab the next oldest line. Basically keep this loop going for an infinite amount of time. I am quite new to the world of Threading and been poking around at AsyncTask. Is there an example of this anyone can point me to? I have googled quite a bit but can't find exactly what i'm looking for.

At least with .Net 4.5 async programming has becoming very sugary if you know what I mean.

Here is a simple set of examples:

public async void DoAsync()
{
      await Task.Run( () =>
      { 
         // Do task! 
      } );
}

public async Task<string> GetStringAsync()
{
       string s = "";
       await Task.Run( () =>
       {
            for(int I = 0; I < 9999999; I++)
            {
                   s += I.ToString();
            }
       }
       return s;
}

Helpful Resources

http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx

AsyncTask is better suited to one-off operations. For an ongoing task you can consider a worker thread.

Disclaimer: I don't claim this is the best way to do it, but it should give you some ideas and stuff to read up on.

    public class ThreadingSample : IDisposable
{
    private Queue<SomeObject> _processingQueue = new Queue<SomeObject>();
    private Thread _worker;
    private volatile bool _workerTerminateSignal = false;
    private EventWaitHandle _waitHandle = new EventWaitHandle(false, EventResetMode.AutoReset);

    public bool HasQueuedItem
    {
        get
        {
            lock(_processingQueue)
            {
                return _processingQueue.Any();
            }
        }
    }

    public SomeObject NextQueuedItem
    {
        get
        {
            if ( !HasQueuedItem )
                return null;

            lock(_processingQueue)
            {
                return _processingQueue.Dequeue();
            }
        }
    }

    public void AddItem(SomeObject item)
    {
        lock(_processingQueue)
        {
            _processingQueue.Enqueue(item);
        }
        _waitHandle.Set();
    }
    public ThreadingSample()
    {
        _worker = new Thread(ProcessQueue);
        _worker.Start();
    }

    private void ProcessQueue()
    {
        while(!_workerTerminateSignal)
        {
            if ( !HasQueuedItem )
            {
                Console.WriteLine("No items, waiting.");
                _waitHandle.WaitOne();
                Console.WriteLine("Waking up...");
            }
            var item = NextQueuedItem;
            if (item != null)   // Item can be missing if woken up when the worker is being cleaned up and closed.
                Console.WriteLine(string.Format("Worker processing item: {0}", item.Data));
        }
    }

    public void Dispose()
    {
        if (_worker != null)
        {
            _workerTerminateSignal = true;
            _waitHandle.Set();
            if ( !_worker.Join( TimeSpan.FromMinutes( 1 ) ) )
            {
                Console.WriteLine("Worker busy, aborting the thread.");
                _worker.Abort();
            }
            _worker = null;
        }
    }

    public class SomeObject
    {
        public string Data
        {
            get;
            set;
        }
    }
}

Testing it I use a Unit test to kick it off. You can extend the unit test into a proper test to ensure that actions are being performed as expected. In my case they're a good initial assertion to spike out behaviour.

        [Test]
    public void TestThreading()
    {
        using ( var sample = new ThreadingSample() )
        {
            sample.AddItem(new ThreadingSample.SomeObject {Data = "First Item"});
            sample.AddItem(new ThreadingSample.SomeObject {Data = "Second Item"});
            Thread.Sleep(50);
            sample.AddItem(new ThreadingSample.SomeObject {Data = "Third Item"});
        }

    }

Relevant output from the test:

------ Test started: Assembly: NHMapping.dll ------

Worker processing item: First Item
Worker processing item: Second Item
No items, waiting.
Waking up...
No items, waiting.
Waking up...
Worker processing item: Third Item
No items, waiting.
Waking up...

1 passed, 0 failed, 0 skipped, took 0.12 seconds (Ad hoc).

Here you can see the worker going to sleep, then waking up to process items in the queue. Technically you can use a list, then fetch 10 items from the list before releasing it from the lock and process those 10 items before checking the list again.

When the class is disposed it releases the loop then waits a moment for the worker thread to terminate before aborting. Here you'd probably want to check for any outstanding items and either log that they will not be processed, or persist them to file for later processing.

Edit: I found the issue with the double-event... A better implementation would be to use a ManualReset on the EventWaitHandle

private EventWaitHandle _waitHandle = new EventWaitHandle(false, EventResetMode.ManualReset);

Then when handling the case where you process an item, resent the handle:

                var item = NextQueuedItem;
            if (item != null)   // Item can be missing if woken up when the worker is being cleaned up and closed.
            {
                Console.WriteLine(string.Format("Worker processing item: {0}", item.Data));
                _waitHandle.Reset();
            }

This produces the better test results:

------ Test started: Assembly: NHMapping.dll ------

Worker processing item: First Item
Worker processing item: Second Item
No items, waiting.
Waking up...
Worker processing item: Third Item
No items, waiting.
Waking up...

1 passed, 0 failed, 0 skipped, took 0.13 seconds (Ad hoc).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM