RemoveRef() public method

Remove a reference to a device.
Removing the final reference causes a device to be deleted.
public RemoveRef ( ) : void
return void
Exemplo n.º 1
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')
                {
                    ;
                }
            }
        }