Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将用户操作异步插入数据库,而不产生大量线程C_C#_Multithreading_Blockingcollection - Fatal编程技术网

C# 将用户操作异步插入数据库,而不产生大量线程C

C# 将用户操作异步插入数据库,而不产生大量线程C,c#,multithreading,blockingcollection,C#,Multithreading,Blockingcollection,我正在尝试建立这个分析系统,它将用户执行的特定操作的数据推送到数据库 将有多个操作需要被记录,因此这需要异步完成,但它不能产生许多线程,需要低优先级而不影响系统性能,我正在考虑使用阻塞收集,即在收集中不断推送数据并拾取它们以保存到数据库 我真的不知道如何实现这一点,有人能给我一个例子,说明我如何实现这一点吗?嗨,你的问题既不具体,也没有很好的表述,因此我提出了这个问题 尽管如此,这里还是有一些代码行可以帮助您开始: using System.Threading; using System.Dia

我正在尝试建立这个分析系统,它将用户执行的特定操作的数据推送到数据库

将有多个操作需要被记录,因此这需要异步完成,但它不能产生许多线程,需要低优先级而不影响系统性能,我正在考虑使用阻塞收集,即在收集中不断推送数据并拾取它们以保存到数据库


我真的不知道如何实现这一点,有人能给我一个例子,说明我如何实现这一点吗?

嗨,你的问题既不具体,也没有很好的表述,因此我提出了这个问题

尽管如此,这里还是有一些代码行可以帮助您开始:

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
        }
    }
}

不要担心创建自己的线程太低级;只需使用线程池或任务对象,并让.NET类库处理细节。您的想法已经是正确的了。采取一些阻塞队列,或者只是锁定所有调用并使用一个writer线程,该线程将一个项目从队列中出列,并将其放在数据库中,或者为了性能起见,一次多个/所有可用条目。