Boucle infini - services

jumicoud Messages postés 1 Date d'inscription lundi 30 août 2010 Statut Membre Dernière intervention 20 avril 2011 - 20 avril 2011 à 13:48
nhervagault Messages postés 6063 Date d'inscription dimanche 13 avril 2003 Statut Membre Dernière intervention 15 juillet 2011 - 21 avril 2011 à 20:56
Bonjour,

Je réalise un service qui à pour but de faire communiquer 2 applications client serveur.
Avec l'application A je communique en TCP, avec la B via son SDK.
J'essaye de réaliser un service qui se connecte à A et B puis reste à l'écoute d'événement pour agir.

Tout fonctionne très bien lorsque j'ai développé avec un winform, tant que le winform est ouvert la com est ok.
Pour transposer tout cela en service il faut que je fasse une boucle infinie je penses pour eviter que mon service ne se termine.
Le problème c'est que la connexion a mon application ne se fait pas dans ce cas là. Je penses qu'il faut passer par des thread mais j'ai du mal avec cela.

ci-joint un bout du code.
Merci pour votre aide.

code qui fonctionne avec un form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using SDK;

namespace interfacegraphique
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
                InterfaceSDK MonSDK = new InterfaceSDK();
                MonSDK.connexion(); //Methode asynchrone de connection
        }
    }
}
       
:

Et transformé en service :

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.Threading;
using System.Reflection;
using SDK;
using System.Configuration;
using System.IO;



namespace SVC
{
    public partial class SVC : ServiceBase
    {
        private Timer serviceTimer;
        private string Apppath;
        private bool RunService;

        public SVC()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {   
            RunService = true;
                InterfaceSDK MonSDK = new InterfaceSDK();
                MonSDK.connexion(); //Methode asynchrone de connection
            
            TimerCallback timerDelegate = new TimerCallback(DoWork);
            serviceTimer = new Timer(timerDelegate, null, 10000, 10000); 
        }

        protected override void OnStop()
        {
            RunService = false;
        }
        private void DoWork(object state)
        {
            do
            { Thread.Sleep(1000); }
            while (1 == 1);
        }
    }
    
}


1 réponse

nhervagault Messages postés 6063 Date d'inscription dimanche 13 avril 2003 Statut Membre Dernière intervention 15 juillet 2011 37
21 avril 2011 à 20:56
Salut,

Essaye de trouver une bonne implementation du service.
Exemple
http://blogs.msdn.com/b/bclteam/archive/2005/03/15/396428.aspx

bon dev
0
Rejoignez-nous