Bonjour,

J'ai une application qui va lire les fichiers .docs dans un répertoire pour ensuite les transformer dans un autre répertoire en PDF à l'aide de PDFCrreator avec un composant COM .

ça fonctionne bien en WinForm.

J'ai essayé d’adapter mon code WinForm en Windows service.

Mais j'obtiens une erreur:

Le service sur ordinateur local a démarré et s'est ensuite arrêté. Certains services s'arrêtent automatiquement s'ils ne sont utilisés par d'autres services ou programmes.

Voici mon code:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Configuration;
 
namespace WindowsServiceDocToPdf
{
    public partial class Service1 : ServiceBase
    {
        private const int maxTime = 20;
 
        private PDFCreator.clsPDFCreator _PDFCreator;
        private PDFCreator.clsPDFCreatorError pErr;
 
        private bool ReadyState;
 
        public Service1()
        {
            InitializeComponent();
        }
 
        protected override void OnStart(string[] args)
        {
 
            string parameters;
            MessageBox.Show ("Status: Program is started.");
 
            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);
 
            parameters = "/NoProcessingAtStartup";
 
            if (!_PDFCreator.cStart(parameters, false))
            {
                MessageBox.Show("Status: Error[" + pErr.Number + "]: " + pErr.Description);
            }
 
            // 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;
 
                    _PDFCreator.cPrinterStop = true;
                    _PDFCreator.cDefaultPrinter = DefaultPrinter;
                }
            }
 
        }
        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;
        }
 
        protected override void OnStop()
        {
            _PDFCreator.cClose();
            while (_PDFCreator.cProgramIsRunning)
            {
                System.Threading.Thread.Sleep(100);
            }
            _PDFCreator = null;
            pErr = null;
 
            System.Diagnostics.Process.Start("taskkill", "/F /IM [taskname].exe");
        }
    }
}
Je tiens à préciser que c'est mon premier service

D'avance merci