Advertisement
Guest User

form1

a guest
Aug 20th, 2012
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Threading;
  10.  
  11. namespace CShrpColorNC
  12. {
  13. public partial class Form1 : Form
  14. {
  15. DateTime m_Form2Start;
  16. DateTime m_LastProgressCall = DateTime.Now;
  17. TimeSpan m_totalDif;
  18.  
  19. int m_csNumLines = 0;
  20. string m_csText = "";
  21. BackgroundWorker backgroundWorker1 = new BackgroundWorker();
  22.  
  23. public Form1()
  24. {
  25. InitializeComponent();
  26. InitializeBackgroundWorker();
  27. backgroundWorker1.WorkerReportsProgress = true;
  28. backgroundWorker1.WorkerSupportsCancellation = true;
  29. }
  30.  
  31. private void Form1_Load(object sender, EventArgs e)
  32. {
  33.  
  34. }
  35.  
  36. private void button1_Click(object sender, EventArgs e)
  37. {
  38. if (backgroundWorker1.IsBusy != true)
  39. {
  40. m_csText = this.m_bruteView.Text;
  41. backgroundWorker1.RunWorkerAsync();
  42. }
  43. resultLabel.Text = "Started...";
  44. }
  45.  
  46. private void InitializeBackgroundWorker()
  47. {
  48. backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
  49. backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler( backgroundWorker1_RunWorkerCompleted);
  50. backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler( backgroundWorker1_ProgressChanged);
  51. }
  52.  
  53. // This event handler is where the time-consuming work is done.
  54. private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
  55. {
  56. m_Form2Start= DateTime.Now;
  57. BackgroundWorker worker = sender as BackgroundWorker;
  58. ComplexSyntaxer.ComplexSyntaxer cs = new ComplexSyntaxer.ComplexSyntaxer();
  59. cs.Text = m_csText;
  60. m_csNumLines = cs.Lines.Length;
  61.  
  62. int nStartPos = 0;
  63. int i = 0;
  64. int nOriginalPos = cs.SelectionStart;
  65. while (i < cs.Lines.Length)
  66. {
  67. cs.m_strLine = cs.Lines[i];
  68. cs.m_nLineStart = nStartPos;
  69. cs.m_nLineEnd = cs.m_nLineStart + cs.m_strLine.Length;
  70. cs.m_nLineLength = cs.m_nLineEnd - cs.m_nLineStart;
  71.  
  72. cs.ProcessLine();
  73. worker.ReportProgress(i);//(int)(100 * (double)i / cs.Lines.Length));
  74. i++;
  75.  
  76. nStartPos += cs.m_strLine.Length + 1;
  77. }
  78. cs.SelectionStart = nOriginalPos;
  79. m_csText = cs.Rtf;
  80.  
  81. TimeSpan total = DateTime.Now.Subtract(m_Form2Start);
  82. Console.WriteLine("Total Time " + total.ToString());
  83. Console.WriteLine("Total Time2 " + m_totalDif.ToString());
  84. }
  85.  
  86. // This event handler updates the progress.
  87. private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
  88. {
  89. TimeSpan difInCalls = DateTime.Now.Subtract(m_LastProgressCall);
  90. m_LastProgressCall = DateTime.Now;
  91. m_totalDif = m_totalDif.Add(difInCalls);
  92. TimeSpan avgDif = new TimeSpan(0, 0, 0, 0, ((int)m_totalDif.TotalMilliseconds / m_csNumLines));
  93. double totalTime = m_csNumLines * avgDif.TotalSeconds;
  94. double timeLeft = totalTime - (DateTime.Now.Subtract(m_Form2Start).TotalSeconds);
  95. Console.WriteLine("TotalEstTime: " + totalTime + " TimeLeft: " + timeLeft);
  96.  
  97.  
  98. // Update the progress label
  99. resultLabel.Text = "Line " + e.ProgressPercentage.ToString() + " of " + m_csNumLines + " at " + (int)(100 * (double)e.ProgressPercentage / m_csNumLines) + "% loaded";
  100. }
  101.  
  102. // This event handler deals with the results of the background operation.
  103. private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  104. {
  105. if (e.Cancelled == true)
  106. {
  107. resultLabel.Text = "Canceled!";
  108. }
  109. else if (e.Error != null)
  110. {
  111. resultLabel.Text = "Error: " + e.Error.Message;
  112. }
  113. else
  114. {
  115. resultLabel.Text = "Done!";
  116. Form2 form2 = new Form2(m_csText);
  117. form2.ShowDialog();
  118. }
  119. }
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement