LCM.LCM.LCM.Unsubscribe C# (CSharp) Method

Unsubscribe() public method

Remove this particular regex/subscriber pair (UNTESTED AND API MAY CHANGE). If regex is null, all subscriptions for 'sub' are cancelled. If subscriber is null, any previous subscriptions matching the regular expression will be cancelled. If both 'sub' and 'regex' are null, all subscriptions will be cancelled.
public Unsubscribe ( string regex, LCMSubscriber sub ) : void
regex string regular expression determining the channels to unsubscribe
sub LCMSubscriber unsubscribing object
return void
		public void Unsubscribe(string regex, LCMSubscriber sub)
		{
            if (this.closed)
            {
                throw new SystemException();
            }
			
            lock (this)
            {
                foreach (Provider p in providers)
                {
                    p.Unsubscribe(regex);
                }
            }

			// TODO: need providers to unsubscribe?
			// TODO: providers don't seem to use anything beyond first channel
			
			lock (subscriptions)
			{	
				// Find and remove subscriber from list
                foreach (SubscriptionRecord sr in subscriptions.ToArray())
                {
                    if ((sub == null || sr.lcsub == sub) && (regex == null || sr.regex.Equals(regex)))
                    {
                        subscriptions.Remove(sr);
                    }
                }

                // Find and remove subscriber from map
                List<SubscriptionRecord> srecs;
	            foreach (string channel in subscriptionsMap.Keys)
                {
                    if (subscriptionsMap.TryGetValue(channel, out srecs))
                    {
                        foreach (SubscriptionRecord sr in srecs.ToArray())
                        {
		                    if ((sub == null || sr.lcsub == sub) && (regex == null || sr.regex.Equals(regex)))
                            {
                                srecs.Remove(sr);
		                    }
                        }
                    }
	            }
            }
		}