<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Серый форум &mdash; AHK: Class Timer]]></title>
		<link>https://forum.script-coding.com/viewtopic.php?id=17173</link>
		<atom:link href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=17173&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[Недавние сообщения в теме «AHK: Class Timer».]]></description>
		<lastBuildDate>Sat, 11 Jun 2022 19:17:20 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: AHK: Class Timer]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=153531#p153531</link>
			<description><![CDATA[<p>Думаю, не так всё сложно:<br /></p><div class="codebox"><pre><code>#Persistent
MyTimer := new Timer(&quot;Counter&quot;)
MyTimer.Set(&quot;500&quot;, 3)

Counter() {
   static i := 0
   ToolTip % ++i
}

class Timer
{
   __New(callBack) {
      this.Events.callBack := callBack
      this.timer := ObjBindMethod(this.Events, &quot;OnTimer&quot;)
   }
   Set(value, count := &quot;&quot;) {
      this.Events.count := count
      timer := this.timer
      SetTimer, % timer, % value
   }
   Stop() {
      this.Events.count := &quot;&quot;
      timer := this.timer
      SetTimer, % timer, Off
   }
   class Events {
      OnTimer() {
         callBack := this.callBack
         %callBack%()
         if (this.count &amp;&amp; !--this.count)
            SetTimer,, Delete
      }
   }
   __Delete() {
      this.Events.count := &quot;&quot;
      timer := this.timer
      SetTimer, % timer, Delete
   }
}</code></pre></div><p>Как видите, вложенному классу не приходится вызывать методы из внешнего.</p>]]></description>
			<author><![CDATA[null@example.com (teadrinker)]]></author>
			<pubDate>Sat, 11 Jun 2022 19:17:20 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=153531#p153531</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Class Timer]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=153530#p153530</link>
			<description><![CDATA[<p>Я все еще думаю, стоит ли вызывать функцию с добавлением количества. Ибо это может все сломать.</p>]]></description>
			<author><![CDATA[null@example.com (Phoenixxx_Czar)]]></author>
			<pubDate>Sat, 11 Jun 2022 18:36:23 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=153530#p153530</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Class Timer]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=153529#p153529</link>
			<description><![CDATA[<p>Проще так:<br /></p><div class="codebox"><pre><code>
			if (this.count == 0)
			{
				SetTimer,, Off ; или Delete
			}</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (teadrinker)]]></author>
			<pubDate>Sat, 11 Jun 2022 18:31:16 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=153529#p153529</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Class Timer]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=153528#p153528</link>
			<description><![CDATA[<p>Я решил это так:<br /></p><div class="codebox"><pre><code>class Timer
{
	__new(handler, timeout, count := &quot;infinity&quot;, callOnInit := false)
	{
		this.Events.stop := this.stop

		this.handler := (IsObject(handler) ? handler : func(handler))
		this.timeout := timeout
		this.count   := count

		if (this.timeout &lt; 0)
		{
			this.count := 1
		}

		funcObj := ObjBindMethod(this.Events, &quot;onTimer&quot;)
		this.funcObj := funcObj

		if (callOnInit)
		{
			this.Events.onTimer()
		}

		this.start()
	}

	__delete()
	{
		funcObj := this.funcObj

		setTimer, % funcObj, delete
	}

	stop()
	{
		funcObj := this.funcObj

		if (funcObj)
		{
			setTimer, % funcObj, off
		}
	}

	start()
	{
		funcObj := this.funcObj

		if (funcObj &amp;&amp; (this.count == &quot;infinity&quot; || this.count &gt; 0))
		{
			setTimer, % funcObj, % this.timeout
		}
	}

	class Events
	{
		onTimer()
		{
			if (this.count != &quot;infinity&quot;)
			{
				this.count -= 1
			}

			this.handler.call(this.count)

			if (this.count == 0)
			{
				this.stop()
			}
		}
	}

	count[]
	{
		get
		{
			return this.Events.count
		}

		set
		{
			this.Events.count := value

			return this.Events.count
		}
	}

	handler[]
	{
		get
		{
			return this.Events.handler
		}

		set
		{
			this.Events.handler := value

			return this.Events.handler
		}
	}
}
</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (Phoenixxx_Czar)]]></author>
			<pubDate>Sat, 11 Jun 2022 18:23:29 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=153528#p153528</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Class Timer]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=153527#p153527</link>
			<description><![CDATA[<p>Технически можно через название внешнего класса, но с точки зрения архитектуры приложения в этом нет смысла, вложенный класс нужен как вспомогательный для внешнего, но не наоборот.</p>]]></description>
			<author><![CDATA[null@example.com (teadrinker)]]></author>
			<pubDate>Sat, 11 Jun 2022 18:22:11 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=153527#p153527</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Class Timer]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=153526#p153526</link>
			<description><![CDATA[<p>А как я могу из вложенного класса обратиться к методу первого класса?</p>]]></description>
			<author><![CDATA[null@example.com (Phoenixxx_Czar)]]></author>
			<pubDate>Sat, 11 Jun 2022 18:06:54 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=153526#p153526</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Class Timer]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=153525#p153525</link>
			<description><![CDATA[<p>Вроде того. Извне к нему можно обратиться только через внешний (или через его инстанс).</p>]]></description>
			<author><![CDATA[null@example.com (teadrinker)]]></author>
			<pubDate>Sat, 11 Jun 2022 17:54:49 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=153525#p153525</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Class Timer]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=153524#p153524</link>
			<description><![CDATA[<p>То есть.. Вложенный класс будет виден только внутри первого?</p>]]></description>
			<author><![CDATA[null@example.com (Phoenixxx_Czar)]]></author>
			<pubDate>Sat, 11 Jun 2022 17:49:07 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=153524#p153524</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Class Timer]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=153523#p153523</link>
			<description><![CDATA[<p>Вложенные классы мало чем отличаются от обычных, можно и обычный использовать. Но тогда получится, что функционал будет разнесён на два отдельных класса, причём второй нужен только для использования из первого. Так что просто логичнее упаковать один в другой.<br /></p><div class="codebox"><pre><code>MyTimer := new Timer(&quot;Counter&quot;)
MyTimer.Set(100)
Sleep, 1000
MyTimer := &quot;&quot;
Sleep, 1000
Return

Counter() {
   static i := 0
   ToolTip % ++i
}

class Timer
{
   __New(callBack) {
      this.Events.callBack := callBack
      this.timer := ObjBindMethod(this.Events, &quot;OnTimer&quot;)
   }
   Set(value) {
      timer := this.timer
      SetTimer, % timer, % value
   }
   Stop() {
      timer := this.timer
      SetTimer, % timer, Off
   }
   class Events {
      OnTimer() {
         callBack := this.callBack
         %callBack%()
      }
   }
   __Delete() {
      timer := this.timer
      SetTimer, % timer, Delete
   }
}</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (teadrinker)]]></author>
			<pubDate>Sat, 11 Jun 2022 16:53:23 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=153523#p153523</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Class Timer]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=153522#p153522</link>
			<description><![CDATA[<p>Можете привести пример? Я никогда не использовал вложенные классы и почему-то думал, что они как отдельные классы и в целом не понимаю, зачем так делать.</p>]]></description>
			<author><![CDATA[null@example.com (Phoenixxx_Czar)]]></author>
			<pubDate>Sat, 11 Jun 2022 16:04:52 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=153522#p153522</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Class Timer]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=153521#p153521</link>
			<description><![CDATA[<p>Можно вложенный класс использовать для функции таймера.</p>]]></description>
			<author><![CDATA[null@example.com (teadrinker)]]></author>
			<pubDate>Sat, 11 Jun 2022 15:52:47 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=153521#p153521</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Class Timer]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=153520#p153520</link>
			<description><![CDATA[<p>И как лучше поступить?</p>]]></description>
			<author><![CDATA[null@example.com (Phoenixxx_Czar)]]></author>
			<pubDate>Sat, 11 Jun 2022 15:47:56 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=153520#p153520</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Class Timer]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=153519#p153519</link>
			<description><![CDATA[<p>Этот метод срабатывает, когда удаляется последняя ссылка на инстанс, но если внутри класса используется ObjBindMethod(this, ...), то возникает лишняя ссылка внутри класса, и __Delete() не запускается при удалении внешнего инстанса.</p>]]></description>
			<author><![CDATA[null@example.com (teadrinker)]]></author>
			<pubDate>Sat, 11 Jun 2022 13:22:48 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=153519#p153519</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Class Timer]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=153518#p153518</link>
			<description><![CDATA[<p>Я уже добавил метод __delete, который остановит таймер. Хотя.. Оно почему-то не работает.<br />Хотя у меня в целом не срабатывает метод __delete.</p>]]></description>
			<author><![CDATA[null@example.com (Phoenixxx_Czar)]]></author>
			<pubDate>Sat, 11 Jun 2022 10:58:00 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=153518#p153518</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Class Timer]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=153517#p153517</link>
			<description><![CDATA[<p>По-моему, главная проблема такого дизайна в том, что при удалении инстанса класса таймер будет продолжать вызываться.<br /></p><div class="codebox"><pre><code>tm := new Timer(&quot;MyFunc&quot;, 1000)
Sleep, 1000
tm := &quot;&quot;

MyFunc() {
   SoundBeep
}</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (teadrinker)]]></author>
			<pubDate>Sat, 11 Jun 2022 10:52:19 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=153517#p153517</guid>
		</item>
	</channel>
</rss>
