Plugin that kills performance  Topic is solved

Discussions relating to Jiwa 7 plugin development, and the Jiwa 7 API.

Plugin that kills performance

Postby Atronics » Fri Jul 03, 2015 6:50 pm

When I enabled the attached plugin, the system grinds to a halt.

Imports JiwaFinancials.Jiwa
Imports Microsoft.VisualBasic
Imports System.Windows.Forms
Imports System.Data.SqlClient
Imports System.Drawing
Imports System.Data

Public Class FormPlugin
Inherits System.MarshalByRefObject
Implements JiwaApplication.IJiwaFormPlugin

Private salesOrderForm As JiwaSalesUI.SalesOrder.SalesOrderEntryForm

Public Overrides Function InitializeLifetimeService() As Object
' returning null here will prevent the lease manager
' from deleting the Object.
Return Nothing
End Function

Public Sub SetupBeforeHandlers(ByVal JiwaForm As JiwaApplication.IJiwaForm, ByVal Plugin As JiwaApplication.Plugin.Plugin) Implements JiwaApplication.IJiwaFormPlugin.SetupBeforeHandlers
End Sub

Public Sub Setup(ByVal JiwaForm As JiwaApplication.IJiwaForm, ByVal Plugin As JiwaApplication.Plugin.Plugin) Implements JiwaApplication.IJiwaFormPlugin.Setup
salesOrderForm = DirectCast(JiwaForm, JiwaSalesUI.SalesOrder.SalesOrderEntryForm)
AddHandler salesOrderForm.SalesOrder.SaveEnd , AddressOf SalesOrderSaveEnd
End Sub

Private Sub SalesOrderSaveEnd(sender As Object, e As System.EventArgs )
If salesOrderForm.SalesOrder.SalesOrderLines.Count > 0 Then
For Each salesOrderLine As JiwaSales.SalesOrder.SalesOrderLine In salesOrderForm.SalesOrder.SalesOrderLines
If salesOrderLine.CostIn <> 0 Then
salesOrderLine.UserDefinedFloat1 = (((salesOrderLine.LineTotal/salesOrderLine.QuantityThisDelivery) - salesOrderLine.CostIn)/ salesOrderLine.CostIn)*100
Else
salesOrderLine.UserDefinedFloat1 = 0
End If
Next
salesOrderForm.SalesOrder.Save
End If
End Sub

End Class

Public Class BusinessLogicPlugin
Inherits System.MarshalByRefObject
Implements JiwaApplication.IJiwaBusinessLogicPlugin

Public Overrides Function InitializeLifetimeService() As Object
' returning null here will prevent the lease manager
' from deleting the Object.
Return Nothing
End Function

Public Sub Setup(ByVal JiwaBusinessLogic As JiwaApplication.IJiwaBusinessLogic, ByVal Plugin As JiwaApplication.Plugin.Plugin) Implements JiwaApplication.IJiwaBusinessLogicPlugin.Setup
End Sub

End Class

Public Class ApplicationManagerPlugin
Inherits System.MarshalByRefObject
Implements JiwaApplication.IJiwaApplicationManagerPlugin

Public Overrides Function InitializeLifetimeService() As Object
' returning null here will prevent the lease manager
' from deleting the Object.
Return Nothing
End Function

Public Sub Setup(ByVal Plugin As JiwaApplication.Plugin.Plugin) Implements JiwaApplication.IJiwaApplicationManagerPlugin.Setup
End Sub

End Class

Public Class CustomFieldPlugin
Inherits System.MarshalByRefObject
Implements JiwaApplication.IJiwaCustomFieldPlugin

Public Overrides Function InitializeLifetimeService() As Object
' returning null here will prevent the lease manager
' from deleting the Object.
Return Nothing
End Function

Public Sub FormatCell(ByVal BusinessLogicHost As JiwaApplication.IJiwaBusinessLogic, ByVal GridObject As JiwaApplication.Controls.JiwaGrid, ByVal FormObject As JiwaApplication.IJiwaForm, ByVal Col As Integer, ByVal Row As Integer, ByVal HostObject As JiwaApplication.IJiwaCustomFieldValues, ByVal CustomField As JiwaApplication.CustomFields.CustomField, ByVal CustomFieldValue As JiwaApplication.CustomFields.CustomFieldValue) Implements JiwaApplication.IJiwaCustomFieldPlugin.FormatCell
End Sub

