Wisej 3.5.5. Timer control does not stop when parent control is unloaded.

Answered
0
0

Hi. I’m using Wisej 3.5.5 and C#, Visual Studio 2022. I defined a custom control that have a Wisej.Web.Timer on it,

That control is created dynamically and added on a tab page, and after that the timer is enabled by code and works as expected. When the tag page is closed, the timer still works. I never explicitly dispose my control, I assumed that closing the parent (tab page) will do.

Please advice.

  • You must to post comments
Best Answer
0
0

“created a UserControl (let’s call it UC1) – added a timer on it (via toolbox)”- this is the issue- the timer can’t be added to a specific control. Adding the timer in the designer doesn’t mean that the timer is tied to the UserControl.

Here’s the Timer documentation, you will find this useful: https://docs.wisej.com/api/wisej.web/other-components/wisej.web.timer

I would recommend creating the timer via code when the UserControl is created, ie: Timer timer1 = new Timer();

Dispose of the timer when the UserControl is disposed of. Attach an event handler to the Disposed event of the UserControl (https://docs.wisej.com/api/wisej.web/general/control#disposed). When the event fires, dispose of the timer. The code for this is timer1.Dispose(); (Assuming you used the default name for the timer.)

  • Adrian Zagar
    I understand now my mistake, thank you! My surprise was that my control is not automatically disposed! When I close the TabPage, It seems that the tabpage is simply removed from the tab control TabPages collection, but not destroyed. Is that the normal behavior? I finally use the timer tick itself to verify that UC1.Parent.Parent==null (meaning the tabPage is no longer on a Tab control) and destroyed it directly. Thank you again for your clarifications and I would appreciate your comments on the aspects above.
  • Julie (ITG)
    Yes, that is the normal behavior. Closing like that (clicking on the X to close the TabPage) doesn’t dispose the TabPage, it simply removes it from the TabControl because the app may re-add it. It fires the TabClosed event. If the application wants to dispose the tab it should call Dispose().
  • You must to post comments
0
0

Your code is wrong in some way. A Timer cannot be a child of any control-so that’s why it’s not being disposed of when your custom control is. Use the designer and add a timer from the toolbox, then check the Designer.cs file to see the correct way to add a container for components. You will need to dispose of the timer explicitly via code.

If this does not fully answer your question, please provide a sample, following the guidelines under “Before Posting an Issue”” https://wisej.com/support/question/forum-guidelines and we will take a look at it.

  • Adrian Zagar
    Hi Julie, Thank you for your answer. We agree that my code is wrong in some way. I did the following: – created a UserControl (let’s call it UC1) – added a timer on it (via toolbox) – on a Page I have a tabControl. I created by code a new tab, I created by code a new UC1 and add it on the tab.Controls. – I enabled the timer (by code). When I close the tab, the timer is still executing. Please advise what to change or add in my logic to stop or dispose the timer.
  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.