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