Public Sub ReadData(ByVal BusinessLogicHost As JiwaApplication.IJiwaBusinessLogic, ByVal GridObject As JiwaApplication.Controls.JiwaGrid, ByVal FormObject As JiwaApplication.IJiwaForm, ByVal Row As Integer, ByVal HostObject As JiwaApplication.IJiwaCustomFieldValues, ByVal CustomField As JiwaApplication.CustomFields.CustomField, ByVal CustomFieldValue As JiwaApplication.CustomFields.CustomFieldValue) Implements JiwaApplication.IJiwaCustomFieldPlugin.ReadData
End Sub

Public Sub ButtonClicked(ByVal BusinessLogicHost As JiwaApplication.IJiwaBusinessLogic, ByVal GridObject As JiwaApplication.Controls.JiwaGrid, ByVal FormObject As JiwaApplication.IJiwaForm, ByVal Col As Integer, ByVal Row As Integer, ByVal HostObject As JiwaApplication.IJiwaCustomFieldValues, ByVal CustomField As JiwaApplication.CustomFields.CustomField, ByVal CustomFieldValue As JiwaApplication.CustomFields.CustomFieldValue) Implements JiwaApplication.IJiwaCustomFieldPlugin.ButtonClicked
End Sub

End Class

Public Class LineCustomFieldPlugin
Inherits System.MarshalByRefObject
Implements JiwaApplication.IJiwaLineCustomFieldPlugin

Public Overrides Function InitializeLifetimeService() As Object
' returning null here will prevent the lease manager
' from deleting the Object.
Return Nothing
End Function

Public Sub FormatCell(ByVal BusinessLogicHost As JiwaApplication.IJiwaBusinessLogic, ByVal GridObject As JiwaApplication.Controls.JiwaGrid, ByVal FormObject As JiwaApplication.IJiwaForm, ByVal Col As Integer, ByVal Row As Integer, ByVal HostItem As JiwaApplication.IJiwaLineCustomFieldValues, ByVal CustomField As JiwaApplication.CustomFields.CustomField, ByVal CustomFieldValue As JiwaApplication.CustomFields.CustomFieldValue) Implements JiwaApplication.IJiwaLineCustomFieldPlugin.FormatCell
End Sub

Public Sub ReadData(ByVal BusinessLogicHost As JiwaApplication.IJiwaBusinessLogic, ByVal GridObject As JiwaApplication.Controls.JiwaGrid, ByVal FormObject As JiwaApplication.IJiwaForm, ByVal Row As Integer, ByVal HostItem As JiwaApplication.IJiwaLineCustomFieldValues, ByVal CustomField As JiwaApplication.CustomFields.CustomField, ByVal CustomFieldValue As JiwaApplication.CustomFields.CustomFieldValue) Implements JiwaApplication.IJiwaLineCustomFieldPlugin.ReadData
End Sub

Public Sub ButtonClicked(ByVal BusinessLogicHost As JiwaApplication.IJiwaBusinessLogic, ByVal GridObject As JiwaApplication.Controls.JiwaGrid, ByVal FormObject As JiwaApplication.IJiwaForm, ByVal Col As Integer, ByVal Row As Integer, ByVal HostItem As JiwaApplication.IJiwaLineCustomFieldValues, ByVal CustomField As JiwaApplication.CustomFields.CustomField, ByVal CustomFieldValue As JiwaApplication.CustomFields.CustomFieldValue) Implements JiwaApplication.IJiwaLineCustomFieldPlugin.ButtonClicked
End Sub

End Class

Public Class SystemSettingPlugin
Inherits System.MarshalByRefObject
Implements JiwaApplication.IJiwaSystemSettingPlugin

Public Overrides Function InitializeLifetimeService() As Object
' returning null here will prevent the lease manager
' from deleting the Object.
Return Nothing
End Function

Public Sub FormatCell(ByVal BusinessLogicHost As JiwaApplication.IJiwaBusinessLogic, ByVal GridObject As JiwaApplication.Controls.JiwaGrid, ByVal FormObject As JiwaApplication.IJiwaForm, ByVal Col As Integer, ByVal Row As Integer, ByVal SystemSetting As JiwaApplication.SystemSettings.Setting) Implements JiwaApplication.IJiwaSystemSettingPlugin.FormatCell
End Sub

Public Sub ReadData(ByVal BusinessLogicHost As JiwaApplication.IJiwaBusinessLogic, ByVal GridObject As JiwaApplication.Controls.JiwaGrid, ByVal FormObject As JiwaApplication.IJiwaForm, ByVal Row As Integer, ByVal SystemSetting As JiwaApplication.SystemSettings.Setting) Implements JiwaApplication.IJiwaSystemSettingPlugin.ReadData
End Sub

