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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
| // ================================== SxProgress=========================================
/*
// SxProgress is simple implementation of a progress pop-up form as shown in following example
using System.Runtime.InteropServices;
private void ProgressTest_Click(object sender,EventArgs e)
{
// SxProgress.StopImage=MyStopImage ;
object[] UserObjects = null ;
// To pass parameters to procedure, replace this instruction by:
// object[] UserObjects = new object[] { Param1, Param2 } ;
if (SxProgress.Execute("XXXXX in progress...",300,true,true,ProgressTest_ExecInThread,UserObjects))
MessageBox.Show("Terminated") ;
else MessageBox.Show("Cancelled by user") ;
}
private bool ProgressTest_ExecInThread(int ItemIndex,object[] UserObjects)
{
System.Threading.Thread.Sleep(5) ;
LastItemIndex=Math.Max(LastItemIndex,ItemIndex) ;
System.Threading.Thread.Sleep(1) ;
// display a SubProgressBar for first 100 items, then hide it
if (ItemIndex<=100) for (int i=0;i<100;i++) SxProgress.SetSubProgressValue(i==100?-1:i,100,UserObjects) ; ²
return true ;
}
*/
internal class SxProgress
{ // simple interface for of a progress pop-up form
internal delegate bool ProcD(int ItemIndex,object[] UserObjects) ;
internal static Image StopImage = null ;
internal static bool Execute (string Title,int MaxCount,bool ShowCountLabel,bool StopAllowed,ProcD UserProc,Object[] UserObjects)
{ // function calls are executed in a separate thread in order to have a progress HMI.
SxProgressForm ProgressForm = new SxProgressForm() ;
bool Result=ProgressForm.Execute(false,Title,MaxCount,ShowCountLabel,StopAllowed,UserProc,UserObjects) ;
Exception TheException = ProgressForm.TheException;
ProgressForm.Dispose() ;
if (TheException!=null) throw new Exception(TheException.Message+Environment.NewLine+TheException.StackTrace) ;
return Result ;
}
internal static bool ExecuteMulti(string Title,int MaxCount,bool ShowCountLabel,bool StopAllowed,ProcD UserProc,Object[] UserObjects)
{ // function calls are executed in parallel in different threads with a progress HMI (threads count is equal to cores count)
SxProgressForm ProgressForm = new SxProgressForm() ;
bool Result=ProgressForm.Execute(true,Title,MaxCount,ShowCountLabel,StopAllowed,UserProc,UserObjects) ;
Exception TheException = ProgressForm.TheException;
ProgressForm.Dispose() ;
if (TheException!=null) throw new Exception(TheException.Message+Environment.NewLine+TheException.StackTrace) ;
return Result ;
}
internal static bool ExecuteNoThread(string Title,int MaxCount,bool ShowCountLabel,bool StopAllowed,ProcD UserProc,Object[] UserObjects)
{ // Execute function calls in application main thread for debug purpose. No Progress HMI.
bool FirstCall = true ;
object[] NewObjects = SxProgressForm.UpdateUserObjects(UserObjects,FirstCall) ;
bool Cancelled = false ;
for (int i=0;i<MaxCount && !Cancelled;i++) { Cancelled = !UserProc(i,NewObjects) ; FirstCall=false ; }
return !Cancelled ;
}
internal static void SetSubProgressValue(int SubValue,int SubMaxCount,object[] UserObjects)
{ // Allows to visualize a sub progress bar inside a function call (sub progress bar is cleared if SubMaxCount<0)
object UserObject = UserObjects[UserObjects.Length-1] ;
if (UserObject!=null)
{
if (UserObject is SxProgressForm ) { ((SxProgressForm )UserObject).SubValue=SubValue ; ((SxProgressForm)UserObject).SubMaxCount=SubMaxCount ; }
else SxProgressForm.SetSubProgress(UserObject,SubValue,SubMaxCount) ;
}
}
internal static int GetThreadIndex(object[] UserObjects)
{ // Provides the index of the thread in case of multithread execution.
// The objective is to associate to each thread different instances of process classes
object UserObject = UserObjects[UserObjects.Length-1] ;
return UserObject==null || UserObject is SxProgressForm ? 0 : SxProgressForm.GetMultiThreadIndex(UserObjects) ;
}
internal static bool GetThreadFirstCall(object[] UserObjects)
{ // Indicates if it is the the first function call of a thread.
// Objective : initialize the process classes associated to the thread.
object UserObject = UserObjects[UserObjects.Length-1] ;
return UserObject==null ? (bool)UserObject : UserObject is SxProgressForm ? ((SxProgressForm)UserObject).FirstCall : SxProgressForm.GetMultiThreadFirstCall(UserObjects) ;
}
private class SxProgressForm : Form
{
private bool OneEnded = false;
private bool AllEnded = false;
private bool Cancelled = false;
internal Exception TheException = null ;
private bool MultiThread = false;
private int Value = 0 ; // Progress index from 0 to MaxCount
private string Title = "" ;
private int MaxCount = 0 ;
private object[] UserObjects = null ;
private SxProgress.ProcD UserProc = null ;
// common controls
private SxProgressPanel TheProgressPanel = null ;
private Button TheStopButton = null ;
private Label TheStopLabel = null ;
internal int SubMaxCount = 0 ; // Count for SubProgressBar
internal int SubValue = -1 ; // SubProgress index from 0 to SubMaxCount (-1:SubProgressBar not visible)
private int SubMaxCountPrec = 0 ;
private int SubValuePrec = -1 ;
// multi thread variables
private int ProgressThreadIdx = 0 ;
private List<SxProgressThread> ProgressThreads = null ;
private List<SxProgressPanel> TheProgressPanels = new List<SxProgressPanel>();
private int ProcessorsCount = Environment.ProcessorCount ;
private int ThreadsCount = 1 ;
private List<Point> Blocks = new List<Point>() ;
// mono thread variables
internal bool FirstCall = true ;
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 bool Execute(bool vMultiThread,string vTitle,int vMaxCount,bool ShowLabel,bool StopAllowed,ProcD vUserProc,object[] vUserObjects)
{
MultiThread = vMultiThread ;
UserProc = vUserProc ;
UserObjects = vUserObjects ;
Title = vTitle ;
MaxCount = vMaxCount ;
FormBorderStyle = FormBorderStyle.FixedDialog ;
MaximizeBox = false ;
ShowInTaskbar = false ;
StartPosition = FormStartPosition.CenterParent ;
ClientSize = new Size(450,25) ;
Text = Title ;
// BackColor = Color.FromArgb(255,255,160);
BackColor = Color.Silver;
TheTimer.Interval = 333 ;
TheProgressPanel = new SxProgressPanel() ;
int ProgressBar_Right ;
TheProgressPanel.Init(ShowLabel,MaxCount,-1,this,out ProgressBar_Right) ;
HmiInit(ShowLabel,StopAllowed,ProgressBar_Right) ;
Show() ;
Hide() ;
if (MultiThread)
{
BuildBlocks() ;
ThreadsCount=Math.Min(ProcessorsCount,Blocks.Count) ;
if (ThreadsCount<=1) MultiThread = false ;
}
// MessageBox.Show("Multithread="+MultiThread+" BlockSize="+(MultiThread?Blocks[0].Y-Blocks[0].X+1:MaxCount)+" BlocksCount="+Blocks.Count) ;
if (MultiThread)
{ // Multiple Threads
TheTimer.Tick +=TheTimer_TickMulti ;
ProgressThreads = new List<SxProgressThread>() ;
for (int i=0;i<ThreadsCount;i++)
{
TheProgressPanels.Add(new SxProgressPanel()) ;
TheProgressPanels[i].Init(ShowLabel,Blocks[0].Y-Blocks[0].X+1,i,this,out ProgressBar_Right) ;
TheProgressPanels[i].BringToFront() ;
ProgressThreads.Add(new SxProgressThread(this,TheProgressPanels[i],ProgressThreads.Count)) ;
}
Height=Height+ThreadsCount*TheProgressPanels[0].Height ;
for (int i=0;i<ThreadsCount;i++)
{
System.Threading.Thread TheProgressMultiThread = new System.Threading.Thread(ThreadStartMulti);
TheProgressMultiThread.CurrentCulture = Application.CurrentCulture ;
TheProgressMultiThread.Start();
}
}
else
{ // single Thread
TheTimer.Tick +=TheTimer_Tick ;
System.Threading.Thread TheProgressThread = new System.Threading.Thread(ThreadStart);
TheProgressThread.CurrentCulture = Application.CurrentCulture ;
TheProgressThread.Start();
}
TheTimer.Enabled=true ;
ShowDialog() ;
return !Cancelled ;
}
private void HmiInit(bool ShowLabel,bool StopAllowed,int ProgressBar_Right)
{
if (StopAllowed)
{
TheStopButton = new Button() ;
TheStopButton.TextAlign = ContentAlignment.MiddleLeft ;
TheStopButton.ImageAlign = ContentAlignment.MiddleLeft ;
TheStopButton.TextImageRelation = TextImageRelation.ImageBeforeText ;
TheStopButton.Image = SxProgress.StopImage ;
TheStopButton.Size = new Size(120+(TheStopButton.Image==null?0:TheStopButton.Image.Width+7),
Math.Max(25,(TheStopButton.Image==null?0:TheStopButton.Image.Height+7))) ;
TheStopButton.Location = new Point(ProgressBar_Right-TheStopButton.Width,TheProgressPanel.Bottom+5) ;
TheStopButton.BackColor = Color.LightGray ;
TheStopButton.Text = " Stop process" ;
TheStopButton.Font = new Font("Arial",10,FontStyle.Bold) ;
TheStopButton.Parent = this ;
TheStopButton.Click += TheStopButton_Click ;
ClientSize = new Size(ClientSize.Width,TheStopButton.Bottom+5) ;
TheStopButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Left ;
// Stop label ready for parallel process not yet implemented
TheStopLabel = new Label() ;
TheStopLabel.Text = "Process interrupted. Please wait." ;
TheStopLabel.Parent = this ;
TheStopLabel.ForeColor = Color.FromArgb(255,72,72) ;
TheStopLabel.Font = new Font("Arial",11,FontStyle.Bold) ;
TheStopLabel.Visible = false ;
TheStopLabel.Location = new Point(10,3) ;
TheStopLabel.Width = 300 ;
}
}
internal static object[] UpdateUserObjects(object[] UserObjects,object AddedObject)
{
object[] Result=new object[UserObjects==null?1:UserObjects.Length+1] ;
if (UserObjects!=null) for (int i=0;i<UserObjects.Length;i++) Result[i]=UserObjects[i] ;
Result[Result.Length-1]=AddedObject ;
return Result ;
}
private void ThreadStart()
{
try
{
object[] NewObjects = UpdateUserObjects(UserObjects,this) ;
for (int i=0;i<MaxCount && !Cancelled;i++) { Cancelled = !UserProc(i,NewObjects) ; Value=i ; FirstCall=false ; }
OneEnded=true ;
}
catch (Exception Ex) { TheException=Ex ; OneEnded=true ; Cancelled=true ; }
}
private void ThreadStartMulti()
{
int ThreadIndex ;
SxProgressThread ProgressThread ;
lock (ProgressThreads) { ThreadIndex = ProgressThreadIdx++ ; ProgressThread = ProgressThreads[ThreadIndex] ; }
object[] NewObjects = UpdateUserObjects(UserObjects,ProgressThread) ;
try
{
bool Continue = true ;
Point Block ;
while (!Cancelled && (Block=GetNextBlock()).Y>0)
{
ProgressThread.BlockValue = 0 ;
ProgressThread.BlockCount = Block.Y -Block.X+1 ;
for (int i=Block.X;i<=Block.Y && !Cancelled;i++)
{
Continue=UserProc(i,NewObjects) ;
if (!Continue) Cancelled=true ;
ProgressThread.BlockValue=i-Block.X+1 ;
ProgressThread.Value++ ;
ProgressThread.FirstCall = false ;
}
}
ProgressThread.Ended=true ;
}
catch (Exception Ex) { ProgressThread.TheException=Ex ; ProgressThread.Ended=true ; Cancelled=true ; }
}
private void TheStopButton_Click(object sender, EventArgs e)
{
TheTimer.Enabled = false ;
Cancelled=MessageBox.Show("Stop required. Confirm ?",Title,MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes ;
if (Cancelled) OneEnded = true ;
TheTimer.Enabled = true ;
}
private void TheTimer_Tick(object sender, EventArgs e)
{
TheTimer.Enabled=false ;
TheProgressPanel.SetProgressBarValue(Value,MaxCount) ;
if (SubValuePrec!=SubValue || SubMaxCountPrec!=SubMaxCount)
{
TheProgressPanel.SetSubProgressBarValue(SubValue,SubMaxCount) ;
SubValuePrec = SubValue ;
SubMaxCountPrec= SubMaxCount;
}
if (OneEnded)
{
Refresh() ;
DialogResult = DialogResult.OK ;
if (Cancelled) { TheProgressPanel.SetProgressBarValue(-1,1) ; if (TheStopLabel!=null) TheStopLabel.Visible=true ; }
}
else TheTimer.Enabled = true ;
}
private void TheTimer_TickMulti(object sender, EventArgs e)
{
TheTimer.Enabled=false ;
int AllValue=0 ;
AllEnded=true ;
for (int i=0;i<ProgressThreads.Count;i++)
{
SxProgressThread ProgressThread = ProgressThreads[i] ;
AllValue+=ProgressThread.Value ;
AllEnded=AllEnded && ProgressThread.Ended ;
if (ProgressThread.Ended && !ProgressThread.HideProcessed)
{ // Hide Phread progress bar
Height=Height-TheProgressPanels[i].Height ;
TheProgressPanels[i].Visible = false ;
ProgressThread .HideProcessed = true ;
}
else
{ // Update Thread progress bars
TheProgressPanels[i].SetProgressBarValue(ProgressThread.BlockValue,ProgressThread.BlockCount ) ;
if (ProgressThread.SubValuePrec!=ProgressThread.SubValue || ProgressThread.SubMaxCountPrec!=ProgressThread.SubMaxCount)
{
TheProgressPanels[i].SetSubProgressBarValue(ProgressThread.SubValue,ProgressThread.SubMaxCount) ;
ProgressThread.SubValuePrec = ProgressThread.SubValue ;
ProgressThread.SubMaxCountPrec= ProgressThread.SubMaxCount;
}
}
OneEnded = OneEnded || ProgressThreads[i].Ended ;
AllEnded = AllEnded && ProgressThreads[i].Ended ;
if (ProgressThreads[i].TheException!=null) { TheException=ProgressThreads[i].TheException ; Cancelled=true ; }
}
TheProgressPanel.SetProgressBarValue(Cancelled?-1:AllValue,MaxCount) ;
if (Cancelled) { TheStopLabel.Visible=true ; TheStopLabel.BringToFront() ; TheStopButton.Enabled = false; }
if (AllEnded)
{
Refresh() ;
DialogResult = DialogResult.OK ;
if (TheException!=null) throw new Exception(TheException.Message+Environment.NewLine+TheException.StackTrace) ;
}
else TheTimer.Enabled = true ;
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr w, IntPtr l);
private void BuildBlocks()
{
int BlockSize = MaxCount<25 ? 1 : MaxCount<50 ? 2 : MaxCount<125 ? 5 :
MaxCount<250 ? 10: MaxCount<500 ? 20: MaxCount<1250? 50:100 ;
BlockSize = Math.Max(Math.Min((MaxCount+ProcessorsCount-1)/ProcessorsCount,BlockSize),1) ;
for (int i=0;i<MaxCount;i+=BlockSize) Blocks.Add(new Point(i,Math.Min(i+BlockSize-1,MaxCount-1))) ;
}
internal Point GetNextBlock()
{
// Warning : routine must be thread safe
Point Result = new Point(0,-1) ;
lock (Blocks) { if (Blocks.Count>0) { Result=Blocks[0] ; Blocks.RemoveAt(0) ; } }
return Result;
}
internal static void SetSubProgress(object ProgressThreadObj,int SubValue,int SubMaxCount)
{
((SxProgressThread)ProgressThreadObj).SubValue = SubValue ;
((SxProgressThread)ProgressThreadObj).SubMaxCount = SubMaxCount ;
}
internal static int GetMultiThreadIndex(object[] UserObjects)
{
return ((SxProgressThread)UserObjects[UserObjects.Length-1]).ThreadIndex ;
}
internal static bool GetMultiThreadFirstCall(object[] UserObjects)
{
return ((SxProgressThread)UserObjects[UserObjects.Length-1]).FirstCall ;
}
private class SxProgressThread
{
internal int ThreadIndex = 0 ;
internal SxProgressPanel ProgressPanel = null ;
internal Exception TheException = null ;
internal SxProgressForm ProgressForm = null ;
internal bool Ended = false;
internal int Value = 0 ; // Progress index from 0 to MaxCount
internal int BlockValue = 0 ;
internal int BlockCount = 0 ;
internal bool HideProcessed = false;
internal int SubValue = 0 ;
internal int SubMaxCount = 0 ;
internal int SubMaxCountPrec = 0 ;
internal int SubValuePrec = -1 ;
internal bool FirstCall = true ;
internal SxProgressThread(SxProgressForm vProgressForm, SxProgressPanel vProgressPanel,int vThreadIndex)
{
ProgressForm = vProgressForm ;
ProgressPanel= vProgressPanel ;
ThreadIndex = vThreadIndex ;
}
} // SxProgressThread
private class SxProgressPanel : Panel
{
private Label TheLabel = null ;
private ProgressBar TheProgressBar = null ;
private ProgressBar TheSubProgressBar = null ;
private int MaxCount = 0 ;
private int ThreadIndex = 0 ;
private bool ShowLabel = false;
private bool SubProgressVisible = false;
internal void Init(bool vShowLabel,int vMaxCount,int vThreadIndex,Control ParentControl,out int ProgressBar_Right)
{
ShowLabel = vShowLabel ;
MaxCount = vMaxCount ;
ThreadIndex = vThreadIndex ;
Dock = DockStyle.Top ;
Parent = ParentControl ;
Height = 29 ;
if (ShowLabel)
{
TheLabel = new Label() ;
TheLabel.Text = ThreadIndex<0 ? "0/"+MaxCount : "Thread "+(ThreadIndex<24?""+(char)((int)'A'+ThreadIndex):'Y'+(ThreadIndex-24).ToString()) ;
TheLabel.Parent = this ;
TheLabel.Location = new Point(Width-100,7) ;
TheLabel.ForeColor = ThreadIndex<0 ? Color.Black : Color.Gray ;
}
TheProgressBar = new ProgressBar() ;
TheProgressBar.Maximum = MaxCount ;
TheProgressBar .Size = new Size (Width-(ShowLabel?115:25),12) ;
TheProgressBar .Location = new Point(10,7) ;
TheProgressBar .Parent = this ;
ProgressBar_Right = TheProgressBar.Right ;
TheSubProgressBar = new ProgressBar() ;
TheSubProgressBar.Size = new Size (Width-(ShowLabel?115:25),6) ;
TheSubProgressBar.ForeColor = Color.Firebrick ;
TheSubProgressBar.Location = new Point(10,22) ;
TheSubProgressBar.Parent = this ;
TheSubProgressBar.Visible = false ;
// 1 = normal (green); 2 = error (red); 3 = warning (yellow).
SendMessage(TheProgressBar .Handle, 1040, (IntPtr)(ThreadIndex<0?1:3), IntPtr.Zero);
SendMessage(TheSubProgressBar.Handle, 1040, (IntPtr) 2 , IntPtr.Zero);
}
internal void SetProgressBarValue(int Value,int MaxCount)
{
if (MaxCount!=TheProgressBar.Maximum && MaxCount>=0) TheSubProgressBar.Maximum=MaxCount ;
if (Value<MaxCount && TheProgressBar.Value!=Value)
{
if (TheLabel!=null && ThreadIndex<0) TheLabel.Text=(Value+0)+"/"+TheProgressBar.Maximum ;
TheProgressBar.Value = Math.Max(0,Value);
}
if (Value<0)
{
TheProgressBar.Visible=false ;
if (TheLabel!=null) TheLabel.Visible=false ;
}
}
internal void SetSubProgressBarValue(int SubValue,int SubMaxCount)
{
if (SubValue<0) TheSubProgressBar.Visible=SubProgressVisible=false ;
else
{
if (SubMaxCount!=TheSubProgressBar.Maximum && SubMaxCount>=0) TheSubProgressBar.Maximum=SubMaxCount ;
if (SubValue<TheSubProgressBar.Maximum && TheSubProgressBar.Value!=SubValue+1) TheSubProgressBar.Value=SubValue+1;
if (!SubProgressVisible) TheSubProgressBar.Visible=SubProgressVisible=true ;
}
}
} // SxProgressPanel
}// SxProgressForm
} // SxProgress |