📄 pollingservice.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;
using System.Xml;
using System.Xml.Serialization;
using ShoppingAssistantLib;
using WinInformationPollingService.CategoriesProxy;
using WinInformationPollingService.ProductsProxy;
namespace WinInformationPollingService
{
partial class PollingService : ServiceBase
{
private CategoriesService _categoriesService;
private ProductsService _productsService;
Timer stateTimer;
public PollingService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
AutoResetEvent autoEvent = new AutoResetEvent(false);
// Create the delegate that invokes methods for the timer
TimerCallback timerDelegate = new TimerCallback(GetData);
// Create a timer that signals the delegate to invoke GetData method
//immediately, and every 10 seconds thereafter
stateTimer = new Timer(timerDelegate, autoEvent, 0, 10000);
//Configure the FileSystemWatcher to watch for changes
fileSystemWatcher1 = new FileSystemWatcher();
fileSystemWatcher1.Path = @"C:\Projects\Wrox\Drop\";
//Set the Filter to watch for .xml files
fileSystemWatcher1.Filter = "*.xml";
//Filter for Last Write changes
fileSystemWatcher1.NotifyFilter = NotifyFilters.LastWrite;
fileSystemWatcher1.IncludeSubdirectories = false;
//Add event handlers
fileSystemWatcher1.Changed += new FileSystemEventHandler(OnChanged);
//Enable the component to begin watching for changes
fileSystemWatcher1.EnableRaisingEvents = true;
}
private void OnChanged(object source, FileSystemEventArgs e)
{
//Read the contents of the file and save it to the database
SaveReportInfo(e.FullPath);
}
private void SaveReportInfo(string path)
{
try
{
XmlReader reader = XmlReader.Create(path);
XmlDocument document = new XmlDocument();
document.Load(reader);
XmlNode reportInfoNode = document.DocumentElement;
ReportInfo report = new ReportInfo();
//Get all the values from the XML document into the ReportInfo object
report.Browser = reportInfoNode.ChildNodes.Item(0).InnerText;
report.RequestType = reportInfoNode.ChildNodes.Item(1).InnerText;
report.Authenticated = reportInfoNode.ChildNodes.Item(2).InnerText;
report.CategoryID = Convert.ToInt32(reportInfoNode.ChildNodes.Item(3).InnerText);
report.ProductID = Convert.ToInt32(reportInfoNode.ChildNodes.Item(4).InnerText);
ReportDB reportDB = new ReportDB();
reportDB.InsertReportInfo(report);
reader.Close();
}
catch (Exception ex)
{
throw ex;
}
}
private void GetData(Object stateInfo)
{
//System.Diagnostics.EventLog.WriteEntry("Application", "Entry time is : " + DateTime.Now.ToString());
_categoriesService = new CategoriesService();
_categoriesService.GetCategoriesCompleted += new
GetCategoriesCompletedEventHandler(this.GetCategoriesCompleted);
_categoriesService.GetCategoriesAsync();
_productsService = new ProductsService();
_productsService.GetProductsCompleted += new
GetProductsCompletedEventHandler(this.GetProductsCompleted);
_productsService.GetProductsAsync();
}
void GetCategoriesCompleted(object sender, GetCategoriesCompletedEventArgs args)
{
string xmlFilePath = @"C:\Projects\Wrox\Categories.xml";
Category[] categoryArray = args.Result;
XmlSerializer serializer = new XmlSerializer(typeof(Category[]));
TextWriter writer = new StreamWriter(xmlFilePath);
//Serialize the Category array and close the TextWriter
serializer.Serialize(writer, categoryArray);
writer.Close();
}
void GetProductsCompleted(object sender, GetProductsCompletedEventArgs args)
{
string xmlFilePath = @"C:\Projects\Wrox\Products.xml";
Product[] productArray = args.Result;
XmlSerializer serializer = new XmlSerializer(typeof(Product[]));
TextWriter writer = new StreamWriter(xmlFilePath);
//Serialize the Product array and close the TextWriter
serializer.Serialize(writer, productArray);
writer.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -