Advertisement
Guest User

Untitled

a guest
Jul 30th, 2014
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. public partial class Form1 : Form
  2. {
  3. public Form1()
  4. {
  5. InitializeComponent();
  6.  
  7. DoCount();
  8. }
  9. public void DoCount()
  10. {
  11. for (int i = 0; i < 100; i++)
  12. {
  13. objTextBox.Text = i.ToString();
  14. Thread.Sleep(100);
  15. }
  16. }
  17. }
  18.  
  19. public partial class Form1 : Form
  20. {
  21. public Form1()
  22. {
  23. InitializeComponent();
  24. }
  25.  
  26. private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
  27. {
  28. BackgroundWorker backgroundWorker = (BackgroundWorker)sender;
  29. for (int i = 0; i < 100; i++)
  30. {
  31. backgroundWorker.ReportProgress(i);
  32. Thread.Sleep(100);
  33. }
  34. }
  35.  
  36. private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
  37. {
  38. textBox1.Text = e.ProgressPercentage.ToString();
  39. }
  40.  
  41. private void button1_Click(object sender, EventArgs e)
  42. {
  43. backgroundWorker1.RunWorkerAsync();
  44. }
  45. }
  46.  
  47. public partial class Form1 : Form
  48. {
  49. public Form1()
  50. {
  51. InitializeComponent();
  52. DoCount();
  53. }
  54. public void DoCount()
  55. {
  56. Thread t = new Thread(new ThreadStart(delegate
  57. {
  58. for (int i = 0; i < 100; i++)
  59. {
  60. this.Invoke((Action) delegate { objTextBox.Text = i.ToString(); });
  61. Thread.Sleep(1000);
  62. }
  63. }));
  64. t.IsBackground = true;
  65. t.Start();
  66. }
  67. }
  68.  
  69. public partial class Form1 : Form
  70. {
  71. private SynchronizationContext c;
  72. private Thread t;
  73. private EventWaitHandle pause =
  74. new EventWaitHandle(false, EventResetMode.ManualReset);
  75.  
  76. public Form1()
  77. {
  78. this.InitializeComponent();
  79. this.c = SynchronizationContext.Current;
  80. }
  81.  
  82. private void Form1Activated(object sender, EventArgs e)
  83. {
  84. this.t = new Thread(new ThreadStart(delegate
  85. {
  86. this.pause.Reset();
  87. while (this.t.IsAlive && !this.pause.WaitOne(1000))
  88. {
  89. this.c.Post(
  90. state => this.label1.Text = DateTime.Now.ToString(),
  91. null);
  92. }
  93. }));
  94. this.t.IsBackground = true;
  95. this.t.Start();
  96. }
  97.  
  98. private void Form1Deactivate(object sender, EventArgs e)
  99. {
  100. this.pause.Set();
  101. this.t.Join();
  102. }
  103.  
  104. /// <summary>
  105. /// Button1s the click.
  106. /// </summary>
  107. /// <param name="sender">The sender.</param>
  108. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
  109. private void Button1Click(object sender, EventArgs e)
  110. {
  111. this.Close();
  112. }
  113. }
  114.  
  115. this.Update();
  116.  
  117. if (this.textBox1.InvokeRequired)
  118. {
  119. SetTextCallback d = new SetTextCallback(SetText);
  120. this.Invoke(d, new object[] { text });
  121. }
  122.  
  123. using System.ComponentModel;
  124. using System.Threading;
  125. using System.Windows.Forms;
  126.  
  127. namespace tester
  128. {
  129. public partial class Form1 : Form
  130. {
  131. public Form1()
  132. {
  133. InitializeComponent();
  134. if (!backgroundWorker1.IsBusy)
  135. backgroundWorker1.RunWorkerAsync();
  136. }
  137.  
  138. /// <summary>
  139. /// This delegate enables asynchronous calls for setting the text property on a control.
  140. /// </summary>
  141. delegate void SetTextCallback(string status);
  142.  
  143. private void BackgroundWorker1DoWork(object sender, DoWorkEventArgs e)
  144. {
  145. for (var i = 0; i < 100; i++)
  146. {
  147. backgroundWorker1.ReportProgress(i);
  148. Thread.Sleep(100);
  149. }
  150. }
  151.  
  152. private void BackgroundWorker1ProgressChanged(object sender, ProgressChangedEventArgs e)
  153. {
  154. if (label1.InvokeRequired)
  155. Invoke(new SetTextCallback(SetLabelText), new object[] { e.ProgressPercentage.ToString()});
  156. else
  157. SetLabelText(e.ProgressPercentage.ToString());
  158. }
  159.  
  160. private void SetLabelText(string text)
  161. {
  162. label1.Text = text;
  163. }
  164. }
  165. }
  166.  
  167. for (int i = 0; i < 100; i++)
  168. {
  169. objTextBox.Text = i.ToString();
  170. Application.DoEvents();
  171. Thread.Sleep(100);
  172. }
  173.  
  174. public partial class Form1 : Form
  175. {
  176. public Form1()
  177. {
  178. InitializeComponent();
  179. }
  180.  
  181. private void Form1_Load(object sender, EventArgs e)
  182. {
  183. Action countUp = this.CountUp;
  184. countUp.BeginInvoke(null, null);
  185. }
  186.  
  187. private void CountUp()
  188. {
  189. for (int i = 0; i < 100; i++)
  190. {
  191. this.Invoke(new Action<string>(UpdateTextBox), new object[] { i.ToString() });
  192. Thread.Sleep(100);
  193. }
  194. }
  195.  
  196. private void UpdateTextBox(string text)
  197. {
  198. this.textBox1.Text = text;
  199. }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement