Advertisement
Guest User

Untitled

a guest
Jan 9th, 2012
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 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.  
  10. namespace WindowsFormsApplication1
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18.  
  19. private void SetText(string cntName,string msg)
  20. {
  21. if (this.InvokeRequired)
  22. {
  23.  
  24. //Invoke(new SetTextCallback(SetText), new object[] { cntName }, new object[] { msg });
  25. Invoke(new SetTextCallback(SetText), cntName , msg );
  26.  
  27. return;
  28. }
  29.  
  30. switch (cntName)
  31. {
  32. case shareData.Control.button1:
  33. break;
  34. case shareData.Control.button2:
  35. break;
  36. case shareData.Control.button3:
  37. break;
  38. case shareData.Control.button4:
  39. break;
  40. case shareData.Control.button5:
  41. break;
  42. case shareData.Control.button6:
  43. break;
  44. case shareData.Control.button7:
  45. break;
  46. case shareData.Control.button8:
  47. break;
  48. case shareData.Control.button9:
  49. break;
  50. case shareData.Control.button12:
  51. break;
  52. case shareData.Control.textBox1:
  53. textBox1.Text = (msg);
  54. break;
  55. case shareData.Control.textBox2:
  56. textBox2.AppendText(msg);
  57. break;
  58. default:
  59. break;
  60. }
  61. }
  62.  
  63. private void Form1_Shown(object sender, EventArgs e)
  64. {
  65. workerThread t = new workerThread(SetText);
  66. t.Start();
  67. }
  68. }
  69. }
  70.  
  71. public class shareData
  72. {
  73.  
  74. public struct Control
  75. {
  76. public const string textBox1 = "textBox1";
  77. public const string textBox2 = "textBox2";
  78. public const string label1 = "label1";
  79. public const string label2 = "label2";
  80. public const string label3 = "label3";
  81. public const string label4 = "label4";
  82. public const string label5 = "label5";
  83. public const string label6 = "label6";
  84. public const string label7 = "label7";
  85. public const string label8 = "label8";
  86. public const string button1 = "button1";
  87. public const string button2 = "button2";
  88. public const string button3 = "button3";
  89. public const string button4 = "button4";
  90. public const string button5 = "button5";
  91. public const string button6 = "button6";
  92. public const string button7 = "button7";
  93. public const string button8 = "button8";
  94. public const string button9 = "button9";
  95. public const string button12 = "button12";
  96. public const string listView1 = "listView1";
  97. }
  98. }
  99.  
  100.  
  101. using System;
  102. using System.Collections.Generic;
  103. using System.Linq;
  104. using System.Text;
  105. using System.Threading;
  106.  
  107. namespace WindowsFormsApplication1
  108. {
  109. delegate void SetTextCallback(string cntName, string msg);
  110.  
  111. class workerThread
  112. {
  113. SetTextCallback _SetText;
  114.  
  115. int TID;
  116.  
  117. public workerThread(SetTextCallback SetText)
  118. {
  119. _SetText = SetText;
  120. TID = 0;
  121. }
  122.  
  123. Thread t;
  124. public void Start()
  125. {
  126. t = new Thread(new ThreadStart(worker));
  127. t.Start();
  128. }
  129.  
  130.  
  131. void worker()
  132. {
  133. int tid = TID;
  134. TID++;
  135. for (int i = 0; i < 100; i++)
  136. {
  137. _SetText(shareData.Control.textBox1, i.ToString());
  138. _SetText(shareData.Control.textBox2, i.ToString());
  139. System.Threading.Thread.Sleep(1000);
  140. }
  141. }
  142.  
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement