locked
Parallel thread execution in C# RRS feed

  • Question

  • User1960812541 posted

    I have a problem as described below:

    I have list of data from database which will be converted to list of objects. While this conversion is going on, I need to start a separate thread with certain number of objects in the list for further processing. So,now I need the conversion process and the processing of data to run parallel. It may happen that when list of objects reach a  threshold level, another thread will start with next set of objects.

    How can I achieve this in C#? Will the use of async methods will help in this scenario?

    Please guide me with example.

    Wednesday, March 15, 2017 11:02 AM

All replies

  • User765422875 posted

    You want to look into using the Task Parallel Library (TPL), specifically Parallel.ForEach or a Parallel.For. Async methods will not help you with the parallel loops / data processing. The point of an async  Task is to let it execute in the background without blocking on the main thread. You use it when you are dealing with I/O bound resources such as file access and database access. Async DB commands are critical in achieving scale, throughput and latency so you should use them for the ADO.NET database calls.

    Example of an async ADO.NET database call:

            public async Task<List<SomeObject>> GetData()
            {
                var results = new List<SomeObject>();
                using (var connection = new SqlConnection("YOUR CONNECTION STRING"))
                {
                    await connection.OpenAsync().ConfigureAwait(false);
                    using (var command = connection.CreateCommand())
                    {
                        command.CommandText = "Select id, FirstName, LastName From [dbo].[Table];";
                        var reader = await command.ExecuteReaderAsync();
    
                        while (await reader.ReadAsync())
                        {
                            var someObject = new SomeObject
                            {
                                Id = reader.GetInt32(0),
                                FirstName = reader.GetString(1),
                                LastName = reader.GetString(2)
                            };
    
                            results.Add(someObject);
                        }
                    }
                }
    
                return results;
            }

    example of a simple Parralel.ForEeach. If you run the code, you'll see that the items are not processed sequentially.

                List<string> someList = new List<string>
                {
                    "this",
                    "is",
                    "a",
                    "randome",
                    "sentance"
                };
    
                Parallel.ForEach(someList, item =>
                {
                    Console.WriteLine("Item {0} has {1} characters",
                        item, item.Length);
                });

    example with thread safe list manipulation

                List<object> list = new List<object>();
                object listLock = new object();
                Parallel.ForEach(transactions, tran =>
                {
                    lock (listLock)
                    {
                        list.Add(new object());
                    }          
                });

    example with thread safe collection - ConcurrentBag

                ConcurrentBag<object> list = new ConcurrentBag<object>();
    
                Parallel.ForEach(transactions, tran =>
                {
                    list.Add(new object());   
                });

    Wednesday, March 15, 2017 7:51 PM