EDN Admin
Well-known member
<p align="left" style="text-align:left <span lang="EN-US Iâve created a library that allows you to stop running the code where you want to wait for an event using the functionarity of yield sentence, which has the ability to stop the execution of the
code in the line where you write âyield returnâ. Iâm not sure if it is good to submit this here, but I posted it and would like to discuss it.
<p align="left" style="text-align:left <span lang="EN-US [Download]
<p align="left" style="text-align:left <span lang="EN-US First of all, you can download the library from the following links:
<p align="left" style="text-align:left http://cid-6d750b1d390978fd.office.live.com/self.aspx/Public/1%20EventWaitingCore.zip" target="_blank http://cid-6d750b1d390978fd.office.live.com/self.aspx/Public/1%20EventWaitingCore.zip
<p align="left" style="text-align:left <span lang="EN-US [How to Use]
<p align="left" style="text-align:left <span lang="EN-US The library itself is just included in the file âWaitCore.csâ (The EventWaitingCore.zip file includes many tests for the details). You can use it like the following codes, which makes it
possible to wait for events by stopping running the code.
<pre style="line-height:13.5pt <span> <span lang="EN-US" style="font-size:9.0pt; color:#ff3300 using SimonPG.WaitCore; <span lang="EN-US" style="font-size:9.0pt; color:#434343 // <span style="font-size:9.0pt; color:#434343 â<span lang="EN-US [A]
<span lang="EN-US" style="font-size:9.0pt; color:#434343 IEnumerable TestFunc(EventWaiter waiter) { // <span style="font-size:9.0pt; color:#434343 â<span lang="EN-US
<span style="font-size:12px; color:#434343 <span> ...Process A...
<span lang="EN-US" style="font-size:9.0pt; color:#434343 <span> <span> <span lang="EN-US" style="font-size:9.0pt; color:#ff3300 yield return <span lang="EN-US" style="font-size:9.0pt; color:#434343 waiter.<span> <span lang="EN-US" style="font-size:9.0pt; color:#ff3300 Wait( <span lang="EN-US" style="font-size:9.0pt; color:#434343 button1, "Click"<span> <span lang="EN-US" style="font-size:9.0pt; color:#ff3300 ) <span lang="EN-US" style="font-size:9.0pt; color:#434343 ; // <span style="font-size:9.0pt; color:#434343 â<span lang="EN-US [C]
<span style="font-size:12px; color:#434343 <span> ...Process B...
<span style="font-size:12px; color:#434343 }
<span style="font-size:12px; color:#434343 void Form_Load(object sender, EventArgs e) {
<span lang="EN-US" style="font-size:9.0pt; color:#434343 <span> new EventWaiter(TestFunc); // <span style="font-size:9.0pt; color:#434343 â<span lang="EN-US [D]
<span style="font-size:12px; color:#434343 }[/code]
<p align="left" style="text-align:left I named the namespace for the library as âSimonPG.WaitCoreâ, so please import it like the line [A]. The main class in the library is EventWaiter. Make an instance of the class with specifying a function in
the argument like the line [D] so that you can wait for events in the specified function. To wait for events, call Wait function from âwaiterâ variable which has EventWaiterâs instance and specify the object and the name of the event that
you want to wait for in the argument like the line [C]. At last return the value of Wait function using yield sentence.
<p align="left" style="text-align:left <span lang="EN-US How the code above behaves is that when the form is loaded, the instantiation of [D] calls TestFunc function internally and runs . There, the code of âProcess Aâ will be executed and
the execution will wait in the line [C] until Click event of âbutton1â is raised. When âbutton1â is clicked, the waiting is completed and the code of âProcess Bâ will be executed, and all the process of the code has done.
<p align="left" style="text-align:left <span lang="EN-US [Useful Case]
<p align="left" style="text-align:left <span lang="EN-US There are many cases in which it is very useful if you can wait for events by stopping running the code. I will show you an example of blinking the backcolor of a label with a timer. If you use EventWaiter
and wait for events, you can write codes in a very nice way without using any state variables. You will see the code gets more readable and you can easily understand the flow of the code at a glance.
<pre style="line-height:13.5pt <span> <span lang="EN-US" style="font-size:9.0pt; color:#ff3300 using SimonPG.WaitCore
<span style="font-size:12px; color:#434343 <span style="color:#ff3300
IEnumerable TestFunc(EventWaiter waiter) {
<span style="font-size:12px; color:#434343 <span> while (true) {
<span style="font-size:12px; color:#434343 <span> for (var bright = 0; bright < 255; bright += 10) {
<span style="font-size:12px; color:#434343 <span> <span> label1.BackColor = Color.FromArgb(
<span style="font-size:12px; color:#434343 <span> bright, bright, bright);
<span lang="EN-US" style="font-size:9.0pt; color:#434343 <span> <span> <span lang="EN-US" style="font-size:9.0pt; color:#ff3300 <span> yield return <span lang="EN-US" style="font-size:9.0pt; color:#434343 waiter<span> <span lang="EN-US" style="font-size:9.0pt; color:#ff3300 .Wait( <span lang="EN-US" style="font-size:9.0pt; color:#434343 timer1, "Tick"<span> <span lang="EN-US" style="font-size:9.0pt; color:#ff3300 ) <span lang="EN-US" style="font-size:9.0pt; color:#434343 ;
<span style="font-size:12px; color:#434343 <span> }
<span style="font-size:12px; color:#434343 <span style="font-size:12px; color:#434343 <span> for (var bright = 255; bright > 0; bright -= 10) {
<span style="font-size:12px; color:#434343 <span> label1.BackColor = Color.FromArgb(
<span style="font-size:12px; color:#434343 <span> bright, bright, bright);
<span lang="EN-US" style="font-size:9.0pt; color:#434343 <span> <span> <span lang="EN-US" style="font-size:9.0pt; color:#ff3300 yield return <span lang="EN-US" style="font-size:9.0pt; color:#434343 waiter<span> <span lang="EN-US" style="font-size:9.0pt; color:#ff3300 .Wait( <span lang="EN-US" style="font-size:9.0pt; color:#434343 timer1, "Tick"<span> <span lang="EN-US" style="font-size:9.0pt; color:#ff3300 ) <span lang="EN-US" style="font-size:9.0pt; color:#434343 ;
<span style="font-size:12px; color:#434343 <span> }
<span style="font-size:12px; color:#434343 <span> }
<span style="font-size:12px; color:#434343 }
<span style="font-size:12px; color:#434343 void Form_Load(object sender, EventArgs e) {
<span style="font-size:12px; color:#434343 <span> new EventWaiter(TestFunc);
<span style="font-size:12px; color:#434343 }[/code]
<p align="left" style="text-align:left [Advanced Functions]
<p align="left" style="text-align:left <span lang="EN-US This library has many other features not only of waiting for events but also of waiting for several events connected with And/Or/Not, waiting until an event raises several times and more. For details,
you can see some test codes in the EventWaitingCore.zip file.
<p align="left" style="text-align:left <span lang="EN-US [Links]
<p align="left" style="text-align:left <span lang="EN-US (Sorry, but these are Japanese only now)<br/>
http://simonpg.web.fc2.com/Pages/labo/idea_p/20091001_UseYieldForWait/index.html" target="_blank http://simonpg.web.fc2.com/Pages/labo/idea_p/20091001_UseYieldForWait/index.html <br/>
http://simonpg.web.fc2.com/Pages/labo/idea_p/20090213_StopCodeWithWaitKeyword/index.html" target="_blank http://simonpg.web.fc2.com/Pages/labo/idea_p/20090213_StopCodeWithWaitKeyword/index.html
View the full article
code in the line where you write âyield returnâ. Iâm not sure if it is good to submit this here, but I posted it and would like to discuss it.
<p align="left" style="text-align:left <span lang="EN-US [Download]
<p align="left" style="text-align:left <span lang="EN-US First of all, you can download the library from the following links:
<p align="left" style="text-align:left http://cid-6d750b1d390978fd.office.live.com/self.aspx/Public/1%20EventWaitingCore.zip" target="_blank http://cid-6d750b1d390978fd.office.live.com/self.aspx/Public/1%20EventWaitingCore.zip
<p align="left" style="text-align:left <span lang="EN-US [How to Use]
<p align="left" style="text-align:left <span lang="EN-US The library itself is just included in the file âWaitCore.csâ (The EventWaitingCore.zip file includes many tests for the details). You can use it like the following codes, which makes it
possible to wait for events by stopping running the code.
<pre style="line-height:13.5pt <span> <span lang="EN-US" style="font-size:9.0pt; color:#ff3300 using SimonPG.WaitCore; <span lang="EN-US" style="font-size:9.0pt; color:#434343 // <span style="font-size:9.0pt; color:#434343 â<span lang="EN-US [A]
<span lang="EN-US" style="font-size:9.0pt; color:#434343 IEnumerable TestFunc(EventWaiter waiter) { // <span style="font-size:9.0pt; color:#434343 â<span lang="EN-US
<span style="font-size:12px; color:#434343 <span> ...Process A...
<span lang="EN-US" style="font-size:9.0pt; color:#434343 <span> <span> <span lang="EN-US" style="font-size:9.0pt; color:#ff3300 yield return <span lang="EN-US" style="font-size:9.0pt; color:#434343 waiter.<span> <span lang="EN-US" style="font-size:9.0pt; color:#ff3300 Wait( <span lang="EN-US" style="font-size:9.0pt; color:#434343 button1, "Click"<span> <span lang="EN-US" style="font-size:9.0pt; color:#ff3300 ) <span lang="EN-US" style="font-size:9.0pt; color:#434343 ; // <span style="font-size:9.0pt; color:#434343 â<span lang="EN-US [C]
<span style="font-size:12px; color:#434343 <span> ...Process B...
<span style="font-size:12px; color:#434343 }
<span style="font-size:12px; color:#434343 void Form_Load(object sender, EventArgs e) {
<span lang="EN-US" style="font-size:9.0pt; color:#434343 <span> new EventWaiter(TestFunc); // <span style="font-size:9.0pt; color:#434343 â<span lang="EN-US [D]
<span style="font-size:12px; color:#434343 }[/code]
<p align="left" style="text-align:left I named the namespace for the library as âSimonPG.WaitCoreâ, so please import it like the line [A]. The main class in the library is EventWaiter. Make an instance of the class with specifying a function in
the argument like the line [D] so that you can wait for events in the specified function. To wait for events, call Wait function from âwaiterâ variable which has EventWaiterâs instance and specify the object and the name of the event that
you want to wait for in the argument like the line [C]. At last return the value of Wait function using yield sentence.
<p align="left" style="text-align:left <span lang="EN-US How the code above behaves is that when the form is loaded, the instantiation of [D] calls TestFunc function internally and runs . There, the code of âProcess Aâ will be executed and
the execution will wait in the line [C] until Click event of âbutton1â is raised. When âbutton1â is clicked, the waiting is completed and the code of âProcess Bâ will be executed, and all the process of the code has done.
<p align="left" style="text-align:left <span lang="EN-US [Useful Case]
<p align="left" style="text-align:left <span lang="EN-US There are many cases in which it is very useful if you can wait for events by stopping running the code. I will show you an example of blinking the backcolor of a label with a timer. If you use EventWaiter
and wait for events, you can write codes in a very nice way without using any state variables. You will see the code gets more readable and you can easily understand the flow of the code at a glance.
<pre style="line-height:13.5pt <span> <span lang="EN-US" style="font-size:9.0pt; color:#ff3300 using SimonPG.WaitCore
<span style="font-size:12px; color:#434343 <span style="color:#ff3300
IEnumerable TestFunc(EventWaiter waiter) {
<span style="font-size:12px; color:#434343 <span> while (true) {
<span style="font-size:12px; color:#434343 <span> for (var bright = 0; bright < 255; bright += 10) {
<span style="font-size:12px; color:#434343 <span> <span> label1.BackColor = Color.FromArgb(
<span style="font-size:12px; color:#434343 <span> bright, bright, bright);
<span lang="EN-US" style="font-size:9.0pt; color:#434343 <span> <span> <span lang="EN-US" style="font-size:9.0pt; color:#ff3300 <span> yield return <span lang="EN-US" style="font-size:9.0pt; color:#434343 waiter<span> <span lang="EN-US" style="font-size:9.0pt; color:#ff3300 .Wait( <span lang="EN-US" style="font-size:9.0pt; color:#434343 timer1, "Tick"<span> <span lang="EN-US" style="font-size:9.0pt; color:#ff3300 ) <span lang="EN-US" style="font-size:9.0pt; color:#434343 ;
<span style="font-size:12px; color:#434343 <span> }
<span style="font-size:12px; color:#434343 <span style="font-size:12px; color:#434343 <span> for (var bright = 255; bright > 0; bright -= 10) {
<span style="font-size:12px; color:#434343 <span> label1.BackColor = Color.FromArgb(
<span style="font-size:12px; color:#434343 <span> bright, bright, bright);
<span lang="EN-US" style="font-size:9.0pt; color:#434343 <span> <span> <span lang="EN-US" style="font-size:9.0pt; color:#ff3300 yield return <span lang="EN-US" style="font-size:9.0pt; color:#434343 waiter<span> <span lang="EN-US" style="font-size:9.0pt; color:#ff3300 .Wait( <span lang="EN-US" style="font-size:9.0pt; color:#434343 timer1, "Tick"<span> <span lang="EN-US" style="font-size:9.0pt; color:#ff3300 ) <span lang="EN-US" style="font-size:9.0pt; color:#434343 ;
<span style="font-size:12px; color:#434343 <span> }
<span style="font-size:12px; color:#434343 <span> }
<span style="font-size:12px; color:#434343 }
<span style="font-size:12px; color:#434343 void Form_Load(object sender, EventArgs e) {
<span style="font-size:12px; color:#434343 <span> new EventWaiter(TestFunc);
<span style="font-size:12px; color:#434343 }[/code]
<p align="left" style="text-align:left [Advanced Functions]
<p align="left" style="text-align:left <span lang="EN-US This library has many other features not only of waiting for events but also of waiting for several events connected with And/Or/Not, waiting until an event raises several times and more. For details,
you can see some test codes in the EventWaitingCore.zip file.
<p align="left" style="text-align:left <span lang="EN-US [Links]
<p align="left" style="text-align:left <span lang="EN-US (Sorry, but these are Japanese only now)<br/>
http://simonpg.web.fc2.com/Pages/labo/idea_p/20091001_UseYieldForWait/index.html" target="_blank http://simonpg.web.fc2.com/Pages/labo/idea_p/20091001_UseYieldForWait/index.html <br/>
http://simonpg.web.fc2.com/Pages/labo/idea_p/20090213_StopCodeWithWaitKeyword/index.html" target="_blank http://simonpg.web.fc2.com/Pages/labo/idea_p/20090213_StopCodeWithWaitKeyword/index.html
View the full article