-3

I'm trying to build this analytical system, this pushes data to the database on specific actions user performs.

There will be multiple actions which will need to be logged, hence this needs to be asynchrously done, but it can't spawn off many threads, need this to be at low priority not hampering performance of the system, I was thinking of using blocking collection, i.e. keep pushing data in the collection and picking them to save to the database.

I haven't really gotten how to implement this, can anyone give me an example of how I can achieve this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user1393503
  • 275
  • 1
  • 4
  • 10
  • Don't worry about creating your own threads (too low-level); just use the `ThreadPool`, or `Task` objects, and let the .NET class library deal with the details. – stakx - no longer contributing Jun 29 '15 at 11:04
  • Your thought is already proper. Take some blocking queue (or just lock all the calls) and use one writer-thread, which dequeues an item from the queue and put it on the the database (or, for the sake of performance, multiple/all available entries at a time). – Patrik Jun 29 '15 at 11:09

1 Answers1

1

Hi your question is neither specific nor well formulated (hence the votedowns).

Nonetheless here are some code lines that should get you started:

using System.Threading;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace ParallelTest
{
    static class Program
    {
        static readonly Stopwatch Stopwatch = new Stopwatch();

        static void Main()
        {
            Stopwatch.Start();
            var myList = new List<string>();
            for (var i = 0; i < 10000; i++)
                myList.Add(string.Format("String Item or Object Nr. {0:00000000}", i));

            Debug.WriteLine("End Create In-Memory List: {0}", Stopwatch.Elapsed);

            // NON-PARELLEL
            Stopwatch.Restart();
            myList.ForEach(item =>
            {
                // ... Do something
                Thread.Sleep(1);
            });

            Debug.WriteLine("NON-PARALLEL LIST: {0}", Stopwatch.Elapsed);
            // ------------------------------------------------------------------------


            // If you don't need to edit the collection at runtime you can use a normal 
            // list with Parallel options

            // PARALLEL (MULTITHREADED)
            // Example with unlimited Thread (CLR auto-manages, be careful 
            // with SQL-Connections)
            Stopwatch.Restart();
            Parallel.ForEach(myList, item =>
            {
                // ... Do something
                Thread.Sleep(1);
            });
            Debug.WriteLine("PARALLEL LIST: {0}", Stopwatch.Elapsed);
            // ------------------------------------------------------------------------


            // If you don't need to edit the collection at runtime you can use a normal 
            // list with Parallel options

            // PARALLEL WITH LIMITED THREADS
            // Example with 2 Threads
            Stopwatch.Restart();
            Parallel.ForEach(myList, 
                new ParallelOptions { MaxDegreeOfParallelism = 2 }, item =>
            {
                // ... Do something
                Thread.Sleep(1);
            });
            Debug.WriteLine("PARALLEL LIST 2 THREADS: {0}", Stopwatch.Elapsed);
            // ------------------------------------------------------------------------

            // If you need to make changes to the list during runtime you need to use 
            // a thread-safe collection type such as BlockingCollection
            // Avoid race-conditions

            // See examples under 
            // https://msdn.microsoft.com/en-us/library/dd997306(v=vs.110).aspx
        }
    }
}

System.Parallel on MSDN

KarmaEDV
  • 1,631
  • 16
  • 27