Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
compcode [2011/01/05 16:26] hourdin fixes in the code, update to new version |
compcode [2011/06/06 10:48] Stéphane Lavirotte [Source Code] |
||
---|---|---|---|
Line 6: | Line 6: | ||
<code csharp> | <code csharp> | ||
- | using WComp.Beans; // Contains the definition of the [Bean] attribute | + | using System; |
using System.Threading; // For the thread demo purposes | using System.Threading; // For the thread demo purposes | ||
+ | using WComp.Beans; | ||
- | namespace Your_Namespace_Name { | + | namespace WComp.Beans |
+ | { | ||
+ | /// <summary> | ||
+ | /// This is a sample bean, using a thread, which has an integer evented property and a method | ||
+ | /// to start the thread. | ||
+ | /// | ||
+ | /// Notes: this bean uses the IThreadCreator interface providing a cleanup method named `Stop()'. | ||
+ | /// Several classes can be defined or used by a Bean, but only the class with the | ||
+ | /// [Bean] attribute will be available in WComp. Its ports will be all public methods, | ||
+ | /// events and properties definied in that class. | ||
+ | /// </summary> | ||
+ | [Bean(Category="MyCategory")] | ||
+ | public class BeanThread1 : IThreadCreator { | ||
+ | private Thread t; // Private attributes of the class | ||
+ | private volatile bool run = false; | ||
+ | private int sleepVal = 1000; | ||
+ | private volatile int eventValue; | ||
+ | |||
+ | public BeanThread1() { | ||
+ | // Put here your init instructions | ||
+ | eventValue = 10; | ||
+ | } | ||
+ | |||
+ | public void Start() { // method starting the thread | ||
+ | if (!run) { | ||
+ | run = true; | ||
+ | t = new Thread(new ThreadStart(ThreadLoopMethod)); | ||
+ | t.Start(); | ||
+ | } | ||
+ | } | ||
+ | public void Stop() { // IThreadCreator defines the Stop() method | ||
+ | run = false; | ||
+ | } | ||
+ | |||
+ | // Loop sample | ||
+ | public void ThreadLoopMethod() { | ||
+ | while(run) { | ||
+ | Thread.Sleep(sleepVal); | ||
+ | // Check if the output is connected | ||
+ | if(Output_Sample != null) | ||
+ | // call the connected methods sequentially | ||
+ | Output_Sample(eventValue); | ||
+ | // and so on... | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // --- Start: Input port sample --- | ||
+ | // an input port is a public method (like below) | ||
+ | public void Input_Sample(int intParam) { | ||
+ | eventValue = intParam; | ||
+ | // No return value is expected in WComp: | ||
+ | // results are given using events | ||
+ | } | ||
+ | // --- End: Input port sample --- | ||
+ | |||
+ | // --- Start: Output port sample --- | ||
+ | public delegate void Output_Sample_Signature(int val); | ||
+ | // The delegate defines the signature of the output method | ||
+ | public event Output_Sample_Signature Output_Sample; | ||
+ | // The output port is the event, named here Output_Sample | ||
+ | // --- End: Output port sample --- | ||
+ | } | ||
+ | } | ||
- | [Bean] | ||
- | public class Your_CSharpBean_Name : IThreadCreator { | ||
- | private Thread t; // Private attributes of the class | ||
- | private volatile bool run = false; | ||
- | |||
- | public Your_CSharpBean_Name() { | ||
- | // Put here your init instructions | ||
- | // Example of a loop that call the output | ||
- | // every second. | ||
- | t = new Thread(new ThreadStart(ThreadLoopMethod)); | ||
- | } | ||
- | |||
- | public void Start() { | ||
- | if (!run) { | ||
- | run = true; | ||
- | t.Start(); | ||
- | } | ||
- | } | ||
- | public void Stop() { // since version 2.4.0.856, | ||
- | run = false; // IThreadCreator defines the Stop() method | ||
- | } | ||
- | |||
- | // Loop sample | ||
- | public void ThreadLoopMethod() { | ||
- | while(run) { | ||
- | Thread.Sleep(1000); | ||
- | double result; | ||
- | // Check if the output is connected | ||
- | if(Output_Sample != null) | ||
- | // call the connected methods sequentially | ||
- | result = Output_Sample(123); | ||
- | // and so on... | ||
- | } | ||
- | } | ||
- | |||
- | // --- Start: Input port sample --- | ||
- | // an input port is a method (below) | ||
- | public void Input_Sample(string param_name) { | ||
- | string pn = param_name; // You may use the parameters | ||
- | // No return value is expected in WComp: | ||
- | // results are given using events | ||
- | } | ||
- | // --- End: Input port sample --- | ||
- | |||
- | // --- Start: Output port sample --- | ||
- | public delegate double Output_Sample_Signature(int param_name); | ||
- | // The delegate defines the signature of the output method | ||
- | public event Output_Sample_Signature Output_Sample; | ||
- | // The output port is the event, named here Output_Sample | ||
- | // --- End: Output port sample --- | ||
- | } | ||
- | } | ||
</code> | </code> | ||
- | |||
- | |||
===== Compilation ===== | ===== Compilation ===== |