所以我从一个通过该control.invoke
方法调用委托的 GUI 程序中获得了一些代码,现在我想在控制台中执行此操作。
这是代码:
public class upnpforwarder
{
List<INatDevice> devices = new List<INatDevice>();
public upnpforwarder()
{
//Hook into the events so you know when a router has been detected or has gone offline
NatUtility.DeviceFound += DeviceFound;
//Start searching for upnp enabled routers
NatUtility.StartDiscovery();
}
delegate void AddDeviceDelegate(INatDevice device);
delegate void RemoveDeviceDelegate(INatDevice device);
// Adding devices to the list and listbox
private void AddDevice(INatDevice device)
{
if (!devices.Contains(device))
{
devices.Add(device);
IPAddress external = device.GetExternalIP();
Mapping[] maps = device.GetAllMappings();
//complicated stuff because the library only allows to display some data via .ToString() as far as I know
string str = device.ToString();
}
}
// Event that handles a new found device
private void DeviceFound(object sender, DeviceEventArgs args)
{
//Make it thread-safe
AddDeviceDelegate AddDeviceInstance = new AddDeviceDelegate(this.AddDevice);
this.Invoke(AddDeviceInstance, new object[] { args.Device });
}
什么是最好的替代方法:
this.Invoke(AddDeviceInstance, new object[] { args.Device });
?