Author Topic: Unsubscribing from a subscription on form close  (Read 1127 times)

phuz

  • Newbie
  • *
  • Posts: 7
    • View Profile
Unsubscribing from a subscription on form close
« on: May 10, 2020, 11:32:16 AM »
I couldn't find a relevant thread on this, so if it does exist, my apologies.

I understand that unsubscribing on close is a form of good housekeeping, but is it necessary?  If so, must you loop through each unique ID or is there a method to just dispose of all active subscriptions?

I mean, sure it's very simple...just wondering how necessary it is.

Code: [Select]
        While subscriptionID > 0
            mainplc.UnSubscribe(subscriptionID)
            subscriptionID = subscriptionID - 1
        End While
« Last Edit: May 10, 2020, 11:34:50 AM by phuz »

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5320
    • View Profile
    • AdvancedHMI
Re: Unsubscribing from a subscription on form close
« Reply #1 on: May 10, 2020, 11:50:10 AM »
If a form is closed with a driver instance on it, all of the subscriptions are terminated automatically. This is because subscriptions live in the driver instance and not in the backend DLL. So closing a form disposes the driver instance, which in turn deletes all subscriptions

phuz

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Unsubscribing from a subscription on form close
« Reply #2 on: May 11, 2020, 01:34:29 PM »
I assume the same is true if driver is initialized in a service application instead of a form?

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5320
    • View Profile
    • AdvancedHMI
Re: Unsubscribing from a subscription on form close
« Reply #3 on: May 11, 2020, 01:36:59 PM »
If you are doing all code, it is up to you to properly handle disposing of driver instances. Otherwise the subscription threads will continue to run.

phuz

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Unsubscribing from a subscription on form close
« Reply #4 on: May 12, 2020, 06:26:40 AM »
Would it be sufficient to say "EthernetCLXDriver.DisableSubscriptions = True" on form close and call it a day?

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5320
    • View Profile
    • AdvancedHMI
Re: Unsubscribing from a subscription on form close
« Reply #5 on: May 12, 2020, 07:16:38 AM »
DisableSubcriptions only pauses the update process. It does not shut down the subscriptions threads

phuz

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Unsubscribing from a subscription on form close
« Reply #6 on: May 14, 2020, 12:40:00 PM »
Is there a way to get a list of all active subscriptions?

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5320
    • View Profile
    • AdvancedHMI
Re: Unsubscribing from a subscription on form close
« Reply #7 on: May 17, 2020, 08:55:54 AM »
I don't think there is a way to expose the list of active subscriptions. It is really up to you to keep a list of the subscription IDs.

If you want to unsubscribe from them all, a hack you can use is to create a new subscription to get the latest ID. The use a loop from 0 to the latest ID to call Unsubscribe on each ID