Service Windows Optimisation
Bonjour,
Je viens de creer mon premier service qui fonctionne :)
Bon et comme c'est mon premier service je suppose qu'il est tout pourri :?
Aussi je le pose ici en esperant que deux trois âmes charitables a l'esprit eclaire serait disponible pour me dire ce qui est bon ou pas bon dans mon code. Comment l'ameliorer, optimiser etc... :P
Bon n'hesitez pas a critiquer, insulter, vous moquer .. tant que ca reste constructif :lol:
Deja il y a un truc bizarre. Normalment j'ai programme pour demande que mon service envoie un sms toutes les 2 sec. Dans les faits j'ai l'impression qu'il envoie 2 ou 3 sms toutes les secondes
Code:
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
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Net;
using System.IO;
using Npgsql;
using System.Timers;
namespace ServiceLudendo
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
System.Diagnostics.EventLog.WriteEntry("Arnaud ", "test Service démaré");
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
// Set the Interval to 2 seconds (2000 milliseconds).
aTimer.Interval = 2000;
aTimer.Enabled = true;
// Keep the timer alive until the end of Main.
GC.KeepAlive(aTimer);
}
// Specify what you want to happen when the Elapsed event is
// raised.
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
EnvoieSMS();
RecupDonnee();
}
/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
System.Diagnostics.EventLog.WriteEntry("Arnaud ", "test Service arrete");
}
/// <summary>
/// Envoie d'un SMS... Par la suite le numero de portable a joindre sera recupere dans la bdd.
/// </summary>
private void EnvoieSMS()
{
string url = "http://www.msinnovations.com/smsmod/amsmodule.sendsms.v2.php?clientcode=*******&passcode=******&XMLFlow=<DATA><CLIENT>********</CLIENT><MESSAGE>"
+ DateTime.Now.ToString()
+ " "
+ "DEPUIS UN SERVICE "
+ "</MESSAGE>"
+ "<CAMPAIGN_NAME>****</CAMPAIGN_NAME>"
+ "<MOBILEPHONE>*******</MOBILEPHONE></DATA>";
HttpWebResponse HttpWResponse;
HttpWebRequest HttpWReq;
StreamReader sr = null;
try
{
HttpWReq = (HttpWebRequest)WebRequest.Create(url);
HttpWReq.Method = "GET";
HttpWReq.KeepAlive = true;
HttpWReq.UserAgent = "CodaSystem";
HttpWResponse = (HttpWebResponse)HttpWReq.GetResponse();
sr = new StreamReader(HttpWResponse.GetResponseStream());
System.Diagnostics.EventLog.WriteEntry("Arnaud ", "SR Envoie reussi " + sr.ReadToEnd() + "");
}
catch
{
System.Diagnostics.EventLog.WriteEntry("Arnaud ", "Probleme dans l'envoie SMS");
}
finally
{
sr.Close();
HttpWReq = null;
HttpWResponse = null;
}
}
/// <summary>
/// Recuperation de donnee dans la BDD. Ici je recupere des donnees bidon c'est juste pour verifier que ca marche. Par la suite en fonction de ces donnees j'enverrais ou non le SMS.
/// </summary>
protected void RecupDonnee()
{
NpgsqlConnection connexion = new NpgsqlConnection("Server=******.com;Port=****;User Id=*****;Password=*****;Database=*****;ssl=true");
NpgsqlCommand command = null;
NpgsqlDataReader dr = null;
try
{
connexion.Open();
string requete = "SELECT * FROM equipes WHERE idequipe = 1 LIMIT 1";
command = new NpgsqlCommand(requete, connexion);
dr = command.ExecuteReader();
dr.Read();
string nom = "";
if (!(dr.IsDBNull(1))) { nom = (string)dr[1]; }
System.Diagnostics.EventLog.WriteEntry("Arnaud ", "On recupere ca dans la bdd " + nom);
}
catch (Exception ex)
{
System.Diagnostics.EventLog.WriteEntry("Arnaud ", "Probleme avec postgresql" + ex.ToString());
}
finally
{
command = null;
dr.Close();
dr = null;
connexion.Close();
connexion = null;
}
}
}
} |
D avance merci :king: