473,464 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

c# event handler fires an event

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;
}
}


Nov 16 '05 #1
6 5500
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;
}
}


Nov 16 '05 #2
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]" <a.*******@online.att.net> wrote in message
news:25**********************************@microsof t.com...
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;
}
}


Nov 16 '05 #3
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:er**************@TK2MSFTNGP09.phx.gbl...
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;
}
}

Nov 16 '05 #4
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" <ct****@spamfree-opennetcf.org> wrote in message
news:eb**************@TK2MSFTNGP14.phx.gbl...
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.

Nov 16 '05 #5
> 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:%2***************@TK2MSFTNGP10.phx.gbl... 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" <ct****@spamfree-opennetcf.org> wrote in message
news:eb**************@TK2MSFTNGP14.phx.gbl...
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.


Nov 16 '05 #6
I've never heard of how to use Invoke before, makes me humble for sure!

"Daniel Moth" <dm*****@hotmail.com> wrote in message
news:et**************@TK2MSFTNGP12.phx.gbl...
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:%2***************@TK2MSFTNGP10.phx.gbl...
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" <ct****@spamfree-opennetcf.org> wrote in message
news:eb**************@TK2MSFTNGP14.phx.gbl...
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.



Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Tom | last post by:
Hi, In the following code I have reproduced a problem in IE that is extremely annoying for my application. <html> <head> <script type="text/javascript">
1
by: Earl Teigrob | last post by:
I did a ton of searching to try and find a simple solution to this issue and finally wrote my own, which I am sharing with everyone. In my searching, I did find a very complete and robust solution at...
8
by: Paul | last post by:
Hi, In a class i built, i fire an event when a field changes. In a webform that has an instance of the class, the event (from the class) is fired and the code is executed. However, my webpage...
2
by: Breeto | last post by:
Can anyone please tell me why the following doesn't work... using System; using System.Web; namespace AspTests {
3
by: bill | last post by:
I am using VS2005 to build a web form dynamically. I'm using AddHandler to connect a custom event handler to the TextChanged event of dynamically added textbox controls. Data entered in the...
6
by: Joseph Geretz | last post by:
I'm porting a C# Outlook Addin originally engineered as a COM Addin over to use VSTO. I've gotten this to the point where my VSTO Addin installs its Menu items and Toolbar buttons when Outlook...
9
by: jeff | last post by:
New VB user...developer... Situation...simplified... - I want to wrap a pre and post event around a system generated where the pre-event will always execute before the system event and the...
3
by: Drex10 | last post by:
I have an event that occurs within a user control. The event is for a dropdownlist within the user control which fires when the selected item changes. This user control is created dynamicaly...
1
by: Jeremy Martin | last post by:
Hi, I am currently learning c# so be gentle :) I am converting a Delphi.NET website to c# website and I am confused as to how events are handled. 2 Situations. 1. A component on a...
6
by: kirk | last post by:
I have three events, using event handler methods as depicted below. Two of those event handler methods need to reset specific data whenever the other event left fires. I wasn't sure how to...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.