Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
compcode [2007/06/12 13:34] – créée lavirotte | compcode [2011/06/06 08:48] (current) – [Source Code] Stéphane Lavirotte | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Sample of a Bean ====== | ||
+ | |||
+ | ===== Source Code ===== | ||
+ | |||
+ | Here is a sample code of a component in C# (save this to a file called Your_CSharpBean_Name.cs) : | ||
+ | |||
<code csharp> | <code csharp> | ||
- | using WComp.Beans; // It contains the definition of the attribute [Bean] | + | using System; |
using System.Threading; | using System.Threading; | ||
- | namespace | + | using WComp.Beans; |
- | [Bean] | + | |
- | public class Your_CSharpBean_Name | + | namespace |
- | public | + | { |
- | // Put here your init instructions | + | /// < |
- | // Example of a loop that call the output | + | /// This is a sample bean, using a thread, which has an integer evented property and a method |
- | // every second. | + | /// to start the thread. |
- | new Thread(new ThreadStart(ThreadLoopMethod)).Start(); | + | /// |
- | } | + | /// Notes: this bean uses the IThreadCreator interface providing a cleanup method named `Stop()' |
- | // Loop sample | + | /// Several classes can be defined or used by a Bean, but only the class with the |
- | public void ThreadLoopMethod() { | + | /// [Bean] attribute will be available in WComp. Its ports will be all public methods, |
- | while(Thread.CurrentThread.IsAlive) { | + | /// |
- | Thread.Sleep(1000); | + | /// </ |
- | double result; | + | [Bean(Category=" |
- | // Check if the output is connected | + | public class BeanThread1 : IThreadCreator |
- | if(Output_Sample != null) | + | private Thread t; // Private attributes of the class |
- | // call the connected methods sequentially | + | private volatile bool run = false; |
- | result = Output_Sample(123); | + | private int sleepVal = 1000; |
- | // and so on... | + | private volatile int eventValue; |
- | } | + | |
- | } | + | public |
- | // --- Start: Input port sample --- | + | // Put here your init instructions |
- | // an input port is a method (below) | + | eventValue = 10; |
- | public | + | } |
- | string pn = param_name; // You may use the parameters | + | |
- | return | + | public void Start() { |
- | } | + | if (!run) { |
- | // --- End: Input port sample --- | + | run = true; |
- | // --- Start: Output port sample --- | + | t = new Thread(new ThreadStart(ThreadLoopMethod)); |
- | public delegate | + | t.Start(); |
- | // This is the signature of the output method | + | } |
- | public event Output_Sample_Signature Output_Sample; | + | } |
- | // The output port is the event (before) | + | public void Stop() { // IThreadCreator defines the Stop() method |
- | // --- End: Output port sample --- | + | 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 | ||
+ | public | ||
+ | eventValue | ||
+ | // No return | ||
+ | // results are given using events | ||
+ | } | ||
+ | // --- End: Input port sample --- | ||
+ | |||
+ | // --- Start: Output port sample --- | ||
+ | public delegate | ||
+ | // The delegate defines | ||
+ | public event Output_Sample_Signature Output_Sample; | ||
+ | // The output port is the event, named here Output_Sample | ||
+ | // --- End: Output port sample --- | ||
+ | } | ||
} | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ===== Compilation ===== | ||
+ | |||
+ | Here is a sample of the command line compilation: | ||
+ | < | ||
+ | csc.exe / | ||
</ | </ | ||
+ | |||
+ | You will find Beans.dll in the SharpWComp distrib. |