Shr*_*500 2 c# events nullreferenceexception
出于某种原因,我的事件抛出了一个空错误.我没有看到任何错误.
这是事件
public delegate void connectionSuccess(bool success);
public event connectionSuccess Connection;
Run Code Online (Sandbox Code Playgroud)
然后在一个函数我有这个女巫抛出null错误
Connection(true);
Run Code Online (Sandbox Code Playgroud)
在另一个调用连接函数的类中编辑*函数
Server.Connection += onConnection;
Server.startConnection();
Run Code Online (Sandbox Code Playgroud)
如果它没有连接,那就是null
.
试试这个:
var h = Connection;
if ( h!=null )
{
h(true);
}
Run Code Online (Sandbox Code Playgroud)
我首先分配一个(本地)变量,以克服多线程场景,其中检查null
和调用之间有修改.
即以下内容不是线程安全的:
if ( Connection!=null )
{
Connection(true); // Here, could already be null, again.
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1076 次 |
最近记录: |