通过windows服务使用多个线程将多个文件发送到多个FTP帐户的理想方式

本文关键字:FTP 方式 理想 文件 服务 windows 线程 通过 | 更新日期: 2023-09-27 18:00:22

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

Clients.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>

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

ServiceImplementation.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);
                }
            }

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

通过windows服务使用多个线程将多个文件发送到多个FTP帐户的理想方式

一些注意事项和建议:

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

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


假设您实现了SendFilesToClient(Clients currentClient)这样的方法,那么您将对其进行如下排队:

ThreadPool.QueueUserWorkItem(new WaitCallback(SendFilesToClient), list);