Public Sub ButtonClicked(ByVal BusinessLogicHost As JiwaApplication.IJiwaBusinessLogic, ByVal GridObject As JiwaApplication.Controls.JiwaGrid, ByVal FormObject As JiwaApplication.IJiwaForm, ByVal Col As Integer, ByVal Row As Integer, ByVal SystemSetting As JiwaApplication.SystemSettings.Setting) Implements JiwaApplication.IJiwaSystemSettingPlugin.ButtonClicked
End Sub

End Class

Public Class ScheduledExecutionPlugin
Inherits System.MarshalByRefObject
Implements JiwaApplication.IJiwaScheduledExecutionPlugin

Public Sub Execute(ByVal Plugin As JiwaApplication.Plugin.Plugin, ByVal Schedule As JiwaApplication.Schedule.Schedule) Implements JiwaApplication.IJiwaScheduledExecutionPlugin.Execute

End Sub

Public Sub OnServiceStart(ByVal Plugin As JiwaApplication.Plugin.Plugin) Implements JiwaApplication.IJiwaScheduledExecutionPlugin.OnServiceStart

End Sub

Public Sub OnServiceStopping(ByVal Plugin As JiwaApplication.Plugin.Plugin) Implements JiwaApplication.IJiwaScheduledExecutionPlugin.OnServiceStopping

End Sub
End Class
Atronics
Frequent Contributor
Frequent Contributor
 
Posts: 119
Joined: Fri Feb 29, 2008 4:40 pm
Topics Solved: 10

Re: Plugin that kills performance  Topic is solved

Postby Mike.Sheen » Fri Jul 03, 2015 8:18 pm

The problem is here:

Code: Select all
Private Sub SalesOrderSaveEnd(sender As Object, e As System.EventArgs )
    If salesOrderForm.SalesOrder.SalesOrderLines.Count > 0 Then
        For Each salesOrderLine As JiwaSales.SalesOrder.SalesOrderLine In salesOrderForm.SalesOrder.SalesOrderLines
            If salesOrderLine.CostIn <> 0 Then
                salesOrderLine.UserDefinedFloat1 = (((salesOrderLine.LineTotal/salesOrderLine.QuantityThisDelivery) - salesOrderLine.CostIn)/ salesOrderLine.CostIn)*100
            Else
                salesOrderLine.UserDefinedFloat1 = 0
            End If
        Next
        salesOrderForm.SalesOrder.Save
    End If
End Sub


Calling SalesOrder.Save inside the SaveEnd handler is going to result in an infinite loop. If you want to modify values of an order before saving it, you should be doing that in the SaveStart, not the SaveEnd (or even SaveEnding). That way you don't need to call SalesOrder.Save, and it won't loop around forever.

Also the sender inside the SaveStart, End and Ending methods is the sales order business logic, you should be using that, not the salesOrderForm.SalesOrder. Incorporate the following changes:

Code: Select all
' Replace AddHandler salesOrderForm.SalesOrder.SaveEnd , AddressOf SalesOrderSaveEnd with this:
AddHandler salesOrderForm.SalesOrder.SaveStart , AddressOf SalesOrderSaveStart


Code: Select all
' Replace the SalesOrderSaveEnd wtih this:
Private Sub SalesOrderSaveStart(sender As Object, e As System.EventArgs )
    Dim SalesOrder As JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrder = sender

    If SalesOrder.SalesOrderLines.Count > 0 Then
        For Each salesOrderLine As JiwaSales.SalesOrder.SalesOrderLine In SalesOrder.SalesOrderLines
            If salesOrderLine.CostIn <> 0 Then
                salesOrderLine.UserDefinedFloat1 = (((salesOrderLine.LineTotal/salesOrderLine.QuantityThisDelivery) - salesOrderLine.CostIn)/ salesOrderLine.CostIn)*100
            Else
                salesOrderLine.UserDefinedFloat1 = 0
            End If
        Next
    End If
End Sub


I also note you are correctly checking for a divide by zero on salesOrderLine.CostIn, but not salesOrderLine.QuantityThisDelivery - you should protect against that also!
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2583
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 807

Re: Plugin that kills performance

Postby Atronics » Wed Jul 08, 2015 2:28 pm

Thanks, Mike. Your quick response is much appreciated.
Atronics
Frequent Contributor
Frequent Contributor
 
Posts: 119
Joined: Fri Feb 29, 2008 4:40 pm
Topics Solved: 10


Return to Technical and or Programming

Who is online

Users browsing this forum: No registered users and 4 guests