【发布时间】:2011-06-10 17:01:31
【问题描述】:
class ClassA
{
public delegate void WriteLog(string msg);
private WriteLog m_WriteLogDelegate;
public ClassA(WriteLog writelog)
{
m_WriteLogDelegate = writelog;
Thread thread = new Thread(new ThreadStart(Search));
thread.Start();
}
public void Search()
{
/* ... */
m_WriteLogDelegate("msg");
/* ... */
}
}
class classB
{
private ClassA m_classA;
protected void WriteLogCallBack(string msg)
{
// prints msg
/* ... */
}
public classB()
{
m_classA = new ClassA(new WriteLog(WriteLogCallBack));
}
public void test1()
{
Thread thread = new Thread(new ThreadStart(Run));
thread.Start();
}
public void test2()
{
m_classA.Search();
}
public void Run()
{
while(true)
{
/* ... */
m_classA.Search();
/* ... */
Thread.Sleep(1000);
}
}
}
为什么是下面的代码
ClassB b = new ClassB();
b.test2()
打印“味精” 还有这个
ClassB b = new ClassB();
b.test1()
什么都不打印?
【问题讨论】:
-
我在
test1()看到一个帖子... -
test2 的代码在哪里?以及传递给 Thread 构造函数的 Run 方法的代码?
-
在代码中向下滚动;它就在那里。
-
ClassA的构造函数是什么样的?
标签: c# multithreading delegates