そのため、メソッドを介してデリゲートを呼び出す GUI プログラムからいくつかのコードを取得しましcontrol.invoke
た。これをコンソールで実行したいと考えています。
これはコードです:
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 });
ですか?