AddRef() public method

Claim a reference to a device.
Can only be called from code that can guarantee another reference is already held. Each call to AddRef() must later have exactly one matching call to RemoveRef().
public AddRef ( ) : void
return void
 private void DeviceAdded(CpDeviceList aList, CpDevice aDevice)
 {
     lock (this)
     {
         if (iDeviceList.Count == 0 && aDevice.Udn() == DeviceBasic.gDeviceName)
         {
             aDevice.AddRef();
             iDeviceList.Add(aDevice);
         }
     }
 }
Beispiel #2
0
 private void DeviceAdded(ControlPoint.CpDeviceList aList, ControlPoint.CpDevice aDevice)
 {
     lock (this)
     {
         if (!iListFrozen)
         {
             PrintDeviceInfo("Added", aDevice);
             aDevice.AddRef();
             iDeviceList.Add(aDevice);
         }
     }
 }
Beispiel #3
0
        public static void Main(string[] aArgs)
        {
            Console.WriteLine("TestPerformanceCpCs - starting");
            InitParams initParams = new InitParams();

            using (Library lib = Library.Create(initParams))
            {
                SubnetList     subnetList = new SubnetList();
                NetworkAdapter nif        = subnetList.SubnetAt(0);
                uint           subnet     = nif.Subnet();
                subnetList.Dispose();
                var       deviceListFactory = new CpUpnpDeviceListFactory(lib.StartCp(subnet));
                CpDevice  device            = null;
                Semaphore sem        = new Semaphore(0, 1);
                var       deviceList = deviceListFactory.CreateListServiceType("openhome.org", "TestBasic", 1,
                                                                               (aDeviceList, aDevice) =>
                {
                    if (device != null)
                    {
                        throw new Exception("Found more than one device.  Giving up as test results will probably be invalid.");
                    }
                    device = aDevice;
                    device.AddRef();
                    sem.Release();
                },
                                                                               (aDeviceList, aDevice) =>
                {
                    throw new Exception("ERROR: Device removed while test is running.");
                });
                sem.WaitOne();

                // actions
                Console.WriteLine("");
                int[] threadCounts = { 1, 2, 4 };
                foreach (int threadCount in threadCounts)
                {
                    List <Thread> threads = new List <Thread>();
                    for (int i = 0; i < threadCount; i++)
                    {
                        threads.Add(new Thread(ActionThread));
                    }
                    ThreadArgs threadArgs = new ThreadArgs(device, kTestDurationMs);
                    for (int i = 0; i < threadCount; i++)
                    {
                        threads[i].Start(threadArgs);
                    }
                    for (int i = 0; i < threadCount; i++)
                    {
                        threads[i].Join();
                    }
                    Console.WriteLine("Invoked {0} actions in {1}ms using {2} threads", threadArgs.Count, kTestDurationMs, threadCount);
                }

                // subscriptions
                Thread     thread = new Thread(SubscriptionThread);
                ThreadArgs args   = new ThreadArgs(device, kTestDurationMs);
                thread.Start(args);
                thread.Join();
                Console.WriteLine("\nCompleted {0} subscriptions in {1}ms\n", args.Count, kTestDurationMs);

                device.RemoveRef();
                deviceList.Dispose();
                Console.WriteLine("\nTests complete.  Press 'q' to exit.");
                while (Console.ReadKey(true).KeyChar != 'q')
                {
                    ;
                }
            }
        }
Beispiel #4
0
 public void DeviceAdded(CpDevice aDevice)
 {
     string udn = aDevice.Udn();
     Logger.DebugFormat("DeviceAdded: UDN={0}", udn);
     CountedReference<T> newProxyRef = new CountedReference<T>(iProxyConstructor(aDevice));
     aDevice.AddRef();
     ProxyRecord newProxyRecord = new ProxyRecord(aDevice, newProxyRef);
     ProxyRecord oldProxyRecord = null;
     EventHandler<ProxyEventArgs> handler;
     lock (iProxiesByUdn)
     {
         if (iProxiesByUdn.ContainsKey(udn))
         {
             oldProxyRecord = iProxiesByUdn[udn];
         }
         iProxiesByUdn[aDevice.Udn()] = newProxyRecord;
         handler = DeviceDetectedHandler;
         Monitor.PulseAll(iProxiesByUdn);
     }
     if (oldProxyRecord != null)
     {
         oldProxyRecord.InvokeDisappeared();
         oldProxyRecord.Ref.Dispose();
         oldProxyRecord.Device.RemoveRef();
     }
     if (handler != null)
     {
         handler(this, new ProxyEventArgs(aDevice, newProxyRef, newProxyRecord));
     }
 }