我使用c#创建Windows服务但是在安装之后它不起作用 它在日志中没有显示任何内容,所以我不理解解决它的错误 。
这是我的开始活动
protected override void OnStart(string[] args)
{
StartSynchro();
}
public void StartSynchro()
{
while (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
{
System.Threading.Thread.Sleep(TimeSpan.FromHours(1));
}
GoogleDriveHandler driveHandler = new GoogleDriveHandler();
List<Google.Apis.Drive.v3.Data.File> onlineFiles = driveHandler.getFiles();
Google.Apis.Drive.v3.Data.File onlineVerCSVFile = onlineFiles.Where(c => c.Name == GoogleDriveHandler.onlineVerCSV).First();
onlineFiles.Remove(onlineVerCSVFile);
List<KeyValuePair<string, string>> result = Helper.ReadFromMemoryStream(driveHandler.downloadfile_GetStream(ref onlineVerCSVFile));
List<string> corspondingVer = new List<string>();
foreach (var onlineFile in onlineFiles)
{
corspondingVer.Add(result.Find(c => c.Key == onlineFile.Name).Value);
}
List<Google.Apis.Drive.v3.Data.File> fileToDownload = LocalClasses.LocalFilesInfo.getFilesToDwonload(onlineFiles, corspondingVer, ref driveHandler);
driveHandler.StartDownloading(fileToDownload);
return;
}
答案 0 :(得分:1)
最好尽快启动Windows服务。您可以将初始化代码移动到单独的线程,如下所示:
protected override void OnStart(string[] args)
{
Task.Run(() => StartSynchro());
}