public interface IProcess { void Start(); }
public class Dix : IProcess { public void Start() { // Ton traitement... } }
protected override void OnStart(string[] args) { // Pour toutes les dll de ton répertoire... foreach (string path in Directory.GetFiles(@"c:\Traitement", "*.dll")) { Assembly asm = Assembly.LoadFrom(path);// Chargement de la librairie... IProcess process = (IProcess) Activator.CreateInstance(asm.GetType(Path.GetFileName(path)));// Création du traitement process.Start();//Lancement du process... à la place ceci, je te laisse faire tout seul le lancement de la méthode Start() dans un thread... } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.IO; namespace Service_Thread_comptage // même espace de noms pour les DLLs { public class Dixendix : IProcess { public static void Thread10() { int Nombre = 0; // Tant que le thread n'est pas tué, on travaille while (Thread.CurrentThread.IsAlive) { // Attente de 1s Thread.Sleep(1000); Nombre = Nombre + 10; // Ecriture du Nombre dans le fichier "dixendix" StreamWriter sw = new StreamWriter(@"C:/dixendix.log", true); sw.WriteLine(Nombre); sw.Close(); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; public interface IProcess { void Start(); }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; // pour utiliser un service windows using System.Text; // pour écrire dans un fichier texte using Service_Thread_comptage; // pour utiliser les 2 DLLs : dixendix et vingtenvingt using System.Threading; // pour utiliser les Threads using System.IO; // pour écrire dans un fichier log using System.Reflection; using System.Resources; namespace Service_Thread_comptage { public partial class Service1 : ServiceBase { public Service1() { InitializeComponent(); } Thread dll; // création du thread sur lequel sera implémenté les dlls protected override void OnStart(string[] args) { // Ecriture dans "dixendix" de la date et l'heure à laquelle // le 10 en 10 commence StreamWriter sw1 = new StreamWriter(@"C:/dixendix.log", true); sw1.WriteLine("Démarrage du 10 en 10 : " + DateTime.Now.ToLongTimeString()); sw1.Close(); // Ecriture dans "vingtenvingt" de la date et l'heure à laquelle // le 20 en 20 commence StreamWriter sw2 = new StreamWriter(@"C:/vingtenvingt.log", true); sw2.WriteLine("Démarrage du 20 en 20: " + DateTime.Now.ToLongTimeString()); sw2.Close(); int i = 0; // Pour toutes les DLLs de ton répertoire... foreach (string path in Directory.GetFiles(@"C:\Documents and Settings\Administrateur\Mes documents\Visual Studio 2008\Projects\Service_Thread_comptage\Service_Thread_comptage\bien\Debug", "Dixendix.dll")) { Assembly asm = Assembly.LoadFrom(path);// Chargement de la librairie IProcess process = (IProcess)Activator.CreateInstance(asm.GetType(Path.GetFileName(path)));// Création du traitement dll = new Thread(new ThreadStart(process.Start)); // on implémente la méthode Start de l'interface dans un thread. dll.Start(); // Lancement du thread }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionList<Thread> allThreads = new List<Thread>(); Thread thread = new Thread(new ThreadStart(process.Start)); thread.Start(); allThreads.Add(thread);
foreach(Thread thread in allThreads) { thread.Abort(); }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; // pour utiliser un service windows using System.Text; // pour écrire dans un fichier texte using Service_Thread_comptage; // pour utiliser les 2 DLLs : dixendix et vingtenvingt using System.Threading; // pour utiliser les Threads using System.IO; // pour écrire dans un fichier log using System.Reflection; using System.Resources; namespace Service_Thread_comptage { public partial class Service1 : ServiceBase { public Service1() { InitializeComponent(); } // Création d'une liste de Thread List<Thread> allThreads = new List<Thread>(); protected override void OnStart(string[] args) { // Ecriture dans "dixendix" de la date et l'heure à laquelle // le 10 en 10 commence StreamWriter sw1 = new StreamWriter(@"C:/dixendix.log", true); sw1.WriteLine("Démarrage du 10 en 10 : " + DateTime.Now.ToLongTimeString()); sw1.Close(); // Ecriture dans "vingtenvingt" de la date et l'heure à laquelle // le 20 en 20 commence StreamWriter sw2 = new StreamWriter(@"C:/vingtenvingt.log", true); sw2.WriteLine("Démarrage du 20 en 20: " + DateTime.Now.ToLongTimeString()); sw2.Close(); // Pour toutes les DLLs du répertoire: ("C:\Traitement) foreach (string path in Directory.GetFiles(@"C:\Traitement", "*.dll")) { Assembly asm = Assembly.LoadFrom(path);// Chargement de la librairie IProcess process = (IProcess)Activator.CreateInstance(asm.GetType(Path.GetFileName(path)));// Création du traitement // Création d'un thread à partir de la fonction récupérée dans l'interface Thread thread = new Thread(new ThreadStart(process.Start)); // Démarrage du thread thread.Start(); // On ajoute le thread qui vient d'être créé dans la liste de // thread créé précédemment allThreads.Add(thread); } } protected override void OnStop() { // Ecriture dans "dixendix" de la date et l'heure à laquelle // le 10 en 10 s'arrête StreamWriter sw1 = new StreamWriter(@"C:/dixendix.log", true); sw1.WriteLine("Arrêt du 10 en 10 : " + DateTime.Now.ToLongTimeString()); sw1.Close(); // Ecriture dans "vingtenvingt" de la date et l'heure à laquelle // le 20 en 20 s'arrête StreamWriter sw2 = new StreamWriter(@"C:/vingtenvingt.log", true); sw2.WriteLine("Arrêtdu 20 en 20: " + DateTime.Now.ToLongTimeString()); sw2.Close(); // Pour chaque thread de la liste foreach (Thread thread in allThreads) { thread.Abort(); // On arrête le thread } } protected override void OnShutdown() { // Ecriture dans "dixendix" de la date et l'heure à laquelle // le 10 en 10 s'arrête StreamWriter sw1 = new StreamWriter(@"C:/dixendix.log", true); sw1.WriteLine("Arrêt du système à : " + DateTime.Now.ToLongTimeString()); sw1.Close(); // Ecriture dans "vingtenvingt" de la date et l'heure à laquelle // le 20 en 20 s'arrête StreamWriter sw2 = new StreamWriter(@"C:/vingtenvingt.log", true); sw2.WriteLine("Arrêt du système à : " + DateTime.Now.ToLongTimeString()); sw2.Close(); // Pour chaque thread de la liste foreach (Thread thread in allThreads) { thread.Abort(); // On arrête le thread } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.IO; namespace Service_Thread_comptage // même espace de noms pour les DLLs { public class Dixendix : IProcess { public static void Thread10() { int Nombre = 0; // Tant que le thread n'est pas tué, on travaille while (Thread.CurrentThread.IsAlive) { // Attente de 1s Thread.Sleep(1000); Nombre = Nombre + 10; // Ecriture du Nombre dans le fichier "dixendix" StreamWriter sw = new StreamWriter(@"C:/dixendix.log", true); sw.WriteLine(Nombre); sw.Close(); } } #region IProcess Membres public void Start() { throw new NotImplementedException(); } #endregion } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.IO; namespace Service_Thread_comptage // même espace de noms pour les DLLs { public class Vingtenvingt : IProcess { public static void Thread20() { int Nombre = 0; // Tant que le thread n'est pas tué, on travaille while (Thread.CurrentThread.IsAlive) { // Attente de 2s Thread.Sleep(2000); Nombre = Nombre + 20; // Ecriture du Nombre dans le fichier "dixendix" StreamWriter sw = new StreamWriter(@"C:/vingtenvingt.log", true); sw.WriteLine(Nombre); sw.Close(); } } #region IProcess Membres public void Start() { throw new NotImplementedException(); } #endregion } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; public interface IProcess { void Start(); }
StreamWriter sw1 = new StreamWriter(@"C:/dixendix.log", true); sw1.WriteLine("Arrêt du 10 en 10 : " + DateTime.Now.ToLongTimeString()); sw1.Close();
while (Thread.CurrentThread.IsAlive)
IProcess process = (IProcess)Activator.CreateInstance(asm.GetType(Path.GetFileName(path)));
protected override void OnStart(string[] args) { StreamWriter sw = new StreamWriter(@"C:/dixendix.log", true); sw.WriteLine("Actif1"); sw.Close(); // Pour toutes les DLLs du répertoire: ("C:\Traitement) foreach (string path in Directory.GetFiles(@"C:\Traitement", "*.dll")) { sw = new StreamWriter(@"C:/dixendix.log", true); sw.WriteLine("Actif2"); sw.Close(); Assembly asm = Assembly.LoadFrom(path);// Chargement de la librairie sw = new StreamWriter(@"C:/dixendix.log", true); sw.WriteLine("Actif3"); sw.Close(); IProcess process = (IProcess)Activator.CreateInstance(asm.GetType(Path.GetFileName(path)));// Création du traitement sw = new StreamWriter(@"C:/dixendix.log", true); sw.WriteLine("Actif4"); sw.Close(); // Création d'un thread à partir de la fonction récupérée dans l'interface Thread thread = new Thread(new ThreadStart(process.Start)); sw = new StreamWriter(@"C:/dixendix.log", true); sw.WriteLine("Actif5"); sw.Close(); // Démarrage du thread thread.Start(); sw = new StreamWriter(@"C:/dixendix.log", true); sw.WriteLine("Actif6"); sw.Close(); // On ajoute le thread qui vient d'être créé dans la liste de thread créé // précédemment pour éviter d'écraser un nouveau thread lors du prochain // passage dans la boucle allThreads.Add(thread); sw = new StreamWriter(@"C:/dixendix.log", true); sw.WriteLine("Actif7"); sw.Close(); } }
try { //... } catch(Exception ex) { //Log de l'exception }
Actif1 Actif2 Actif3 System.ArgumentNullException: La valeur ne peut pas être null. Nom du paramètre : type à System.Activator.CreateInstance(Type type, Boolean nonPublic) à System.Activator.CreateInstance(Type type) à Service_Thread_comptage.Service1.OnStart(String[] args) dans C:\Documents and Settings\Administrateur\Mes documents\Visual Studio 2008\Projects\Service_Thread_comptage\Multithread\Service1.cs:ligne 53
Type type = asm.GetType(Path.GetFileName(path));//est null
string className = Path.GetFileName(path);// n'existe pas dans ta librairie (celle de Dix ou de Vingt)
string className = Path.GetFileNameWithoutExtension(path); Type type = asm.GetType(className);//vérifie bien que la valeur de className correspond bien à une classe de ta dll
Actif1 Actif2 Actif31 Dixendix Actif4 System.ArgumentNullException: La valeur ne peut pas être null. Nom du paramètre : type à System.Activator.CreateInstance(Type type, Boolean nonPublic) à System.Activator.CreateInstance(Type type) à Service_Thread_comptage.Service1.OnStart(String[] args) dans C:\Documents and Settings\Administrateur\Mes documents\Visual Studio 2008\Projects\Service_Thread_comptage\Multithread\Service1.cs:ligne 70 Actif1 Actif2 Actif31 Dixenx Actif4 System.ArgumentNullException: La valeur ne peut pas être null. Nom du paramètre : type à System.Activator.CreateInstance(Type type, Boolean nonPublic) à System.Activator.CreateInstance(Type type) à Service_Thread_comptage.Service1.OnStart(String[] args) dans C:\Documents and Settings\Administrateur\Mes documents\Visual Studio 2008\Projects\Service_Thread_comptage\Multithread\Service1.cs:ligne 70
protected override void OnStart(string[] args) { try { StreamWriter sw = new StreamWriter(@"C:/dixendix.log", true); sw.WriteLine("Actif1"); sw.Close(); // Pour toutes les DLLs du répertoire: ("C:\Traitement) foreach (string path in Directory.GetFiles(@"C:\Traitement", "*.dll")) { sw = new StreamWriter(@"C:/dixendix.log", true); sw.WriteLine("Actif2"); sw.Close(); Assembly asm = Assembly.LoadFrom(path);// Chargement de la librairie sw = new StreamWriter(@"C:/dixendix.log", true); sw.WriteLine("Actif31"); sw.Close(); // On récupère le nom de la classe string className = Path.GetFileNameWithoutExtension(path); sw = new StreamWriter(@"C:/dixendix.log", true); sw.WriteLine(className); sw.Close(); Type type = asm.GetType(className); sw = new StreamWriter(@"C:/dixendix.log", true); sw.WriteLine("Actif4"); sw.Close(); // Création du traitement IProcess process = (IProcess)Activator.CreateInstance(type); sw = new StreamWriter(@"C:/dixendix.log", true); sw.WriteLine("Actif5"); sw.Close();
public class Dixendix : IProcess
Path : C:\Traitement\DLL DIX.dll ASM : DLL DIX, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null className : DLL DIX type : Exception non gérée : System.ArgumentNullException: La valeur ne peut pas être n ull. Nom du paramètre : type à System.Activator.CreateInstance(Type type, Boolean nonPublic) à System.Activator.CreateInstance(Type type) à TEST.Program.Main(String[] args) dans C:\Documents and Settings\Administrat eur\Mes documents\Visual Studio 2008\Projects\PROJETS SERVICE\TEST\TEST\Program. cs:ligne 45 Appuyez sur une touche pour continuer...
static void Main(string[] args) { // Création d'une liste de Thread List<Thread> allThreads = new List<Thread>(); // Pour toutes les DLLs du répertoire: ("C:\Traitement) foreach (string path in Directory.GetFiles(@"C:\Traitement", "*.dll")) { Console.WriteLine("Path : "+path); Assembly asm = Assembly.LoadFrom(path);// Chargement de la librairie Console.WriteLine("ASM : "+asm); // On récupère le nom de la classe string className = Path.GetFileNameWithoutExtension(path); Console.WriteLine("className : "+className); Type type = asm.GetType(className); Console.WriteLine("type :"+type); // Création du traitement IProcess process = (IProcess)Activator.CreateInstance(type); Console.WriteLine("Actif4"); // Création d'un thread à partir de la fonction récupérée dans l'interface Thread thread = new Thread(new ThreadStart(process.Start)); // Démarrage du thread thread.Start(); // On ajoute le thread qui vient d'être créé dans la liste de thread créé // précédemment pour éviter d'écraser un nouveau thread lors du prochain // passage dans la boucle allThreads.Add(thread); } }