PC Review


Reply
Thread Tools Rate Thread

c# event handler fires an event

 
 
Dan Bass
Guest
Posts: n/a
 
      8th Dec 2004
I'm writing a scanner application (C# / .Net CF) where I have one scanner
object (singleton) that hooks into the scan engine drivers.

There is scanner event for a successful scan, that passes through the
scanned data to an event handler. From here I need to pass this information
back to my form, so what I've done is to create another event handler in the
form, which is fired from the event handler in my scanner class.

The Form declaration of this object looks like this:

Is this ok?
Is this considered good practice or is there a blatantly obvious and much
better way of doing this?

Below is the code that's relevant:

// ----------------------------------------------------------
//
// myForm snippet
//

ScanEngine scanner;

private void myForm_Load(object sender, System.EventArgs e)
{
scanner = new ScanEngine();
scanner.StartScan();
scanner.Scanned += new EventHandler(BarcodeRead);
}

private void FormPut_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
if ( scanner != null )
{
scanner.StopScan();
scanner.Scanned -= new EventHandler(BarcodeRead);
scanner = null;
}
}

void BarcodeRead(object sender, System.EventArgs e)
{
if ( scanner != null )
{
string barData = scanner.BarcodeData;

// do stuff here

}
}

// ----------------------------------------------------------
//
// ScanEngine snippet
//


private string barData = "";
public event System.EventHandler Scanned;

// called when a scan occurs
protected virtual void OnScanned(EventArgs e)
{
if (Scanned != null)
Scanned(this, e);
}

public string BarcodeData
{
get
{
return barData;
}
}




 
Reply With Quote
 
 
 
 
=?Utf-8?B?QWxleCBZYWtobmluIFtNVlBd?=
Guest
Posts: n/a
 
      8th Dec 2004
You should declare your own delegate type, something like that:

public delegate void ScannedEventHandler(object sender, string barcodeData);

and then in your ScanEngine class you'd declare your event as

public event ScannedEventHandler Scanned;

HTH... Alex

--
Alex Yakhnin, .NET CF MVP
www.intelliprog.com
www.opennetcf.org


"Dan Bass" wrote:

> I'm writing a scanner application (C# / .Net CF) where I have one scanner
> object (singleton) that hooks into the scan engine drivers.
>
> There is scanner event for a successful scan, that passes through the
> scanned data to an event handler. From here I need to pass this information
> back to my form, so what I've done is to create another event handler in the
> form, which is fired from the event handler in my scanner class.
>
> The Form declaration of this object looks like this:
>
> Is this ok?
> Is this considered good practice or is there a blatantly obvious and much
> better way of doing this?
>
> Below is the code that's relevant:
>
> // ----------------------------------------------------------
> //
> // myForm snippet
> //
>
> ScanEngine scanner;
>
> private void myForm_Load(object sender, System.EventArgs e)
> {
> scanner = new ScanEngine();
> scanner.StartScan();
> scanner.Scanned += new EventHandler(BarcodeRead);
> }
>
> private void FormPut_Closing(object sender,
> System.ComponentModel.CancelEventArgs e)
> {
> if ( scanner != null )
> {
> scanner.StopScan();
> scanner.Scanned -= new EventHandler(BarcodeRead);
> scanner = null;
> }
> }
>
> void BarcodeRead(object sender, System.EventArgs e)
> {
> if ( scanner != null )
> {
> string barData = scanner.BarcodeData;
>
> // do stuff here
>
> }
> }
>
> // ----------------------------------------------------------
> //
> // ScanEngine snippet
> //
>
>
> private string barData = "";
> public event System.EventHandler Scanned;
>
> // called when a scan occurs
> protected virtual void OnScanned(EventArgs e)
> {
> if (Scanned != null)
> Scanned(this, e);
> }
>
> public string BarcodeData
> {
> get
> {
> return barData;
> }
> }
>
>
>
>
>

 
Reply With Quote
 
Stelrad Doulton
Guest
Posts: n/a
 
      8th Dec 2004
Expanding on this deriving from System.EventArgs will give you the
opportunity to store the scanned data in the event argument thus:

public class ScannerArgs : System.EventArgs

{

public readonly string Data;

public ResponseArgs(string data)

{

this.data = data;

}

}

