# Reusing Timers, Serials & TCPSockets

**URL:** <https://forum.xojo.com/t/reusing-timers-serials-tcpsockets/19734>\
**Category:** General\
**Created:** [October 11, 2014, 3:23am UTC](https://forum.xojo.com/t/reusing-timers-serials-tcpsockets/19734 "2014-10-11T03:23:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dean\_Davidge](https://forum.xojo.com/user_avatar/forum.xojo.com/dean_davidge/32/244_2.png) [@Dean\_Davidge](https://forum.xojo.com/u/Dean_Davidge)\
**Post date:** [October 11, 2014, 3:23am UTC](https://forum.xojo.com/t/reusing-timers-serials-tcpsockets/19734/1 "2014-10-11T03:23:17Z")

</div>

I use several timers as well as a serial control and a TCP socket in a service app that is designed to run continuously and may connect to different IP addresses or serial ports. I know that if this were a desktop app, I could place them on the desktop as controls and I would only have one instance of each one. Both the TCPSocket and the Serial control are subclassed and I create an instance of one or the other depending on what is specified in the database for the device to communicate with.

In creating a watchdog timer to clean up things if the system were to hang for some unknown reason outside my control, it occurred to me that using a single instance might be a better approach. Some of my timers are instantiated when execution starts and some get a new instance whenever one is needed. Is one way better than the other? If I use a new instance each time, do I need to set the old one to NIL first?

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [October 11, 2014, 3:49am UTC](https://forum.xojo.com/t/reusing-timers-serials-tcpsockets/19734/2 "2014-10-11T03:49:07Z")

</div>

> [@134713:@Dean Davidge](#):
>
> do I need to set the old one to NIL first?

No. And I don’t think there’s a huge difference in creating a new one vs. reusing an old Timer. If you’re talking about the sockets, then there may be an advantage to creating a new one, as you get a pristine instance each time and don’t have to worry about any settings being carried forward.

---

<div class="post-metadata">

**Author:** ![Dean\_Davidge](https://forum.xojo.com/user_avatar/forum.xojo.com/dean_davidge/32/244_2.png) [@Dean\_Davidge](https://forum.xojo.com/u/Dean_Davidge)\
**Post date:** [October 12, 2014, 3:07am UTC](https://forum.xojo.com/t/reusing-timers-serials-tcpsockets/19734/3 "2014-10-12T03:07:11Z")

</div>

Thanks Tim

---

<div class="post-metadata">

**Author:** ![scott\_boss](https://forum.xojo.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@scott\_boss](https://forum.xojo.com/u/scott_boss)\
**Post date:** [October 13, 2014, 1:30pm UTC](https://forum.xojo.com/t/reusing-timers-serials-tcpsockets/19734/4 "2014-10-13T13:30:44Z")

</div>

I use timers/threads for a single task each for each instance of them. I am not worried about the few microseconds it takes to make one. Generally it is so few that I dont notice them in the profiler.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 5:27pm UTC](https://forum.xojo.com/t/reusing-timers-serials-tcpsockets/19734/5 "2020-10-29T17:27:02Z")

</div>


