Merci pour vos informations
Pour les MessageBox j'en ai pas vraiment besoin.
j'ai suivi les conseils de Pol63:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Configuration.Install;
namespace ServiceDocPdf
{
[RunInstaller(true)]
public class ServiceInstall : Installer
{
public ServiceInstall()
: base()
{
// On définit le compte sous lequel le service sera lancé (compte Système)
ServiceProcessInstaller process = new ServiceProcessInstaller();
process.Account = ServiceAccount.LocalSystem;
// On définit le mode de lancement (Manuel), le nom du service et sa description
ServiceInstaller service = new ServiceInstaller();
service.StartType = ServiceStartMode.Manual;
service.ServiceName = "Developpez";
service.DisplayName = "Developpez";
service.Description = "Service de test pour DVP";
// On ajoute les installeurs à la collection (l'ordre n'a pas d'importance)
Installers.Add(service);
Installers.Add(process);
}
private System.Windows.Forms.Timer timer1;
private IContainer components;
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
//
// timer1
//
this.timer1.Interval = 10;
}
}
public partial class Service : ServiceBase
{
private const int maxTime = 20;
private PDFCreator.clsPDFCreator _PDFCreator;
private PDFCreator.clsPDFCreatorError pErr;
private bool ReadyState;
public Service()
{
// Permet de définir le mode de lancement du service
ServiceInstaller si = new ServiceInstaller();
si.StartType = ServiceStartMode.Manual;
si.DisplayName = "Developpez";
si.Description = "Service de test pour DVP";
si.ServiceName = "Developpez";
InitializeComponent();
}
protected override void OnStart(string[] args)
{
System.Threading.Thread th = new System.Threading.Thread(traitement);
th.Start();
}
protected override void OnStop()
{
}
private void traitement()
{
while (true)
{
Thread.Sleep(10);
traitementFichier();
}
}
private void traitementFichier()
{
string sourcePath = ConfigurationManager.AppSettings["source"];
string targetPath = ConfigurationManager.AppSettings["target"];
pErr = new PDFCreator.clsPDFCreatorError();
_PDFCreator = new PDFCreator.clsPDFCreator();
_PDFCreator.eError += new PDFCreator.__clsPDFCreator_eErrorEventHandler(_PDFCreator_eError);
_PDFCreator.eReady += new PDFCreator.__clsPDFCreator_eReadyEventHandler(_PDFCreator_eReady);
// récupérer les fichiers du dossier source
string[] fileNames = Directory.GetFiles(sourcePath, "*.doc*");
//MessageBox.Show(Convert.ToString(fileNames.Count()));
for (int i = 0; i < (fileNames.Count()); i++)
{
string fname, DefaultPrinter;
FileInfo fi;
PDFCreator.clsPDFCreatorOptions opt;
fi = new FileInfo(fileNames[i]);
if (fi.Name.Length > 0)
{
if (fi.Name.IndexOf(".") > 1)
{
fname = fi.Name.Substring(0, fi.Name.IndexOf("."));
}
else
{
fname = fi.Name;
}
opt = _PDFCreator.cOptions;
opt.UseAutosave = 1;
opt.UseAutosaveDirectory = 1;
opt.AutosaveDirectory = targetPath;
opt.AutosaveFormat = 0;
opt.AutosaveFilename = fname;
_PDFCreator.cOptions = opt;
_PDFCreator.cClearCache();
DefaultPrinter = _PDFCreator.cDefaultPrinter;
_PDFCreator.cDefaultPrinter = "PDFCreator";
_PDFCreator.cPrintFile(fi.FullName);
ReadyState = false;
_PDFCreator.cPrinterStop = false;
timer1.Interval = maxTime * 1000;
timer1.Enabled = true;
while (!ReadyState && timer1.Enabled)
{
Application.DoEvents();
}
timer1.Enabled = false;
if (File.Exists(@"C:\temp\test.txt"))
{
StreamWriter sw = new StreamWriter(@"C:\temp\test.txt");
sw.WriteLine(targetPath);
sw.Close();
}
else
{
TextWriter file = File.CreateText(@"C:\temp\test.txt");
file.WriteLine(targetPath);
file.Close();
}
_PDFCreator.cPrinterStop = true;
_PDFCreator.cDefaultPrinter = DefaultPrinter;
}
}
_PDFCreator.cClose();
_PDFCreator = null;
pErr = null;
Process.Start("taskkill /f /im PDFCreator.exe");
this.Dispose();
Application.ExitThread();
Application.Exit();
Environment.Exit(0);
}
private void _PDFCreator_eReady()
{
_PDFCreator.cPrinterStop = true;
ReadyState = true;
}
private void _PDFCreator_eError()
{
pErr = _PDFCreator.cError;
}
private void timer1_Tick(object sender, System.EventArgs e)
{
timer1.Enabled = false;
}
}
} |
J'ai l'impression de parcourir tout le code mais pdfcreator ne se lance pas !!
Je pense que c'est la ligne là qui pose problème.
Qu'en pensez vous ??
Et que me conseillez vous ?
Bizarrement c'est quand je fais stop le service que cette ligne ci est exécutée
sw.WriteLine(targetPath);
Comment puis déboguer le service ?
Merci
Partager