public delegate void ScannedEventHandler(object sender, ScannerArgs scan);

so now in your event hander you can access the data via the event object
using e.Data

"Alex Yakhnin [MVP]" <(E-Mail Removed)> wrote in message
news:25486C86-7D9D-4301-87EA-(E-Mail Removed)...
> You should declare your own delegate type, something like that:
>
> public delegate void ScannedEventHandler(object sender, string
> barcodeData);
>
> and then in your ScanEngine class you'd declare your event as
>
> public event ScannedEventHandler Scanned;
>
> HTH... Alex
>
> --
> Alex Yakhnin, .NET CF MVP
> www.intelliprog.com
> www.opennetcf.org
>
>
> "Dan Bass" wrote:
>
>> I'm writing a scanner application (C# / .Net CF) where I have one
>> scanner
>> object (singleton) that hooks into the scan engine drivers.
>>
>> There is scanner event for a successful scan, that passes through the
>> scanned data to an event handler. From here I need to pass this
>> information
>> back to my form, so what I've done is to create another event handler in
>> the
>> form, which is fired from the event handler in my scanner class.
>>
>> The Form declaration of this object looks like this:
>>
>> Is this ok?
>> Is this considered good practice or is there a blatantly obvious and much
>> better way of doing this?
>>
>> Below is the code that's relevant:
>>
>> // ----------------------------------------------------------
>> //
>> // myForm snippet
>> //
>>
>> ScanEngine scanner;
>>
>> private void myForm_Load(object sender, System.EventArgs e)
>> {
>> scanner = new ScanEngine();
>> scanner.StartScan();
>> scanner.Scanned += new EventHandler(BarcodeRead);
>> }
>>
>> private void FormPut_Closing(object sender,
>> System.ComponentModel.CancelEventArgs e)
>> {
>> if ( scanner != null )
>> {
>> scanner.StopScan();
>> scanner.Scanned -= new EventHandler(BarcodeRead);
>> scanner = null;
>> }
>> }
>>
>> void BarcodeRead(object sender, System.EventArgs e)
>> {
>> if ( scanner != null )
>> {
>> string barData = scanner.BarcodeData;
>>
>> // do stuff here
>>
>> }
>> }
>>
>> // ----------------------------------------------------------
>> //
>> // ScanEngine snippet
>> //
>>
>>
>> private string barData = "";
>> public event System.EventHandler Scanned;
>>
>> // called when a scan occurs
>> protected virtual void OnScanned(EventArgs e)
>> {
>> if (Scanned != null)
>> Scanned(this, e);
>> }
>>
>> public string BarcodeData
>> {
>> get
>> {
>> return barData;
>> }
>> }
>>
>>
>>
>>
>>



 
Reply With Quote
 
Chris Tacke, eMVP
Guest
Posts: n/a
 
      8th Dec 2004
If the scan engine runs on a worker thread you might also consider
marshaling the event back to the primary UI thread in your private handler
through Invoke so that the application consumer of the public event can
directly affect UI elements.

--
<ctacke/>
www.OpenNETCF.org
Your CF searches start and end here


"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:(E-Mail Removed)...
> I'm writing a scanner application (C# / .Net CF) where I have one scanner
> object (singleton) that hooks into the scan engine drivers.
>
> There is scanner event for a successful scan, that passes through the
> scanned data to an event handler. From here I need to pass this
> information back to my form, so what I've done is to create another event
> handler in the form, which is fired from the event handler in my scanner
> class.
>
> The Form declaration of this object looks like this:
>
> Is this ok?
> Is this considered good practice or is there a blatantly obvious and much
> better way of doing this?
>
> Below is the code that's relevant:
>
> // ----------------------------------------------------------
> //
> // myForm snippet
> //
>
> ScanEngine scanner;
>
> private void myForm_Load(object sender, System.EventArgs e)
> {
> scanner = new ScanEngine();
> scanner.StartScan();
> scanner.Scanned += new EventHandler(BarcodeRead);
> }
>
> private void FormPut_Closing(object sender,
> System.ComponentModel.CancelEventArgs e)
> {
> if ( scanner != null )
> {
> scanner.StopScan();
> scanner.Scanned -= new EventHandler(BarcodeRead);
> scanner = null;
> }
> }
>
> void BarcodeRead(object sender, System.EventArgs e)
> {
> if ( scanner != null )
> {
> string barData = scanner.BarcodeData;
>
> // do stuff here
>
> }
> }
>
> // ----------------------------------------------------------
> //
> // ScanEngine snippet
> //
>
>
> private string barData = "";
> public event System.EventHandler Scanned;
>
> // called when a scan occurs
> protected virtual void OnScanned(EventArgs e)
> {
> if (Scanned != null)
> Scanned(this, e);
> }
>
> public string BarcodeData
> {
> get
> {
> return barData;
> }
> }
>
>
>
>



 
Reply With Quote
 
