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
|
/// <summary>
/// classe de gestion des méthodes longues
/// </summary>
/// <remarks>utilise un thread parallèle et une fenêtre à ProgressBar</remarks>
public class ThreadManager
{
/// <summary>
/// instance unique (Singleton)
/// </summary>
private static ThreadManager _instance = null;
/// <summary>
/// fenêtre de ProgressBar
/// </summary>
private wdwWait frmWait;
/// <summary>
/// booléen déterminant si l'opération s'est déroulée entièrement
/// </summary>
private bool OperationIsSuccess;
/// <summary>
/// exécutant de fond
/// </summary>
private BackgroundWorker bgw;
/// <summary>
/// constructeur privé
/// </summary>
private ThreadManager()
{
OperationIsSuccess = false;
}
/// <summary>
/// acesseur sur l'instance
/// </summary>
public static ThreadManager Instance
{
get
{
if (_instance == null)
_instance = new ThreadManager();
return _instance;
}
}
/// <summary>
/// démarre le thread parallèle
/// </summary>
/// <param name="_callBack">méthode lancé par le thread</param>
/// <param name="_actionName">intitulé de l'action</param>
/// <param name="_abortMessage">message en cas d'abandon</param>
/// <param name="_allowQuit">détermine si l'action peut être interrompue</param>
/// <returns>succès ou échec de l'opération</returns>
public bool Launch(DoWorkEventHandler _callBack, string _actionName, string _abortMessage, bool _allowQuit)
{
bgw = new BackgroundWorker();
bgw.WorkerReportsProgress = true;
bgw.WorkerSupportsCancellation = true;
bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
bgw.DoWork += new DoWorkEventHandler(_callBack);
bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
bgw.RunWorkerAsync();
frmWait = new wdwWait(_actionName, _abortMessage, _allowQuit);
frmWait.Closed += new EventHandler(frmWait_Closed);
frmWait.ShowDialog();
return OperationIsSuccess;
}
/// <summary>
/// se produit quand la pgroession est modifiée
/// </summary>
/// <param name="sender">objet appelant</param>
/// <param name="evt">données de l'évènement</param>
private void bgw_ProgressChanged(object sender, ProgressChangedEventArgs evt)
{
this.frmWait.Dispatcher.Invoke(new ProgressionHandler(frmWait.SetProgressBarChanged), evt.ProgressPercentage);
}
/// <summary>
/// se produit quand la fenêtre d'attente est fermée
/// </summary>
/// <param name="sender">objet appelant</param>
/// <param name="e">évenement levé</param>
private void frmWait_Closed(object sender, EventArgs e)
{
bgw.CancelAsync();
}
/// <summary>
/// se produit quand l'exécutant a terminé son travail
/// </summary>
/// <param name="sender">objet appelant</param>
/// <param name="evt">données de l'évènement</param>
private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs evt)
{
frmWait.Hide();
OperationIsSuccess = !evt.Cancelled;
}
} |
Partager