我尝试为我的最终项目修改Wiimote Whiteboard应用程序。但我在编程语言C#方面的经验很少。事实上,我只用了几个月就学习了C#。
我不知道几行代码的含义是什么。有谁可以帮助我?
//??
public event EventHandler Connected;
public event EventHandler<EventArgs<string>> Error;
public event EventHandler ConnectionFailed;
//?
public Thread Worker;
//??
Object lockobj = new Object();
//??
public void ConnectWiiMotes(bool DisconnectOld)
{
lock(lockobj)
{
if (Worker != null && Worker.IsAlive)
return;
Worker = new Thread(new ThreadStart(
delegate() { this.Connect(DisconnectOld); }));
Cancel = false;
Worker.Start();
}
}
//??
public bool Cancel { get; set; }
//??
private void LogError(string error)
{
if (Error != null)
Error(this, new EventArgs<string>(error));
} here
答案 0 :(得分:1)
希望这会有所帮助:
// Assign event handlers, read more about events here:
// http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx
public event EventHandler Connected;
public event EventHandler<EventArgs<string>> Error;
public event EventHandler ConnectionFailed;
// Creates a variable that defines a thread. (A unit under a process where some code run)
// Read more about threads here:
// http://msdn.microsoft.com/en-us/library/aa645740(v=vs.71).aspx
public Thread Worker;
Object lockobj = new Object();
// Creates new thread (long story short: think about a thread as a process) that runs a method that somehow connects the wii motes.
public void ConnectWiiMotes(bool DisconnectOld)
{
// Using the lockobj to ensure the method run once at a time
// Read about the lock design pattern and C# locking here:
// http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx
lock (lockobj)
{
// Make sure the thread is not already running.
if (Worker != null && Worker.IsAlive)
return;
// Runs the connection method inside the thread
Worker = new Thread(new ThreadStart(
delegate() { this.Connect(DisconnectOld); }));
Cancel = false;
Worker.Start();
}
}
// If you want to cancel the current connection, you set this property to true.
public bool Cancel { get; set; }
// In case of error, this method raises the error event with the error message.
// You can hook up to this event and catch the error later.
private void LogError(string error)
{
if (Error != null)
Error(this, new EventArgs<string>(error));
}
答案 1 :(得分:0)
我不明白这段代码背后的功能,但从技术上讲,我可以帮助你:
public event EventHandler Connected;
public event EventHandler<EventArgs<string>> Error;
public event EventHandler ConnectionFailed;
如上所述,3个语句是declaration for three Event Handlers
。
事件处理程序是响应特定事件并执行相应Callback
操作的人员
在这里,您可以考虑成功连接为一个事件,如果一个事件发生任何错误,连接失败为一个事件。
因此,在每种情况下都应该发生一些回调。这个事件处理程序完成了这项工作。
事件处理程序使用委托
匿名调用函数进行回调 public Thread Worker;
//??
Object lockobj = new Object();
//??
public void ConnectWiiMotes(bool DisconnectOld)
{
lock(lockobj)
{
if (Worker != null && Worker.IsAlive)
return;
Worker = new Thread(new ThreadStart(
delegate() { this.Connect(DisconnectOld); }));
Cancel = false;
Worker.Start();
}
}
您可以在上面的代码中看到如何调用委托来执行调用 回强>
Lock()
将确保没有其他工作线程同时访问受保护的代码。
事件处理和代理是C#中的一些高级概念,一旦你理解了......你将开始享受它们的力量。
希望这会有所帮助..
答案 2 :(得分:-1)
这看起来像是一个启动线程并维护一些状态变量和事件处理程序的基本框架。这段代码中没有太多“实现”来解释。这些只是简单的语言结构。