Dan Bass
Guest
Posts: n/a
 
      9th Dec 2004
To be honest, I'm not sure.

How would I check whether it is on the main thread or on a seperate thread?

I update the UI from the result of this scan event, so does this mean that
it is on the UI thread or just that my programming is bad practiced because
I'm not sure?

Thanks for your help.

"Chris Tacke, eMVP" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> If the scan engine runs on a worker thread you might also consider
> marshaling the event back to the primary UI thread in your private handler
> through Invoke so that the application consumer of the public event can
> directly affect UI elements.



 
Reply With Quote
 
Daniel Moth
Guest
Posts: n/a
 
      9th Dec 2004
> How would I check whether it is on the main thread or on a seperate
> thread?

On the CF (where Control.InvokeRequired is not supported), you know by
design. If you are not sure if at a given point you are on the GUI thread or
not (which you should be), just use Control.Invoke anyway. For more on this
topic check out my blog entry:
http://www.danielmoth.com/Blog/2004/...d-full-fx.html

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:%(E-Mail Removed)...
> To be honest, I'm not sure.
>
> How would I check whether it is on the main thread or on a seperate
> thread?
>
> I update the UI from the result of this scan event, so does this mean that
> it is on the UI thread or just that my programming is bad practiced
> because I'm not sure?
>
> Thanks for your help.
>
> "Chris Tacke, eMVP" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> If the scan engine runs on a worker thread you might also consider
>> marshaling the event back to the primary UI thread in your private
>> handler through Invoke so that the application consumer of the public
>> event can directly affect UI elements.

>
>



 
Reply With Quote
 
Dan Bass
Guest
Posts: n/a
 
      9th Dec 2004
I've never heard of how to use Invoke before, makes me humble for sure!

"Daniel Moth" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>> How would I check whether it is on the main thread or on a seperate
>> thread?

> On the CF (where Control.InvokeRequired is not supported), you know by
> design. If you are not sure if at a given point you are on the GUI thread
> or not (which you should be), just use Control.Invoke anyway. For more on
> this topic check out my blog entry:
> http://www.danielmoth.com/Blog/2004/...d-full-fx.html
>
> Cheers
> Daniel
> --
> http://www.danielmoth.com/Blog/
>
>
> "Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
> news:%(E-Mail Removed)...
>> To be honest, I'm not sure.
>>
>> How would I check whether it is on the main thread or on a seperate
>> thread?
>>
>> I update the UI from the result of this scan event, so does this mean
>> that it is on the UI thread or just that my programming is bad practiced
>> because I'm not sure?
>>
>> Thanks for your help.
>>
>> "Chris Tacke, eMVP" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> If the scan engine runs on a worker thread you might also consider
>>> marshaling the event back to the primary UI thread in your private
>>> handler through Invoke so that the application consumer of the public
>>> event can directly affect UI elements.

>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Event Handler Fires Twice =?Utf-8?B?R3JlZw==?= Microsoft VB .NET 3 31st Oct 2007 02:49 PM
Textbox LostFocus event fires after Command Button's OnClick event teddysnips@hotmail.com Microsoft VB .NET 14 10th Aug 2006 01:47 PM
VSTO Outlook AddIn - Toolbar button Event Handler Fires One Time Only Joseph Geretz Microsoft C# .NET 6 23rd May 2006 04:33 PM
c# event handler fires an event Dan Bass Microsoft C# .NET 6 9th Dec 2004 11:23 AM
comboBox TextChanged event fires twice because Items.Insert() fires the event too... Rob Hindman Microsoft Dot NET Framework Forms 2 25th Aug 2003 07:10 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:35 AM.