views:

293

answers:

1

Hello all

I have a feature whereby a class in a dll displays a form asking a user to clear a fault on a printer before clicking a button to say "Retry". The users have been just hitting retry without bothering to clear the fault so I am now coding an interlock: The button on the invoked form is disabled until a call is made to an 'enable' method on the form.

This is done with delegate invocation as the events triggering these changes come from other dlls running on different threads.

The form's 'enable' method is wired into an EVENT HANDLER handling an event coming in from a different thread (one that monitors an ethernet IO Server).

The problem I have is that THE "_Fault_StateChanged" EVENT NEVER FIRES. I suspected the cause was the "ShowDialog" and "DialogResult" technique I have used here, but I have used this exact same technique elsewhere in this application.

Any suggestions would be great

See code extract below:

MAIN CLASS excerpt

Public Class StatePrintHandler

    Private WithEvents _RetryForm As frmRetryReject

    Private Delegate Sub delShowRetryDialog()
    Private Delegate Sub delResetEnable()

    Private Sub InvokeResetEnable()
        Dim del As delResetEnable
        del = New delResetEnable(AddressOf ResetEnable)
        del.Invoke()
    End Sub

    Private Sub InvokeRetryDialogue()
        Dim del As delShowRetryDialog
        del = New delShowRetryDialog(AddressOf ShowRetryDialog)
        del.Invoke()
    End Sub

    Private Sub ShowRetryDialog()
        _RetryForm = New frmRetryReject
        _RetryForm.Prep()
        _RetryForm.ShowDialog()
        If (_RetryForm.DialogResult = Windows.Forms.DialogResult.OK) Then
            Me._RetryForm.Visible = False
        End If
    End Sub


Private Sub ResetEnable()
    If (Not IsNothing(_RetryForm)) Then
        _RetryForm.ResetEnable()
    Else
        AuditTrail("Retry form not active, no action", True)
    End If
End Sub


'Event handler for status change coming in on a different thread
    Private Sub _Fault_StateChanged(ByVal sender As Object, ByVal e As Drivers.Common.DigitalSignalChangedEventArgs) Handles _fault.StateChanged
        If (e.NewState) Then
                AuditTrail("Labeller has faulted out during cycling", True)
        Else
                InvokeResetEnable()
        End If
    End Sub
End Class

RETRY FORM CLASS excerpt

Public Class frmRetryReject
    Private Delegate Sub delEnable()
    Public Event Complete()
    Public Sub Prep()
        Me.OK_Button.Enabled = False
    End Sub
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub
    Public Sub ResetEnable()
        If (IsHandleCreated) Then
            Dim params() As Object = {}
            Me.Invoke(New delEnable(AddressOf InvokeEnable), params)
        End If
    End Sub
    Private Sub InvokeEnable()
        Me.OK_Button.Enabled = True
    End Sub
End Class

Additional detail in response to Daniel's comments

The code is incomplete here, it's an excerpt. The fault object is a subscription to an external library and is a handler for an ethernet IO server. The _fault StateChanged event fires when a digital input on an IOServer changes. I know the following: My trace files show the signal change high, this invokes the retry form. The signal physically changes low again when the retry screen is still showing. ...but the event does not fire

It's as if the application cannot service the event coming in until the ShowDialog/DialogResult completes - but I am confused about this because I understood that the ShowDialog in .NET 2.0 did not block, I should still be able to service events, and have used this same pattern elsewhere in the app.

A couple of things to note: The MAIN CLASS is instantiated dynamically at runtime via reflection based on configuration. This is VS2005 SP2

I will post the entire class in another code box if this helps, but it may crowd the scene...

Thanks Andy

Imports System.IO
Imports ACS.Interfaces
Imports ACS.Pallet

