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>

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

            }

ベスト プラクティスとパフォーマンスは非常に重要です。また、現在のアプローチで建設的な批判を受け入れます。

4

1 に答える 1