How i can refresh the progress bar after every copy?I try different things without success.....The progress bar will only after the for loop are finished update to 100%
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
try { double x=0,y=0,count=0; for (; count < copyCountTotal; ) { if ((copyCountX * (xSize + copyOffsetX)) > (x * (xSize + copyOffsetX))) { corelApp.ActiveSelection.Duplicate(x * (xSize + copyOffsetX), y); count++; x++; } else { x = 0; if (copyCountY <= ((ySize+copyOffsetY)/y)) { y -= ySize + copyOffsetY; } else { corelApp.ActiveDocument.InsertPages(1, false, 1); } } //os.OrderToFront(); Progress_Bar.Value = ((count / copyCountTotal)*100); TB_Info.Text = ((count / copyCountTotal) * 100).ToString(); corelApp.EventsEnabled = true; //corelApp.Optimization = true; //corelApp.Refresh(); corelApp.ActiveWindow.Refresh(); Thread.Sleep(200); } corelApp.ActiveSelection.Delete(); //corelApp.ActiveSelection.Move(-xSize * 2, 0); }
Running in UI Thread ?
can you explain me UI Thread?
UI (user interface) Thread is responsible for draw your controls on screen, if code flow stay locked in a loop the UI not will redraw, to solve this issue you can run your action in another thread, but this new thread can't modify the controls in UI Thread if you try will take a Invalid Operation Exception. However exists delegates.
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
private void Stop() { run = false; } private void Start() { run = true; Thread th = new Thread(new ThreadStart(LooperFunction)); th.IsBackground = true; th.Start(); } private void LooperFunction() { for (int i = 0; i < 100; i++) { if (!run) break; //Todo can cames here //Wpf use Current Dispatcher this.Dispatcher.Invoke(new Action(() => { //UI updates is here //Action is a default .net delegate progressBar.Value = i; })); Thread.Sleep(200); } }
This example works. But now i need to find out how i can use it with my copy function. So it need to figure out how it works. (i am a beginner)
I do some wrong....
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
bool run = false; private void Stop() { run = false; } private void Start() { run = true; Thread th = new Thread(new ThreadStart(LooperFunction)); th.IsBackground = true; th.Start(); } private void LooperFunction() { double x = 0, y = 0, count = 0; for (; count < copyCountTotal;) { if (!run) break; if ((copyCountX * (xSize + copyOffsetX)) > (x * (xSize + copyOffsetX))) { corelApp.ActiveSelection.Duplicate(x * (xSize + copyOffsetX), y); count++; x++; } else { x = 0; y -= ySize + copyOffsetY; } corelApp.EventsEnabled = true; //Wpf use Current Dispatcher this.Dispatcher.Invoke(new Action(() => { //UI updates is here //Action is a default .net delegate Progress_Bar.Value = ((count / copyCountTotal) * 100); TB_Info.Text = ((count / copyCountTotal) * 100).ToString(); })); Thread.Sleep(200); } } private void CopySign() { //corel.Shape os; TB_Info.Clear(); corelApp.ActiveDocument.Unit = corelApp.ActiveDocument.Rulers.HUnits; corelApp.ActiveSelection.Shapes.All().CreateSelection(); corelApp.ActiveSelection.Shapes.All().Group(); //var originalShape = corelApp.ActiveSelection; xSize = corelApp.ActiveSelection.SizeWidth; ySize = corelApp.ActiveSelection.SizeHeight; Progress_Bar.Value = 0; try { copyCountTotal = DtTbl.Rows.Count; } catch (Exception ex) { TB_Info.Text = ex.Message.ToString(); return; } if (copyCountTotal == 0) { TB_Info.Text = "Keine Excel Daten geladen"; return; } try { Start(); corelApp.ActiveSelection.Delete(); Stop(); //corelApp.ActiveSelection.Move(-xSize * 2, 0); } catch (System.Runtime.InteropServices.COMException) { TB_Info.Text = "Kein Objekt ausgewählt"; return; } catch (Exception ex) { TB_Info.Text = ex.Message.ToString(); return; } }
Use Locals windows in visual studio to check if you have a null variable. Go in Debug-> Windows -> Locals