* Conduite.cs: Amelioration de la boucle (precision du timing)

Gestion simple des erreurs dans la boucle

* Main.cs: Gestion des erreurs de l'interface

* Info.cs: les infos persistent 5s au lieu de 3
This commit is contained in:
tzim 2013-11-21 10:59:26 +00:00
parent aa3d405b6c
commit afc21dccf0
3 changed files with 48 additions and 11 deletions

View file

@ -177,19 +177,36 @@ namespace DMX2
} }
} }
// On utilise un thread qui boucle au lieu d'un timer. // On utilise un thread qui boucle au lieu d'un timer.
// C'est moins précis, mais ca consomme beaucoup moins de ressources // C'est un peu moins précis, mais ca consomme beaucoup moins de ressources
const int LOOPTIME = 10;
void ThreadLoop () void ThreadLoop ()
{ {
Thread.CurrentThread.Priority = ThreadPriority.AboveNormal; Thread.CurrentThread.Priority = ThreadPriority.AboveNormal;
TimeSpan delay = TimeSpan.FromMilliseconds(10); DateTime next;
TimeSpan sleeptime; int i=0;
while (running) { while (running) {
try{
Tick(); Tick();
sleeptime = delay.Subtract(DateTime.Now - dernierTick); }
if(sleeptime > TimeSpan.Zero) catch (Exception ex) // Gestion d'erreurs ... au cas ou ... on affiche et on continue
Thread.Sleep(sleeptime); {
Info.Publish(ex.ToString());
}
next = dernierTick.AddMilliseconds (LOOPTIME);
while (DateTime.Now< next) Thread.Sleep(1);
// Version plus précise, si besoin :
// next = dernierTick.AddMilliseconds (LOOPTIME-1);
// while (DateTime.Now< next) Thread.Sleep(1);
// next = dernierTick.AddMilliseconds (LOOPTIME);
// while (DateTime.Now<next) Thread.Sleep(0);
} }
} }
@ -201,7 +218,7 @@ namespace DMX2
if (!Pause) { if (!Pause) {
if (deltaT > TimeSpan.FromMilliseconds (12)) // if (deltaT > TimeSpan.FromMilliseconds (LOOPTIME+2))
Info.Publish(string.Format ("{0}", deltaT)); Info.Publish(string.Format ("{0}", deltaT));
lock (this) { lock (this) {
@ -239,10 +256,9 @@ namespace DMX2
EventManager.ProcessEvents(); EventManager.ProcessEvents();
} }
// Cette fonction retourne quasi immédiatement, même si il y'a beaucoup a faire sur l'affichage
} }
// Cette fonction retourne quasi immédiatement, même si il y'a beaucoup a faire sur l'affichage
if(tickTime - derniereMaj > TimeSpan.FromMilliseconds(50)){ if(tickTime - derniereMaj > TimeSpan.FromMilliseconds(50)){
MainWindow.Win.ScheduleUpdate(); MainWindow.Win.ScheduleUpdate();
derniereMaj = DateTime.Now; derniereMaj = DateTime.Now;

View file

@ -8,7 +8,7 @@ namespace DMX2
static string lastinfo = string.Empty; static string lastinfo = string.Empty;
static public void Publish(string info) static public void Publish(string info)
{ {
last = DateTime.Now.AddSeconds(3); last = DateTime.Now.AddSeconds(5);
lastinfo = info; lastinfo = info;
} }
static public string GetInfo(){ static public string GetInfo(){

View file

@ -52,6 +52,9 @@ namespace DMX2
if(fullscreen) win.ToggleFullscreen(); if(fullscreen) win.ToggleFullscreen();
if(aguibtn) win.AfficheBoutonACGUI(); if(aguibtn) win.AfficheBoutonACGUI();
// Gestion des erreurs non traitées dans l'interface
GLib.ExceptionManager.UnhandledException += HandleUnhandledException;
// Lancement // Lancement
win.Show (); win.Show ();
Application.Run (); Application.Run ();
@ -61,5 +64,23 @@ namespace DMX2
Conduite.Courante.Dispose (); Conduite.Courante.Dispose ();
} }
} }
static void HandleUnhandledException (GLib.UnhandledExceptionArgs args)
{
// En cas d'erreur non traitée : on affiche et on demande ...
var dlg = new Dialog ("Erreur !", null, DialogFlags.Modal);
dlg.AddButton ("Continuer ..." , ResponseType.Cancel ).GrabDefault();
dlg.AddButton ("Fermer l'application" , ResponseType.Close );
dlg.VBox.Add (new Label (args.ExceptionObject.ToString()));
dlg.VBox.ShowAll ();
// Si l'utilisateur a clique sur le bon bouton, on continue comme si de rien n'etait...
args.ExitApplication= (ResponseType)dlg.Run() == ResponseType.Close ;
dlg.Destroy();
}
} }
} }