Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Last revision Both sides next revision | ||
compcode [2007/06/13 07:40] lavirotte |
compcode [2011/01/05 16:33] hourdin update again |
||
---|---|---|---|
Line 3: | Line 3: | ||
===== Source Code ===== | ===== Source Code ===== | ||
- | Here is the sample code of a component in c# (save this to a le called Sample_Component.cs) : | + | 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 WComp.Beans; // Contains the definition of the [Bean] attribute |
using System.Threading; // For the thread demo purposes | using System.Threading; // For the thread demo purposes | ||
Line 12: | Line 12: | ||
[Bean] | [Bean] | ||
- | public class Your_CSharpBean_Name { | + | public class Your_CSharpBean_Name : IThreadCreator { |
+ | private Thread t; // Private attributes of the class | ||
+ | private volatile bool run; | ||
+ | private volatile int eventValue; | ||
public Your_CSharpBean_Name() { | public Your_CSharpBean_Name() { | ||
// Put here your init instructions | // Put here your init instructions | ||
- | // Example of a loop that call the output | + | t = new Thread(new ThreadStart(ThreadLoopMethod)); |
- | // every second. | + | run = false; |
- | new Thread(new ThreadStart(ThreadLoopMethod)).Start(); | + | eventValue = 10; |
} | } | ||
+ | |||
+ | public void Start() { // method starting the thread | ||
+ | if (!run) { | ||
+ | run = true; | ||
+ | t.Start(); | ||
+ | } | ||
+ | } | ||
+ | public void Stop() { // since version 2.4.0.856, | ||
+ | run = false; // IThreadCreator defines the Stop() method | ||
+ | } | ||
// Loop sample | // Loop sample | ||
public void ThreadLoopMethod() { | public void ThreadLoopMethod() { | ||
- | while(Thread.CurrentThread.IsAlive) { | + | while(run) { |
Thread.Sleep(1000); | Thread.Sleep(1000); | ||
- | double result; | ||
// Check if the output is connected | // Check if the output is connected | ||
if(Output_Sample != null) | if(Output_Sample != null) | ||
// call the connected methods sequentially | // call the connected methods sequentially | ||
- | result = Output_Sample(123); | + | Output_Sample(eventValue); |
// and so on... | // and so on... | ||
} | } | ||
Line 35: | Line 47: | ||
// --- Start: Input port sample --- | // --- Start: Input port sample --- | ||
- | // an input port is a method (below) | + | // an input port is a public method (like below) |
- | public int Input_Sample(string param_name) { | + | public void Input_Sample(int intParam) { |
- | string pn = param_name; // You may use the parameters | + | eventValue = intParam; |
- | return 0; // You may return anything | + | // No return value is expected in WComp: |
+ | // results are given using events | ||
} | } | ||
// --- End: Input port sample --- | // --- End: Input port sample --- | ||
// --- Start: Output port sample --- | // --- Start: Output port sample --- | ||
- | public delegate double Output_Sample_Signature(int param_name); | + | public delegate void Output_Sample_Signature(int val); |
- | // This is the signature of the output method | + | // The delegate defines the signature of the output method |
public event Output_Sample_Signature Output_Sample; | public event Output_Sample_Signature Output_Sample; | ||
- | // The output port is the event (before) | + | // The output port is the event, named here Output_Sample |
// --- End: Output port sample --- | // --- End: Output port sample --- | ||
} | } | ||
Line 56: | Line 69: | ||
===== Compilation ===== | ===== Compilation ===== | ||
- | Here is a sample of the command line compilation : | + | Here is a sample of the command line compilation: |
<code> | <code> | ||
- | csc.exe /target:library /r:Bean.dll Sample_Component.cs | + | csc.exe /target:library /r:Beans.dll Sample_Component.cs |
</code> | </code> | ||
- | You will find Bean.dll in the WComp distrib. | + | You will find Beans.dll in the SharpWComp distrib. |