public static int ProcessFile(string name, string fullPath) { if (added2List(name, fullPath)) { var item = fileList[name]; //Console.WriteLine(String.Format("{0} added to list of {1}", name, TaskMgr.fileList.Count)); PrintJobThread printJob = TroyPortMonService.setupPrintJob(fullPath); item.status = statusType.Processing; item.start = DateTime.Now; lock (_lockObject) { fileList[name] = item; } int result = doTask(printJob); lock (_lockObject) { fileList.Remove(name); } //Console.WriteLine(String.Format("{0} removed from list of {1}", name, TaskMgr.fileList.Count)); return(result); } else { return(1); } }
public static int doTask(PrintJobThread printJob) { // Create Task, defer starting it, continue with another task Task <int> t = Task <int> .Factory.StartNew(() => { return(printJob.PrintJobReceived()); }); Task cwt = t.ContinueWith(task => { result = task.Result; Console.WriteLine("Task result " + task.Result); }, TaskContinuationOptions.OnlyOnFaulted); return(result); }
private static void fileWatcherService_Changed(object source, FileSystemEventArgs e) { try { PrintJobThread printJob = new PrintJobThread(); printJob.printFileName = e.FullPath; FileInfo getPath = new FileInfo(e.FullPath); string filePath = getPath.DirectoryName.ToString(); if ((filePath.Length > 0) && (!filePath.EndsWith("\\"))) { filePath += "\\"; } TroyPortMonitorConfiguration tpmc = portConfigs[filePath]; if (tpmc != null) { printJob.pmConfig = tpmc; } else { Exception ex1 = new Exception("Error: Could not find Configuration for file path " + filePath); throw ex1; } portMonLogging = portLogs[filePath]; if (portLogs != null) { printJob.pmLogging = portMonLogging; } DataCaptureList customConfiguration = portDataCap[filePath]; if (customConfiguration != null) { printJob.dCapConfig = customConfiguration; } TroyPrinterMap tpm = portPrintMap[filePath]; if (tpm != null) { printJob.printerMap = tpm; } TroyFontConfiguration tfc = portFonts[filePath]; if (tfc != null) { printJob.fontConfigs = tfc; } DataCaptureFlags dcf = dataCapFlags[filePath]; if (dcf != null) { printJob.dataCapFlags = dcf; } PantographConfiguration pgc = portPantoConfig[filePath]; if (pgc != null) { printJob.pantoConfig = pgc; } printJob.insertPjl = InsertPjl; //printJob.LicenseStatus = ls; printJob.SoftwarePantograph = UsingSoftwarePantograph; printJob.SoftwareTroyMark = UsingSoftwareTroymark; Thread printJobThread = new Thread(new ThreadStart(printJob.PrintJobReceived)); printJobThread.Name = e.Name + " thread"; printJobThread.IsBackground = true; printJobThread.Priority = ThreadPriority.Normal; printJobThread.Start(); } catch (Exception ex) { EventLog.WriteEntry("TROY SecurePort Monitor", "Error in fileWatcherService_Changed. Error: " + ex.Message, EventLogEntryType.Error); } }
public static PrintJobThread setupPrintJob(string fullname) { string fullnamePath = extractPath(fullname); PrintJobThread printJob = new PrintJobThread(); printJob.printFileName = fullname; FileInfo fi = new FileInfo(fullname); string dirName = fi.Directory.Name; TroyPortMonitorConfiguration tpmc = portConfigs[dirName]; if (tpmc != null) { printJob.pmConfig = tpmc; } else { Exception ex1 = new Exception("Error: Could not find Configuration for directory " + dirName); throw ex1; } var pml = portLogs[dirName]; if (pml != null) { printJob.pmLogging = pml; } DataCaptureList dcl = portDataCap[dirName]; if (dcl != null) { printJob.dCapConfig = dcl; } TroyPrinterMap pm = portPrintMap[dirName]; if (pm != null) { printJob.printerMap = pm; } TroyFontConfiguration tfc = portFonts[dirName]; if (tfc != null) { printJob.fontConfigs = tfc; } DataCaptureFlags dcf = portDataCapFlags[dirName]; if (dcf != null) { printJob.dataCapFlags = dcf; } PantographConfiguration pgc = portPantoConfig[dirName]; if (pgc != null) { printJob.pantoConfig = pgc; } printJob.insertPjl = InsertPjl; printJob.SoftwarePantograph = UsingSoftwarePantograph; printJob.SoftwareTroyMark = UsingSoftwareTroymark; return(printJob); }
private void SetupFileWatchers() { try { printToFilePath = ""; string configPath; FileSystemWatcher fileWatcher; TroyPortMonitorConfiguration tpmc = null; DataCaptureList dcl = null; foreach (KeyValuePair <string, string> kvp in portsForFileWatcher) { printToFilePath = GetPathFromRegistry(kvp.Key); if (kvp.Value == "") { configPath = ""; } //Default is the print to file path + the config folder else if (kvp.Value.ToUpper() == "DEFAULT") { configPath = printToFilePath + "Config\\"; } else { configPath = kvp.Value.ToString(); } string Extension = ""; if (configPath != "") { if (!configPath.EndsWith("\\")) { configPath += "\\"; } DirectoryInfo dirInfo = new DirectoryInfo(configPath); if (!dirInfo.Exists) { } else { //Read port monitor configuraton file tpmc = new TroyPortMonitorConfiguration(); ReadPortMonitorConfiguration(kvp.Key, configPath, printToFilePath, out Extension, out tpmc); string fileName = "TroyDataCaptureConfiguration.xml"; if (!File.Exists(configPath + fileName)) { EventLog.WriteEntry("TROY SecurePort Monitor", "Error: Unable to open the Data Capture XML file. File name = " + configPath + fileName, EventLogEntryType.Error); } else { XmlSerializer dser = new XmlSerializer(typeof(DataCaptureList)); FileStream fs = new FileStream(configPath + "TroyDataCaptureConfiguration.xml", FileMode.Open, FileAccess.Read, FileShare.Read); dcl = (DataCaptureList)dser.Deserialize(fs); fs.Close(); portDataCap.Add(printToFilePath, dcl); SetupFontConfig(dcl, printToFilePath, tpmc); } fileName = "TroyPrinterMap.xml"; if (!File.Exists(configPath + fileName)) { EventLog.WriteEntry("TROY SecurePort Monitor", "Error: Unable to open the Printer Map XML file. File name = " + configPath + fileName, EventLogEntryType.Error); } else { XmlSerializer dser = new XmlSerializer(typeof(TroyPrinterMap)); FileStream fs = new FileStream(configPath + fileName, FileMode.Open, FileAccess.Read, FileShare.Read); TroyPrinterMap tpm = (TroyPrinterMap)dser.Deserialize(fs); fs.Close(); portPrintMap.Add(printToFilePath, tpm); } fileName = "TroyPantographConfiguration.xml"; if (File.Exists(configPath + fileName)) { XmlSerializer dser = new XmlSerializer(typeof(PantographConfiguration)); FileStream fs = new FileStream(configPath + fileName, FileMode.Open, FileAccess.Read, FileShare.Read); PantographConfiguration temp = (PantographConfiguration)dser.Deserialize(fs); fs.Close(); portPantoConfig.Add(printToFilePath, temp); } } } DirectoryInfo dirCheck = new DirectoryInfo(printToFilePath); if (!dirCheck.Exists) { dirCheck.Create(); } else { if (!ReprintFiles) { foreach (FileInfo file in dirCheck.GetFiles("*." + Extension)) { file.Delete(); } foreach (FileInfo file2 in dirCheck.GetFiles("*.bak")) { file2.Delete(); } foreach (FileInfo file2 in dirCheck.GetFiles("*.enc")) { file2.Delete(); } } } fileWatcher = new FileSystemWatcher(); fileWatcher.InternalBufferSize = 12288; fileWatcher.Path = printToFilePath; fileWatcher.NotifyFilter = NotifyFilters.FileName; fileWatcher.Filter = "*." + Extension; fileWatcher.IncludeSubdirectories = false; //EVENTS HANDLERS (Note: Only one event handler is needed for both events) fileWatcher.Changed += new FileSystemEventHandler(fileWatcherService_Changed); fileWatcher.Created += new FileSystemEventHandler(fileWatcherService_Changed); //ENABLE fileWatcher.EnableRaisingEvents = true; fileWatchers.Add(fileWatcher); if (ReprintFiles) { if (tpmc != null) { foreach (FileInfo file in dirCheck.GetFiles("*." + tpmc.FileExtension)) { PrintJobThread printJob = new PrintJobThread(); printJob.printFileName = file.FullName; printJob.pmConfig = tpmc; printJob.pmLogging = portMonLogging; TroyPrinterMap tpm = portPrintMap[printToFilePath]; if (tpm != null) { printJob.printerMap = tpm; } TroyFontConfiguration tfc = portFonts[printToFilePath]; if (tfc != null) { printJob.fontConfigs = tfc; } //TroyFontConfiguration tfc2 = portFonts2[printToFilePath]; //if (tfc2 != null) //{ // printJob.fontConfigs2 = tfc2; //} if (dcl != null) { printJob.dCapConfig = dcl; } DataCaptureFlags dcf = dataCapFlags[printToFilePath]; if (dcf != null) { printJob.dataCapFlags = dcf; } //printJob.LicenseStatus = ls; printJob.SoftwarePantograph = UsingSoftwarePantograph; printJob.SoftwareTroyMark = UsingSoftwareTroymark; Thread printJobThread = new Thread(new ThreadStart(printJob.PrintJobReceived)); printJobThread.Name = file.Name + " thread"; printJobThread.IsBackground = true; printJobThread.Priority = ThreadPriority.Normal; printJobThread.Start(); Thread.Sleep(200); } } } } } catch (Exception ex) { EventLog.WriteEntry("TROY SecurePort Monitor", "Error in SetupFileWatchers. Error: " + ex.Message, EventLogEntryType.Error); throw ex; } finally { #if (DEBUG) Console.WriteLine("waiting..."); Console.Read(); #endif } }