Subscribed Singleton Method Not Firing - nested windows forms
Adding support for a new mobile device to a legacy application.
Singleton Class Implemented Based On This Article Create Event on Singleton Class
public sealed class Singleton
{
static readonly Singleton instance=new Singleton();
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Singleton()
{
}
Singleton()
{
}
public static Singleton Instance
{
get
{
return instance;
}
}
}
On creation, Each form subscribes a local method to the read method of the barcode object
singleton.Instance.scanEngine.BarcodeRead += new BarcodeReadEventHandler(FormA_BarcodeReadMethod);
On close, Each form unsubscribes from the barcode object read method
singleton.Instance.scanEngine.BarcodeRead -= new BarcodeReadEventHandler(FormA_BarcodeReadMethod);
Scenario #1
- FormA is called from main menu form
- singleton BarcodeReader.BarcodeScan event is null;
- FormA created
FormA event subscribes to singleton BarcodeReader.BarcodeScan event
'singleton.Instance.scanEngine.BarcodeRead += new BarcodeReadEventHandler(FormA_BarcodeReadMethod);'
singleton BarcodeReader.BarcodeScan event is not null;
- Barcode scans and FormA_BarcodeReadMethod fires.
FormA closed, FormA event unsubscribed to singleton
BarcodeReader.BarcodeScan event singleton.Instance.scanEngine.BarcodeRead -= new BarcodeReadEventHandler(FormA_BarcodeReadMethod);
singleton BarcodeReader.BarcodeScan event is null;
- back to main menu form
There are 5 other forms which are called from the main screen and behave ok as above
There is one instance where a form with a scanner subscription calls another form which also requires a scanner subscription
Scenario #2
- FormB is called from main menu form
- singleton BarcodeReader.BarcodeScan event is null;
- FormB created
FormB event subscribes to singleton
singleton.Instance.scanEngine.BarcodeRead += new BarcodeReadEventHandler(FormB_BarcodeReadMethod);
singleton BarcodeReader.BarcodeScan event is not null;
- Barcode scans and FormB_BarcodeReadMethod fires.
When processing the fired event, FormB calls FormC as follows
FormB event unsubscribed to singleton BarcodeReader.BarcodeScan event
singleton.Instance.scanEngine.BarcodeRead -= new BarcodeReadEventHandler(FormB_BarcodeReadMethod);
singleton BarcodeReader.BarcodeScan event is null;
- FormC created
FormC event subscribes to singleton BarcodeReader.BarcodeScan event
singleton.Instance.scanEngine.BarcodeRead += new BarcodeReadEventHandler(FormC_BarcodeReadMethod);
singleton BarcodeReader.BarcodeScan event is not null;
- Barcode scans and FormC_BarcodeReadMethod does not fire.
FormC closed, FormC event unsubscribed to singleton BarcodeReader.BarcodeScan event
singleton.Instance.scanEngine.BarcodeRead -= new BarcodeReadEventHandler(FormC_BarcodeReadMethod);
singleton BarcodeReader.BarcodeScan event is null; - Control returns to FormB - FormB event subscribes to singleton BarcodeReader.BarcodeScan eventsingleton.Instance.scanEngine.BarcodeRead += new BarcodeReadEventHandler(FormB_BarcodeReadMethod); - singleton BarcodeReader.BarcodeScan event is not null; - Barcode scans and FormB_BarcodeReadMethod fires.
The expect result would be that the subscribed FormC event would fire but it does not and I cannot figure out why that is.
Any help much appreciated.
Response To Comment #1
In FormB, a decision is made to call FormC On FormB scanner event is unsubscribed
clsApplicationController.Instance.scanEngineIntermec.BarcodeRead -= new
BarcodeReadEventHandler(this.frmB_Intermec_OnRead);
this.Update();
Application.DoEvents();
FormC is the called as follows
using (formC _formC = new formC())
{
_formC.cUser = _cUser;
var _result = _formC.ShowDialog();
if (_result != DialogResult.OK) return;
}
event is subscribed in FormC constructor
clsApplicationController.Instance.scanEngine.BarcodeRead += new
BarcodeReadEventHandler(this.frmC_OnRead);
this.Update();
Application.DoEvents();
Response To Comment #2
FormC event which is subscribed to but not firing
void formC_OnRead(object sender, BarcodeReadEventArgs bre)
{
try {
ProcessScan(bre.strDataBuffer, sender);
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
}
the formC_OnRead method is subscribed to the Instance.scanEngineIntermec.BarcodeRead event in the FormC constructor. ProcessScan is generic for use by all scanner types. The same method and approach is implemented on the working forms