From: Michaela Brauner on
Hello,
> Do you mean example code for design patterns? If so, look at this site:
> http://www.dofactory.com/Patterns/Patterns.aspx.
thanks.

Overview
http://www1.minpic.de/bild_anzeigen.php?id=110654&key=78643808&ende

Why do I can the not defined the delegate 'EventHandlerScannerVerify'in
my concrete scanner-class A?
What is the reason?
How looks the solution?


Thanks.
Greeting Michaela




Is state

Factory with n-Scanner for selection.
Idee:
Factory
ScannerProducts
Interface --- Event, Delegate
Basis class
Scannertype class fix

Interface:
public delegate void EventHandlerScannerVerify(Object sender,
NotifyEventArgsHardware e);
public interface IScannerVerify
{
event EventHandlerScannerVerify EvHaScannerVerify;
int ComPortOpen();
void ComPortClose();
string Read();
void TriggerStart(string sequence);
}
Base class:
public abstract class BaseScannerVerify : IScannerVerify
{
protected string ReadCode { get; set; }
public event EventHandlerScannerVerify EvHaScannerVerify;

public virtual int ComPortOpen()
{
return 0;
}

public virtual void ComPortClose()
{
}

public virtual string Read()
{
return ReadCode;
}

public virtual void TriggerStart(string sequence)
{
}

// public virtual void EvHaScannerVerifyFunctionAsynchron(string
data)
public void EvHaScannerVerifyFunctionAsynchron(string data)
{
if ( this.EvHaScannerVerify != null )
{
this.EvHaScannerVerify(this, new
NotifyEventArgsHardware(data));
}
}
}
Konkrete Scannerklasse
public class ScannerVerifyTypeA : BaseScannerVerify
{
//private string ReadCode { get; set; }
//public event EventHandlerScannerVerify EvHaScannerVerify;

public override int ComPortOpen()
{
return 0;
}

public override void ComPortClose()
{
}

public override string Read()
{
ReadCode = "2010-04-29-TestCode--A";
EvHaScannerVerifyFunctionAsynchron(ReadCode);
return ReadCode;
}

public override void TriggerStart(string sequence)
{
}

// // public override void
EvHaScannerVerifyFunctionAsynchron(string data)
// public void EvHaScannerVerifyFunctionAsynchron(string data)
// {
// if ( this.EvHaScannerVerify != null )
// {
// this.EvHaScannerVerify(this, new
NotifyEventArgsHardware(data));
// }
// }
}
From: Michaela Brauner on
Hello,

the problem ---

Error 1 The event
'Definitions.Core.BaseScannerVerify.EvHaScannerVerify' can only appear
on the left hand side of += or -=
C:\Hardware.Implementations.ScannerVerify.ScannerA\Core\ScannerVerifyTypeA.cs
38 22 Hardware.Implementations.ScannerVerify.ScannerA


http://www1.minpic.de/bild_anzeigen.php?id=110661&key=11543658&ende


Hope you can see the picture.

Greeting Michaela
From: Peter Duniho on
Michaela Brauner wrote:
> Hello,
>> Do you mean example code for design patterns? If so, look at this
>> site: http://www.dofactory.com/Patterns/Patterns.aspx.
> thanks.
>
> Overview
> http://www1.minpic.de/bild_anzeigen.php?id=110654&key=78643808&ende
>
> Why do I can the not defined the delegate 'EventHandlerScannerVerify'in
> my concrete scanner-class A?

You can only access as a field the event you've declared from within the
class that declares it. Any other class, including sub-classes, see
only the event itself, which has only add and remove accessors, which
are used using "+=" or "-=" only.

If you want the sub-class to be able to raise the event, you need to
provide a method in the base class for that purpose. A common .NET
pattern is to have an "On�" method that does that. But you've already
got a method in your base class that should work for that purposes:
"EvHaScannerVerifyFunctionAsynchron". Just call that from the base class.

Pete
From: Michaela Brauner on
Hello Peter,

I make now this way.
If you want, you can say good, bad or make improvements. Thanks.


http://www1.minpic.de/bild_anzeigen.php?id=110849&key=21919068&ende

Interface:

namespace Brauner.Hardware.Definitions.Core
{
public delegate void EventHandlerScannerVerify(Object sender,
NotifyEventArgsHardware e);

public enum ScannerState
{
NotDefine = -1,
Good = 0,
Bad = 1
}
public interface IScannerVerify
{
// Return always the last scanned code.
string LastCode { get; set; }
// Initialize (setup) scanner.
void Initialize();
// Start asynch operation.
void Scan();
// Return current state.
ScannerState State { get; set; }
// Stop asynch operation.
void Stop();


// Event raised when new barcode is scanned.
//event EventHandler Scanned; // standard
event EventHandlerScannerVerify Scanned; // overwrite with a
delegate
}
}

