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);
}
}
最佳实践和性能非常重要。此外,对当前方法的建设性批评持开放态度。