标签: queueuserworkitem

ThreadPool.QueueUserWorkItem和Parallel.ForEach之间的区别?

以下两种方法之间的主要区别是什么:

ThreadPool.QueueUserWorkItem

    Clients objClient = new Clients();
    List<Clients> objClientList = Clients.GetClientList();

    foreach (var list in objClientList)
    {
        ThreadPool.QueueUserWorkItem(new WaitCallback(SendFilesToClient), list);
    } 
Run Code Online (Sandbox Code Playgroud)

System.Threading.Tasks.Parallel ForEach

    Clients objClient = new Clients();
    List<Clients> objClientList = Clients.GetClientList();

    Parallel.ForEach<Clients>(objClientList, list =>
    {
        SendFilesToClient(list);
    });
Run Code Online (Sandbox Code Playgroud)

我是多线程新手,想知道在每种情况下会发生什么(在执行过程方面)每种方法的多线程水平是多少?帮助我想象这两个过程.

SendFilesToClient:从数据库获取数据,转换为Excel并将Excel文件发送到相应的客户端.

谢谢!

c# multithreading windows-services queueuserworkitem parallel.foreach

22
推荐指数
1
解决办法
6614
查看次数

C#使用ThreadPool时,我可以将多个数据传递给我的目标方法吗?

用于使用ThreadPool.QueueUserWorkItem (WaitCallback, Object)我的目标方法和数据启动一个线程.我可以将多个数据传递给我的方法吗?第二个参数QueueUserWorkItem (WaitCallback, Object)可以是数组吗?

c# multithreading threadpool queueuserworkitem

5
推荐指数
1
解决办法
5048
查看次数

如何在Windows服务中使用Threadpool.QueueUserWorkItem?

我有一个Windows服务,我在使用Threadpool.QueueUserWorkItem.该服务连接到多个客户端数据库,抓取数据,转换为XLS并将文件发送到相应的FTP.

关于以下代码我有3个问题:

  1. 我正确使用Threadpool.QueueUserWorkItem吗?
  2. 我是否需要在代码中的任何位置使用Lock以避免出现问题?如果是,那里和哪个对象.
  3. 代码中有什么不正确的东西吗?如果是,那该怎么办?

码:

private static System.Timers.Timer aTimer = new System.Timers.Timer(50000);

public void OnStart(string[] args)
        {
            CLE.WriteToEventLog("Service Started");
            try
            {
                aTimer.Elapsed += new ElapsedEventHandler(PerformTimerOperation);
                aTimer.Enabled = true;
            }
            catch (Exception ex)
            {
                CLE.WriteToEventLog("Error Starting Service: " + ex.Message);
            }
        }

private void PerformTimerOperation(object source, ElapsedEventArgs e)
        {
            CLE.WriteToEventLog("Timer Operation Started");
                Clients objClient = new Clients();
                List<Clients> objClientList = Clients.GetClientList();

                foreach (var list in objClientList)
                {
                    ThreadPool.QueueUserWorkItem(new WaitCallback(SendFilesToClient), list);
                }                
        }

private void SendFilesToClient(Object stateInfo)
        {
            CLE.WriteToEventLog("Send Files To …
Run Code Online (Sandbox Code Playgroud)

multithreading windows-services queueuserworkitem c#-4.0

2
推荐指数
1
解决办法
5411
查看次数