<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; AHK: Class Timer]]></title>
	<link rel="self" href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=17173&amp;type=atom" />
	<updated>2022-06-11T19:17:20Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.script-coding.com/viewtopic.php?id=17173</id>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Class Timer]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153531#p153531" />
			<content type="html"><![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>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2022-06-11T19:17:20Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153531#p153531</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Class Timer]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153530#p153530" />
			<content type="html"><![CDATA[<p>Я все еще думаю, стоит ли вызывать функцию с добавлением количества. Ибо это может все сломать.</p>]]></content>
			<author>
				<name><![CDATA[Phoenixxx_Czar]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=34426</uri>
			</author>
			<updated>2022-06-11T18:36:23Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153530#p153530</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Class Timer]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153529#p153529" />
			<content type="html"><![CDATA[<p>Проще так:<br /></p><div class="codebox"><pre><code>
			if (this.count == 0)
			{
				SetTimer,, Off ; или Delete
			}</code></pre></div>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2022-06-11T18:31:16Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153529#p153529</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Class Timer]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153528#p153528" />
			<content type="html"><![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>]]></content>
			<author>
				<name><![CDATA[Phoenixxx_Czar]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=34426</uri>
			</author>
			<updated>2022-06-11T18:23:29Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153528#p153528</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Class Timer]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153527#p153527" />
			<content type="html"><![CDATA[<p>Технически можно через название внешнего класса, но с точки зрения архитектуры приложения в этом нет смысла, вложенный класс нужен как вспомогательный для внешнего, но не наоборот.</p>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2022-06-11T18:22:11Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153527#p153527</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Class Timer]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153526#p153526" />
			<content type="html"><![CDATA[<p>А как я могу из вложенного класса обратиться к методу первого класса?</p>]]></content>
			<author>
				<name><![CDATA[Phoenixxx_Czar]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=34426</uri>
			</author>
			<updated>2022-06-11T18:06:54Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153526#p153526</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Class Timer]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153525#p153525" />
			<content type="html"><![CDATA[<p>Вроде того. Извне к нему можно обратиться только через внешний (или через его инстанс).</p>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2022-06-11T17:54:49Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153525#p153525</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Class Timer]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153524#p153524" />
			<content type="html"><![CDATA[<p>То есть.. Вложенный класс будет виден только внутри первого?</p>]]></content>
			<author>
				<name><![CDATA[Phoenixxx_Czar]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=34426</uri>
			</author>
			<updated>2022-06-11T17:49:07Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153524#p153524</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Class Timer]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153523#p153523" />
			<content type="html"><![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>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2022-06-11T16:53:23Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153523#p153523</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Class Timer]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153522#p153522" />
			<content type="html"><![CDATA[<p>Можете привести пример? Я никогда не использовал вложенные классы и почему-то думал, что они как отдельные классы и в целом не понимаю, зачем так делать.</p>]]></content>
			<author>
				<name><![CDATA[Phoenixxx_Czar]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=34426</uri>
			</author>
			<updated>2022-06-11T16:04:52Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153522#p153522</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Class Timer]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153521#p153521" />
			<content type="html"><![CDATA[<p>Можно вложенный класс использовать для функции таймера.</p>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2022-06-11T15:52:47Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153521#p153521</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Class Timer]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153520#p153520" />
			<content type="html"><![CDATA[<p>И как лучше поступить?</p>]]></content>
			<author>
				<name><![CDATA[Phoenixxx_Czar]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=34426</uri>
			</author>
			<updated>2022-06-11T15:47:56Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153520#p153520</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Class Timer]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153519#p153519" />
			<content type="html"><![CDATA[<p>Этот метод срабатывает, когда удаляется последняя ссылка на инстанс, но если внутри класса используется ObjBindMethod(this, ...), то возникает лишняя ссылка внутри класса, и __Delete() не запускается при удалении внешнего инстанса.</p>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2022-06-11T13:22:48Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153519#p153519</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Class Timer]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153518#p153518" />
			<content type="html"><![CDATA[<p>Я уже добавил метод __delete, который остановит таймер. Хотя.. Оно почему-то не работает.<br />Хотя у меня в целом не срабатывает метод __delete.</p>]]></content>
			<author>
				<name><![CDATA[Phoenixxx_Czar]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=34426</uri>
			</author>
			<updated>2022-06-11T10:58:00Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153518#p153518</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Class Timer]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153517#p153517" />
			<content type="html"><![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>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2022-06-11T10:52:19Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153517#p153517</id>
		</entry>
</feed>