Public Class StateCimPAKPrintHandler
    Inherits StateBase
    Private WithEvents _ioManager As ACS.Components.DigitalIOManager
    Private _config As StateCimPAKPrintHandlerBootstrap
    Private CONST_OutcomeOK As String = "OK"
    Private CONST_OutcomeRetry As String = "Retry"
    Private CONST_OutcomeException As String = "Exception"

    Private WithEvents _busy As ACS.Drivers.Common.InputSignal
    Private WithEvents _fault As ACS.Drivers.Common.InputSignal
    Private WithEvents _print As ACS.Drivers.Common.OutputSignal
    Private WithEvents _palletRelease As ACS.Drivers.Common.OutputSignal

    Private _labellingInProgress As Boolean
    Private _faulted As Boolean
    Private _retryScreenInvoked As Boolean
    Private WithEvents _timeout As System.Timers.Timer
    Private WithEvents _faultTimer As System.Timers.Timer

    Private WithEvents _RetryForm As frmRetryReject

    Private Delegate Sub delShowRetryDialog()
    Private Delegate Sub delResetEnable()

    Private Sub InvokeResetEnable()
        Dim del As delResetEnable
        del = New delResetEnable(AddressOf ResetEnable)
        del.Invoke()
    End Sub

    Private Sub InvokeRetryDialogue()
        Dim del As delShowRetryDialog
        del = New delShowRetryDialog(AddressOf ShowRetryDialog)
        del.Invoke()
    End Sub

    Private Sub ShowRetryDialog()
        _timeout.Stop()
        _retryScreenInvoked = True
        _RetryForm = New frmRetryReject
        _RetryForm.Prep()
        AuditTrail("Displaying Retry screen", True)
        _RetryForm.ShowDialog()
        If (_RetryForm.DialogResult = Windows.Forms.DialogResult.OK) Then
            AuditTrail("User clicked RETRY LINE on the RETRY dialogue", True)
            _retryScreenInvoked = False
            Me._RetryForm.Visible = False
            Me.SetOutcome(CONST_OutcomeRetry)
        End If
    End Sub

    Private Sub ResetEnable()
        If (Not IsNothing(_RetryForm)) Then
            _RetryForm.ResetEnable()
        Else
            AuditTrail("Retry form not active, no action", True)
        End If
    End Sub

    Public Sub New(ByVal Sequencer As ISequencer, ByVal ParentPlt As ACS.Interfaces.IPallet, ByVal Name As String)
        MyBase.New(Sequencer, ParentPlt, Name)
        _timeout = New System.Timers.Timer
        _faultTimer = New System.Timers.Timer
        _config = New StateCimPAKPrintHandlerBootstrap(Me._myIniFileName, Name)
        _timeout.Interval = _config.CycleTimeoutMS
        _faultTimer.Interval = 750
        _retryScreenInvoked = False
        _RetryForm = New frmRetryReject
        Me._RetryForm.Visible = False
        _ioManager = ACS.Components.DigitalIOManager.GetInstance
        _busy = _ioManager.GetInput("Busy")
        _fault = _ioManager.GetInput("CimPAKFault")
        _print = _ioManager.GetOutput("Print")
        _palletRelease = _ioManager.GetOutput("PalletRelease")
    End Sub
    Public Overrides Sub Kill()
        _ioManager = Nothing
        _RetryForm = Nothing
        _busy = Nothing
        _fault = Nothing
        _print = Nothing
        _timeout = Nothing
        _faultTimer = Nothing
        _pallet = Nothing
    End Sub
    Public Overrides Sub Execute()
        AuditTrail("Pulsing Print Signal", True)
        _print.PulseOutput(3000)
        _labellingInProgress = True
        _timeout.Start()
    End Sub
    Private Sub _busy_StateChanged(ByVal sender As Object, ByVal e As Drivers.Common.DigitalSignalChangedEventArgs) Handles _busy.StateChanged
        _timeout.Stop()
        AuditTrail("Busy signal changed to : " & e.NewState, True)
        If (e.NewState) Then
            _faulted = False
            AuditTrail("CimPAK = Busy High", True)
            _labellingInProgress = True
        Else
            AuditTrail("CimPAK = Busy Low", True)
            AuditTrail("Wait 750 milliseconds for any faults", True)
            _faultTimer.Start()
        End If
    End Sub
    Private Sub _Fault_StateChanged(ByVal sender As Object, ByVal e As Drivers.Common.DigitalSignalChangedEventArgs) Handles _fault.StateChanged
        AuditTrail("Fault signal changed to : " & e.NewState, True)
        If (e.NewState) Then
            If (_labellingInProgress = True) Then
                AuditTrail("Labeller has faulted out during cycling", True)
                _faulted = True
                If (Not _retryScreenInvoked) Then
                    InvokeRetryDialogue()
                End If
            Else
                AuditTrail("Labeller has faulted out between cycles, no action can be taken", True)
            End If
        Else
            If (_retryScreenInvoked) Then
                AuditTrail("Enable button on Retry screen", True)
                InvokeResetEnable()
            End If
            _faulted = False
        End If
    End Sub
    Private Sub _faultTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles _faultTimer.Elapsed
        _faultTimer.Stop()
        If (_faulted) Then
            AuditTrail("System has faulted", True)
        Else
            AuditTrail("No fault occured, assume pallet is OK to release", True)
            AuditTrail("CimPAK cycle complete", True)
            _labellingInProgress = False
            _palletRelease.PulseOutput(3000)
            Me.SetOutcome(CONST_OutcomeOK)
        End If
    End Sub
    Private Sub _timeout_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles _timeout.Elapsed
        _timeout.Stop()
        AuditTrail("Labeller print cycle timed out", True)
        If (Not _retryScreenInvoked) Then
            _retryScreenInvoked = True
            InvokeRetryDialogue()
            InvokeResetEnable()
        End If
    End Sub



End Class

#Region "Bootstrap"
Public Class StateCimPAKPrintHandlerBootstrap
    Private Const CONST_CycleTimeoutMS As String = "CycleTimeoutMS"
    Private _CycleTimeoutMS As Long
#Region "Properties"
    Public ReadOnly Property CycleTimeoutMS() As Long
        Get
            Return _CycleTimeoutMS
        End Get
    End Property
#End Region
    Public Sub New(ByVal IniFile As String, ByVal Name As String)
        Try
            Dim _cfgFile As String = Environ("ACSVAR") & "\" & IniFile
            ' Check to see if the CFG file exits
            If File.Exists(_cfgFile) = False Then
                Throw New Exception("Configuration file does not exist: " & _cfgFile)
            Else
                'Get values
                _CycleTimeoutMS = ACS.Utility.Configuration.GetLong(_cfgFile, Name, CONST_CycleTimeoutMS)
            End If
        Catch ex As Exception
            Throw
        End Try
    End Sub
End Class
#End Region
A: 

It's been a long time since I did any Winforms work, but are you showing the dialog modally? Because that could be the cause of the delegate invokation not arriving.

Asynchronous delegates work by serializing the invokation details into a memory location internal to the framework and then posting a window message to the top-level window for the relevant thread. Modal dialogs work by going into a message processing loop that steals all the messages for the thread so that only that one dialog can respond. You can see how this might conflict.

U62
U62The ShowDialog call to create the form should render it as modal, so yes, it is a modal form, but I cannot see why the modal form should be preventing an event coming into my MAIN CLASS (StateCimPAKPrintHandler)... It is this class that handles the incoming event from the IOServer and THEN invokes the delegate to update the MODAL FORM (frmRetryReject) changes in a thread safe way...to enable the button. The way this is behaving, its as if the entire application call stack is blocked until someone completes the MODAL loop by hitting the button which triggers the DialogResult return
GermanAndy