Bonjour,

Un des problèmes .net est l'affichage de ProgressBar pendant un traitements long.

Le passage par un thread annexe est malheureusement obligatoire.On peut évidement utiliser un BackGroundWorker, mais c'est parfois un peu lourd pour activer une simple ProgressBar.

J'ai développé une petite classe plûtot facile à implémenter pour montrer dans une form "pop-up" l'avancement du traitement (non interruptible par l'utilisateur).
Voici un exemple de code utilisant la classe :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    SxProgress Progress = null ;
     private void ProgressTest_Click(object sender,EventArgs e)
    { 
      (Progress=new SxProgress()).Init ("XXXXX in progress...",1000,true,ProgressTest_ExecInThread);
    }
 
    private void ProgressTest_ExecInThread()
    {
     for (int i=0;i<1000;i++) 
     { 
       System.Threading.Thread.Sleep(5) ; // replace Sleep() by item i process code   
       Progress.Value=i  ;  
     }
     Progress.Value=-1; // mandatory at end of procedure
    }
Qu'en pensez-vous ?

Voici le code de la classe appelée:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
    using System.Runtime.InteropServices;
    …
       internal class SxProgress
    { // simple implementation of a progress pop-up form
      internal int                          Value        = 0 ; // indicates progress, must be set -1 at end of tread proc
      private  SxProgressForm               ProgressForm = new SxProgressForm() ;
      private  System.Threading.ThreadStart StartProc    = null ;
 
      internal void Init(string Title,int MaxCount,bool ShowCountLabel,System.Threading.ThreadStart vStartProc)
      { 
        StartProc = vStartProc ;
        ProgressForm.Init(this,Title,MaxCount,ShowCountLabel) ;
        ProgressForm.Dispose() ;
        ProgressForm=null ;
      }
 
      internal void ThreadStart()
      { 
          try { StartProc() ; }
          catch (Exception Ex) { ProgressForm.TheException=Ex ; Value=-1 ; }
      }
 
      private class SxProgressForm : Form 
      {
        private Label       TheLabel       = null ;
        private ProgressBar TheProgressBar = null ;
        private bool        Completed      = false;
        private SxProgress  Owner          = null ;
        internal Exception  TheException   = null ;
        private System.Windows.Forms.Timer TheTimer = new System.Windows.Forms.Timer() ;
        private delegate void DoUpdateD(int ProgressValue);
 
        protected override CreateParams CreateParams 
        { // remove SYSMENU 
          get { CreateParams cp = base.CreateParams; cp.ClassStyle = cp.ClassStyle | 0x200 ; return cp; }
        }
 
        internal void Init(SxProgress vOwner, string Title,int MaxCount,bool ShowLabel)
        {
          Owner                  = vOwner ;
          FormBorderStyle        = FormBorderStyle.FixedDialog ;
          MaximizeBox            = false ;
          ShowInTaskbar          = false ;
          StartPosition          = FormStartPosition.CenterParent ;
          ClientSize             = new Size(450,25) ; 
          Text                   = Title ;
          TheTimer.Interval      = 333 ;
          TheTimer.Tick          +=TheTimer_Tick ;
          if (ShowLabel) 
          {
            TheLabel          = new Label() ;
            TheLabel.Text     = "0/"+MaxCount ;
            TheLabel.Parent   = this   ;
            TheLabel.Location = new Point(Width-100,3) ;
          }
          TheProgressBar         = new ProgressBar() ;
          TheProgressBar.Size    = new Size (Width-(ShowLabel?115:25),10) ;
          TheProgressBar.Location= new Point(10,7) ;
          TheProgressBar.Parent  = this ;
          TheProgressBar.Maximum = MaxCount ;
          Completed = false;
          Show() ;
          Hide() ;
          System.Threading.Thread TheProgressThread = new System.Threading.Thread(Owner.ThreadStart);
          TheProgressThread.CurrentCulture = Application.CurrentCulture ;
          TheProgressThread.Start();
          TheTimer.Enabled=true ;
          ShowDialog() ;
        }
 
        private void TheTimer_Tick(object sender, EventArgs e)
        {
          TheTimer.Enabled=false ;
          if (Owner.Value<0)
          { 
            DialogResult = DialogResult.OK ; Completed=true ; 
            if (TheException!=null) throw new Exception(TheException.Message+Environment.NewLine+TheException.StackTrace) ;
          } 
          else 
          {
            if (Owner.Value<TheProgressBar.Maximum && TheProgressBar.Value!=Owner.Value+1) 
            {
              if (TheLabel!=null) TheLabel.Text=(Owner.Value+1)+"/"+TheProgressBar.Maximum ;
              TheProgressBar.Value=Owner.Value+1;
            }
            TheTimer.Enabled = Owner.Value>=0 ;
          }
        }
 
      }// SxProgressForm
 
    } // SxProgress