Thread 0 : 1 Thread 1 : 1 Thread 0 : 2 Thread 2 : 1 Thread 0 : 3 Thread 1 : 2 Thread 0 : 4 Thread 0 : 5 Thread 1 : 3 Thread 2 : 2 Thread 0 : 6 Thread 0 : 7 Thread 1 : 4 Thread 0 : 8 Thread 2 : 3 ID:0 が終了しました ID:1 が終了しました ID:2 が終了しました |
前田稔(Maeda Minoru)の超初心者のプログラム入門
![]()
![]()
using System.Threading; |
// スレッドでカウントする Class
class Count
{ string Id;
int Wait;
// Constructor
public Count(string id, int wait)
{ Id = id;
Wait = wait;
}
// Wait 間隔でカウント
public void Run()
{ for (int i = 1; i < 100; ++i)
{
System.Threading.Thread.Sleep(Wait);
Console.WriteLine(Id + " : " + i);
}
}
}
|
Thread[] threads = new Thread[3];
public Form1()
{
InitializeComponent();
for(int id=0; id<3; id++)
{ Count obj = new Count("Thread " + id, (id+1)*100);
threads[id] = new Thread(new ThreadStart(obj.Run));
threads[id].Start();
}
}
|
private void mouseDown(object sender, MouseEventArgs e)
{
for(int id=0; id<3; id++)
{
threads[id].Abort();
}
for(int id=0; id<3; id++)
{
threads[id].Join();
Console.WriteLine("ID:{0} が終了しました", id);
}
Application.Exit();
}
}
|
'WindowsApplication1.vshost.exe' (マネージ型): 'C:\DATA\C#\02Windows\03ThreadJoin\bin\Debug\WindowsApplication1.exe' が読み込まれました。 シンボルが読み込まれました。 Thread 0 : 1 Thread 1 : 1 Thread 0 : 2 Thread 2 : 1 Thread 0 : 3 Thread 1 : 2 Thread 0 : 4 Thread 0 : 5 Thread 1 : 3 Thread 2 : 2 Thread 0 : 6 Thread 0 : 7 Thread 1 : 4 Thread 0 : 8 Thread 2 : 3 'System.Threading.ThreadAbortException' の初回例外が WindowsApplication1.exe で発生しました。 ID:0 が終了しました ID:1 が終了しました ID:2 が終了しました スレッド 0x32c0 はコード 0 (0x0) で終了しました。 'System.Threading.ThreadAbortException' の初回例外が WindowsApplication1.exe で発生しました。 スレッド 0x38f0 はコード 0 (0x0) で終了しました。 'System.Threading.ThreadAbortException' の初回例外が WindowsApplication1.exe で発生しました。 スレッド 0x1efc はコード 0 (0x0) で終了しました。 スレッド 0xc18 はコード 0 (0x0) で終了しました。 スレッド 0x1038 はコード 0 (0x0) で終了しました。 プログラム '[3748] WindowsApplication1.vshost.exe: マネージ' はコード 0 (0x0) で終了しました。 |
![]()
[Next Chapter ↓] アニメーション&Thread
[Previous Chapter ↑] Thread Class