0

Windows 服务需要根据客户端 ID 将多个文件发送到多个 FTP 文件夹。我的基本操作是定时器每 5 分钟调用一次PerformTimerOperation 。

客户端.xml

<clients>
  <client>
    <id>1</id>
    <name>client 1</name>
    <ftp>FTP URL1</ftp>
    <username>FTP USer1</username>
    <password>FTP Pass1</password>
  </client>
  <client>
    <id>2</id>
    <name>client 2</name>
    <ftp>FTP URL2</ftp>
    <username>FTP USer2</username>
    <password>FTP Pass2</password>
  </client>
</clients>

客户端.cs

public class Clients
    {
        public string ClientName { get; set; }
        public string ClientId { get; set; }
        public string FTP { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }

        public Clients()
        {
            ClientName = string.Empty;
            ClientId = string.Empty;
            FTP = string.Empty;
            Username = string.Empty;
            Password = string.Empty;
        }

        public List<Clients> GetClientList()
        {
            List<Clients> result;
            // load data file
            using (XmlTextReader xmlReader = new XmlTextReader("clients.xml"))
            {
                XDocument xdoc = XDocument.Load(xmlReader);

                var Clients = from clientItem in xdoc.Root.Elements()
                              select new Clients
                              {
                                  ClientName = clientItem.Element("name").Value,
                                  ClientId = clientItem.Element("id").Value,
                                  FTP = clientItem.Element("ftp").Value,
                                  Username = clientItem.Element("username").Value,
                                  Password = clientItem.Element("password").Value
                              };

                result = Clients.ToList();
            }
            return result;
        }
    }

服务实现.cs

    public void OnStart(string[] args)
            {
                System.Threading.Thread.Sleep(1000);
                keepLooping = true;

                //new System.Threading.Thread(PerformTimerOperation).Name = "Thread ";
                new System.Threading.Thread(PerformTimerOperation).Start();
            }


    PerformTimerOperation
            private void PerformTimerOperation(object state)
            {
                while (keepLooping)
                {
                    Clients objClient= new Clients();
                    List<Clients> objClientList = objClient.GetClientList();

                    foreach (var list in objClientList)
                    {
                        ConsoleHarness.WriteToConsole(ConsoleColor.Cyan, list.ClientId);
                        ConsoleHarness.WriteToConsole(ConsoleColor.Cyan, list.ClientName);
                        ConsoleHarness.WriteToConsole(ConsoleColor.Cyan, list.FTP);
                        ConsoleHarness.WriteToConsole(ConsoleColor.Cyan, list.Username);
                        ConsoleHarness.WriteToConsole(ConsoleColor.Cyan, list.Password);

***// Here I would like to call individual client database, get data, convert to XLS and send to appropriate FTP.
// So, if I have 2 clients in the XML file. This loop will call each client one by one and send files to corresponding FTP.
// Is it possible to assign each operation to separate thread so that multiple threads can start sending files to different FTP accounts simultaneously?***

                     }

                    System.Threading.Thread.Sleep(50000);
                }

            }

最佳实践和性能非常重要。此外,对当前方法的建设性批评持开放态度。

4

1 回答 1

2

一些注意事项和建议:

  • 使用计时器和事件订阅而不是线程睡眠。
  • 提取客户端的功能 - 获取文件列表、处理、发送 - 到单独的方法。Clients方法将从当前循环迭代中获取实例。
  • 一旦它是一个单独的方法,您就可以生成一个新线程(来自 ThreadPool),将此方法名称作为线程方法和任何相关参数传递。
  • 将 GetClientList() 设为静态,这样您就不必实例化该类的实例来调用它。你会打电话给Clients.GetClientList()

请注意,在 MSDN 站点上的示例中,他们使用 aThread.Sleep()来模拟线程同步。在您的情况下,这不是必需的,因为您正在运行服务 - 主线程不会结束,直到您告诉它这样做。


假设你实现了一个方法,例如SendFilesToClient(Clients currentClient),你会像这样将它排队:

ThreadPool.QueueUserWorkItem(new WaitCallback(SendFilesToClient), list);
于 2013-02-20T19:53:40.930 回答