- using System;
- using System.Text;
- using System.Windows.Forms;
- using System.Threading;
-
- namespace ThreadWinForm
- {
- /// <summary>
- /// Modèle pour les transmitions de messages entre un Thread et une Form
- /// </summary>
- /// <param name="Value">Pourcentage d'avancement du Thread</param>
- /// <param name="Message">Message correspondant à l'avancement</param>
- public delegate void OnProgressHandler(int Value, string Message);
- /// <summary>
- /// Classe dont l'action principale fonctionne sur un Thread
- /// </summary>
- public class ThreadProgress
- {
- /// <summary>
- /// Constructeur sans Form
- /// </summary>
- public ThreadProgress() { }
- /// <summary>
- /// Constructeur avec Form
- /// </summary>
- /// <param name="FormStartThread">Form sur laquelle interagir</param>
- public ThreadProgress(Form FormStartThread)
- {
- _formStartThread = FormStartThread;
- }
-
- private Thread myThread;
- /// <summary>
- /// Evenement de progression du Thread
- /// </summary>
- public event OnProgressHandler OnProgress;
- private Form _formStartThread;
- /// <summary>
- /// Form sur laquelle interagir
- /// </summary>
- public Form FormStartThread
- {
- get { return _formStartThread; }
- set { _formStartThread = value; }
- }
-
- /// <summary>
- /// Procédure de lancement du Thread
- /// </summary>
- public void Start()
- {
- if (OnProgress != null && _formStartThread != null)
- myThread = new Thread(new ThreadStart(ActionThreadMessForm));
- else
- myThread = new Thread(new ThreadStart(ActionThreadNoMess));
- myThread.Start();
- }
-
- /// <summary>
- /// Lancement du Thread avec traitement du délégué
- /// </summary>
- private void ActionThreadMessForm()
- {
- for (int i = 1; i <= 100; i++)
- {
- Thread.Sleep(100);
- try
- {
- _formStartThread.Invoke(OnProgress, i, string.Format("Vous êtes à {0}%", i));
- }
- catch
- {
- myThread.Abort();
- }
- }
- }
-
- /// <summary>
- /// Lancement du Thread seul
- /// </summary>
- private void ActionThreadNoMess()
- {
- for (int i = 1; i <= 100; i++)
- {
- Thread.Sleep(100);
- }
- }
- }
- }
using System;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace ThreadWinForm
{
/// <summary>
/// Modèle pour les transmitions de messages entre un Thread et une Form
/// </summary>
/// <param name="Value">Pourcentage d'avancement du Thread</param>
/// <param name="Message">Message correspondant à l'avancement</param>
public delegate void OnProgressHandler(int Value, string Message);
/// <summary>
/// Classe dont l'action principale fonctionne sur un Thread
/// </summary>
public class ThreadProgress
{
/// <summary>
/// Constructeur sans Form
/// </summary>
public ThreadProgress() { }
/// <summary>
/// Constructeur avec Form
/// </summary>
/// <param name="FormStartThread">Form sur laquelle interagir</param>
public ThreadProgress(Form FormStartThread)
{
_formStartThread = FormStartThread;
}
private Thread myThread;
/// <summary>
/// Evenement de progression du Thread
/// </summary>
public event OnProgressHandler OnProgress;
private Form _formStartThread;
/// <summary>
/// Form sur laquelle interagir
/// </summary>
public Form FormStartThread
{
get { return _formStartThread; }
set { _formStartThread = value; }
}
/// <summary>
/// Procédure de lancement du Thread
/// </summary>
public void Start()
{
if (OnProgress != null && _formStartThread != null)
myThread = new Thread(new ThreadStart(ActionThreadMessForm));
else
myThread = new Thread(new ThreadStart(ActionThreadNoMess));
myThread.Start();
}
/// <summary>
/// Lancement du Thread avec traitement du délégué
/// </summary>
private void ActionThreadMessForm()
{
for (int i = 1; i <= 100; i++)
{
Thread.Sleep(100);
try
{
_formStartThread.Invoke(OnProgress, i, string.Format("Vous êtes à {0}%", i));
}
catch
{
myThread.Abort();
}
}
}
/// <summary>
/// Lancement du Thread seul
/// </summary>
private void ActionThreadNoMess()
{
for (int i = 1; i <= 100; i++)
{
Thread.Sleep(100);
}
}
}
}