Base class
namespace Brauner.Hardware.Definitions.Core
{
public abstract class BaseScannerVerify : IScannerVerify
{
// public event EventHandler Scanned; standard
//public virtual event EventHandlerScannerVerify Scanned; //
overwrite with the own handler, delegate
public virtual event EventHandlerScannerVerify Scanned; //
overwrite with the own handler, delegate

public virtual void Scan()
{
}

//public virtual void OnScanned(string data)
public virtual void OnScanned(string data)
{
if (this.Scanned != null)
{
this.Scanned(this, new NotifyEventArgsHardware(data));
}
}

// Return always the last scanned code.
public string LastCode { get; set; }

// Initialize (setup) scanner.
public virtual void Initialize()
{
}

// Return current state.
public virtual ScannerState State { get; set; }

// Stop asynch operation.
public virtual void Stop()
{
}
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Brauner.Hardware.Definitions.Core;
using System.ComponentModel;



namespace Brauner.Hardware.Implementations.ScannerVerify.ScannerA.Core
{
public class ScannerVerifyTypeA : BaseScannerVerify
{
public override event EventHandlerScannerVerify Scanned; //
overwrite with the own handler,

delegate

public override void Scan()
{
BackgroundWorkerScannerTypeA bw = new
BackgroundWorkerScannerTypeA();
bw.RunAsync(new

RunWorkerCompletedEventHandler(RunAsyncBackgroundWorkerCompleted_Scan));
}

private void RunAsyncBackgroundWorkerCompleted_Scan(object sender,

RunWorkerCompletedEventArgs e)
{
BackgroundWorkerScannerTypeA bwSender =
(BackgroundWorkerScannerTypeA)sender;

OnScanned(bwSender.Result);
}


public override void OnScanned(string data)
{
if (this.Scanned != null)
{
data += " noch Zusatz, deshalb override speziell f�r A";

this.Scanned(this, new NotifyEventArgsHardware(data));
}
}

// Initialize (setup) scanner.
public override void Initialize()
{
}

// Return current state.
public override ScannerState State { get; set; }

// Stop asynch operation.
public override void Stop()
{
}
}

class BackgroundWorkerScannerTypeA :
System.ComponentModel.BackgroundWorker
{
// object

// in parameters

// out parameters
public string Result { set; get; }

// start function
public void RunAsync(RunWorkerCompletedEventHandler completed)
{
this.DoWork += new DoWorkEventHandler(ThreadProc);
this.RunWorkerCompleted += completed;
this.RunWorkerAsync();
}

// thread proc
private void ThreadProc(object sender, DoWorkEventArgs e)
{
BackgroundWorkerScannerTypeA bw =
(BackgroundWorkerScannerTypeA)sender;
System.Diagnostics.Debug.Assert(this == bw);

System.DateTime dtStart = System.DateTime.Now;

// ** ToDo implement - Serial Port, Scanner DataReceived
System.Threading.Thread.Sleep(8000);

bw.Result = "Hallo �ber BackgroundThread";

System.DateTime dtEnd = System.DateTime.Now;
System.TimeSpan tsDuration = dtEnd.Subtract(dtStart);
double dDuration = tsDuration.TotalMilliseconds;
System.Diagnostics.Trace.TraceInformation("GetScanCode
duration={0} ms", dDuration);
e.Cancel = this.CancellationPending;
}
}
}
From: Peter Duniho on
Michaela Brauner wrote:
> Hello Peter,
>
> I make now this way.
> If you want, you can say good, bad or make improvements. Thanks.

Some broad observations:

� ScannerState: unless your enum is required to have specific values
for the purpose of interoperation with some other component (managed or
unmanaged) that you don't control, there really is not any point in
providing explicit values for each enumeration value.

� BaseScannerVerify: there are no abstract members of this abstract
class. Thus, there is not any point in making the class abstract. If
there are members of the class that you want to require sub-classes to
override, then those members should not be implemented in the base
class. Make them abstract instead.

� ScannerVerifyTypeA: currently, the base class provides default
implementations for every member. There is no point in the sub-class
overriding the base class implementations for members for which it does
not change the implementation. This includes the "Initialize()" and
"Stop()" methods, and the "State" property.

� BackgroundWorkerScannerTypeA: there is almost never any point in
overriding BackgroundWorker, and definitely in your example there's
really no obvious benefit to doing so. Even if you wanted a convenience
method such as the "RunAsync()" method, there's nothing about that
method that suggests it should actually be _in_ an instance of
BackgroundWorker.

� BackgroundWorkerScannerTypeA: your DoWork event handler sets the
Cancel property at the end based on the CancellationPending. But, the
worker object hasn't had its WorkerSupportsCancellation property set to
true. Even if it had, the Cancel property should be set _only_ if the
DoWork event handler was in fact actually cancelled. If it runs to
completion, then in spite of some client code trying to cancel the
worker (resulting in the CancellationPending property getting set), by
definition the worker was not in fact cancelled, and should not report
that it was.

Pete