enter code here
我在 .cs 文件中有这个属性。每当我设置此属性时,与它关联的事件就会被触发。
public event Action ResponseReceived;
private string response;
public string Response
{
get
{
return response;
}
set
{
response = value;
if (ResponseReceived != null) { ResponseReceived(); }
}
}
现在的问题是当我这样做时在另一个文件中
ResponseReceived += new Action(function_ResponseReceived);
void function_ResponseReceived()
{
//change to gui thread
if (InvokeRequired)
{
this.BeginInvoke(new Action(function_ResponseReceived), new object[] { });
return;
}
textBox1.Text = Response;
}
Response = "yes";
. . . (几行之后)。. .
Response = "no";
但yes
不会触发与事件关联的函数,因为Response = "no";
总是触发(该Response
字段的更新速度比触发事件所需的时间快,所以我猜它被覆盖了)。有没有一种方法可以让我设置属性时与事件相关的函数都正确触发