110,499
社区成员
发帖
与我相关
我的任务
分享
class Program
{
static void Main(string[] args)
{
MainThreadClass mt = new MainThreadClass();
mt.Start();
}
}
public class MainThreadClass
{
string[] arrStr = new string[] { "Microsoft", "IBM", "Oracle" };
public void Start()
{
foreach (string str in arrStr)
{
SubTreadClass st = new SubTreadClass();
st.Completed += new EventHandler(st_Completed);
ThreadStart ts = new ThreadStart(st.Method);
Thread th = new Thread(ts);
th.Name = str;
th.Start();
}
}
void st_Completed(object sender, EventArgs e)
{
Console.WriteLine("Thread completed.");
}
}
public class SubTreadClass
{
private event EventHandler m_eventCompleted = null;
public event EventHandler Completed
{
add { m_eventCompleted += value; }
remove { m_eventCompleted -= value; }
}
public void Method()
{
Console.WriteLine(string.Format("Current thread:{0}:{1}", Thread.CurrentThread.Name,
"MyClass.Method() executed."));
if (m_eventCompleted != null)
{
m_eventCompleted(this, EventArgs.Empty);
}
}
}