01 | using System; |
02 | using System.Collections.Generic; |
03 | using System.ComponentModel; |
04 | using System.Data; |
05 | using System.Drawing; |
06 | using System.Text; |
07 | using System.Windows.Forms; |
08 | using System.Threading; |
09 |
10 | namespace Invoke |
11 | { |
12 | public partial class Form1 : Form |
13 | { |
14 | public Form1() |
15 | { |
16 | InitializeComponent(); |
17 | } |
18 |
19 | private void Form1_Load( object sender, EventArgs e) |
20 | { |
21 | Thread NewThread = new Thread( new ThreadStart(NewThreadMethod)); //建立測試用的執行緒 |
22 | NewThread.Start(); //啟動測試用的執行緒 |
23 | } |
24 |
25 | //原執行緒,被其它執行緒呼叫 |
26 | static void Method( int Param) |
27 | { |
28 | int i = Param; |
29 | } |
30 |
31 | //宣告一個委派,定義參數 |
32 | delegate void MyDelegate( int Param); |
33 |
34 | //實作委派,指向員執行緒中被呼叫的Method |
35 | MyDelegate ShowData = new MyDelegate(Method); |
36 |
37 | //測試用的執行緒,在此呼叫原執行緒 |
38 | void NewThreadMethod() |
39 | { |
40 | int i = 0; |
41 | while ( true ) |
42 | { |
43 | this .Invoke( this .ShowData, i); |
44 | Thread.Sleep(2000); |
45 | } |
46 | } |
47 | } |
48 | } |
沒有留言:
張貼留言