Using BeginInvoke in Web asp.net VB - vb.net

I am completely new in Delegate and BeginInvoke . I have a project which is written as Win form app (Vb) and now I have to change it in Web asp.net and I really dont know how I can do that:
More about my project:
when I click on run button, my project runs and it caculates some data and it shows results in some textboxes till it finished . At the End the button save data enabled in order to save Results:
See it in action:
How the Project work
Run Button Function :
e.Run.Enabled = False
Me.Run.Text = "Please wait ..."
Me.simphony.Enabled = False
Me.access.Enabled = False
Me.optimizationEngineField = New OptimizationEngine(economyImportance:=0.5,
simphonyAnalysisModel:=Me.simphonyAnalysisEngine)
Dim tempThread As New Threading.Thread(Sub()
Me.bestSolutionField = Me.optimizationEngineField.StartOptimization(percentErrorTermination:=True,
maxIteration:=CInt(Me.TextBoxMaxIteration.Text),
percentError:=CDbl(Me.TextBoxAccuracy.Text))
End Sub)
tempThread.Start()
Me.TextBoxAccuracy.Enabled = False
Me.TextBoxMaxIteration.Enabled = False
Me.OptimizationStatusUpdate()
<br>
Optimization
<br>
Private Sub OptimizationStatusUpdate()
Dim tempThread As New Threading.Thread(Sub()
Dim attempt As Integer = 0
Dim firstRun As Boolean = True
While True
If Me.optimizationEngineField.IsRunning Then
firstRun = False
Me.BeginInvoke(Sub()
Me.TextBoxStatus.Text = Me.optimizationEngineField.Status
Me.TextBoxIsRunning.Text = Me.optimizationEngineField.IsRunning
Me.TextBoxIterationNumber.Text = Me.optimizationEngineField.IterationNumber
Me.TextBoxPercentError.Text = Me.optimizationEngineField.PercentError
Me.TextBoxActiveAntCount.Text = Me.optimizationEngineField.ActiveAntCount
Me.TextBoxAggregatedAntCount.Text = Me.optimizationEngineField.aggregatedAntCount
End Sub)
Else
If Not Me.optimizationEngineField.IsRunning And firstRun = False Then
attempt = attempt + 1
End If
If attempt >= 5 Then
Exit While
End If
End If
Threading.Thread.Sleep(1000)
End While
Me.BeginInvoke(Sub()
Me.ButtonSaveResults.Text = "Save Results"
Me.ButtonSaveResults.Enabled = True
Me.TextBoxIsRunning.Text = "False"
Me.Run.Enabled = True
Me.Run.Text = "Run"
Me.simphony.Enabled = True
Me.access.Enabled = True
End Sub)
End Sub)
tempThread.Start()
End Sub
<br>
I searched a lot in net and
First of all : all pages approximately have been written for win apps and they are not for web , If I am not wrong and secondly : I Try to convert those codes from C# to VB and got some errors
How can I change Optimization Code to run on Web ?
Thanks

Related

How to fix Out of Memory in MS Access?

I have an MS Access project which I am developing. Everything was good until I clicked the Compact and Repair Database, now every time my app hit line of code start from here Form_PersonalInfoPanel.cmboGender.Enabled = True from this method:
Public Sub setEmployeeForms(flag As Integer)
If flag = 0 Then
...
Else
Form_EmployeePanel.txtFullName.Enabled = True
Form_EmployeePanel.txtFullNameAr.Enabled = True
Form_EmployeePanel.cmbPcn.Enabled = True
Form_EmployeePanel.cmboDepartment.Enabled = True
Form_EmployeePanel.cmboJob.Enabled = True
Form_PersonalInfoPanel.cmboGender.Enabled = True
Form_PersonalInfoPanel.cmboMaritalStatus.Enabled = True
Form_PersonalInfoPanel.cmboNationality.Enabled = True
Form_PersonalInfoPanel.txtDob.Enabled = True
Form_PersonalInfoPanel.txtEmail.Enabled = True
Form_PersonalInfoPanel.txtMobile.Enabled = True
End If
End Sub
It now giving me:
Run-Time error '7':
Out of Memory
In the Debug mode, it says: <Object variable or With block variable not set> As for this one Form_EmployeePanel.cmboJob.Enabled = True, it works fine. My project is only 3.476 MB and I already deleted all files in my temporary folder.
Any help is much appreciated.

Form Show not Called After Second Show

So I am using Visual Basic to create a windows form application. I have buttons on the side to select a table like this. When the 'bookings' button is pressed, the user it taken to a form that looks like this. I need the following code to run every time the form is shown (the form is hidden shown multiple times). I put the below code inside both the form load and the form shown actions and both of them will only run the code on the first time. The code isn't really relevant, I just need to know how I can make this run every time the form is shown, not just once. The only other way I have thought about which works is to make the code run every second which is just unnecessary and a waste of processing power. Also, if I wanted another action to run after this, the action that occurs every second will take priority.
Private Sub Form2_Shown(sender As Object, e As EventArgs) Handles Me.Shown
If GlobalVariables.GlobalFlag = "1" Then
Table1Sleep.Visible = False
Table1Selected.Visible = True
Table1Selected.BringToFront()
table1text.BringToFront()
table1textdesc.BringToFront()
ElseIf GlobalVariables.GlobalFlag = "2" Then
Table2Sleep.Visible = False
Table2Selected.Visible = True
Table2Selected.BringToFront()
table2textdesc.BringToFront()
table2text.BringToFront()
ElseIf GlobalVariables.GlobalFlag = "3" Then
Table3Sleep.Visible = False
Table3Selected.Visible = True
Table3Selected.BringToFront()
table3text.BringToFront()
table3textdesc.BringToFront()
ElseIf GlobalVariables.GlobalFlag = "4" Then
Table4Selected.Visible = True
Table4Selected.BringToFront()
tabel4text.BringToFront()
table4textdesc.BringToFront()
table4sleep.Visible = False
ElseIf GlobalVariables.GlobalFlag = "5" Then
Table5Selected.Visible = True
table5sleep.Visible = False
Table5Selected.BringToFront()
table5text.BringToFront()
table5textdesc.BringToFront()
ElseIf GlobalVariables.GlobalFlag = "6" Then
table6sleep.Visible = False
Table6Selected.Visible = True
Table6Selected.BringToFront()
table6text.BringToFront()
table6textdesc.BringToFront()
Else
MessageBox.Show("Something went wrong. When you press 'OK', you will be taken back to the Main Menu.")
MainMenuForm.Show()
Me.Close()
End If
End Sub
Thanks :)

File is in Use VB.NET

I am using VB.NET (VS-2012), I use to scan documents in my applicant software. When i scan document i resize the scanned image and save it over the original file. When the user scan all the required documents then the use click the save button. After clicking the save button the application upload the images to database and try to delete the locally created files but stuck with the error that the file is in use.
I am using the following code...
Tw.Init(Me.Handle)
If (Not msgfilter) Then
Me.Enabled = False
msgfilter = True
Application.AddMessageFilter(Me)
End If
If SelectScanner Then
Tw.Select()
SelectScanner = False
End If
Tw.Acquire(chkScanner.Checked)
This function listens the event triggered by tw.acquire()
Public Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage
Try
Dim cmd As TwainCommand = Tw.PassMessage(m)
If (cmd = TwainCommand.Not) Then
Return False
End If
Select Case cmd
Case TwainCommand.CloseRequest
EndingScan()
Tw.CloseSrc()
Case TwainCommand.CloseOk
EndingScan()
Tw.CloseSrc()
Case TwainCommand.DeviceEvent
Case TwainCommand.TransferReady
Dim pics As ArrayList = Tw.TransferPictures()
EndingScan()
Tw.CloseSrc()
For i = 0 To pics.Count - 1 Step 1
flName = TempPath & "\" & GetNextFileName(TempPath, lstDocType.Items(cboDocTypes.SelectedIndex).ToString)
Dim img As IntPtr = CType(pics(i), IntPtr)
SaveImage(img)
lstDocuments.Items.Add(flName)
lstDocType.Items.Add(GetDocType(cboDocTypes.SelectedIndex))
SetItemData(lstDocuments, lstDocuments.Items.Count - 1, GetItemData(lstApplicants, lstApplicants.SelectedIndex))
Try
lstDocuments.SelectedIndex = lstDocuments.Items.Count - 1
Catch ex As Exception
End Try
Dim Original As Image = Image.FromFile(flName)
Dim Resized As Image = ResizeImage(Original, New Size(1024, 1407), False)
Original.Dispose()
Resized.Save(flName, ImageFormat.Jpeg)
Resized.Dispose()
img = Nothing
pics = Nothing
Next
Tw.Dispose()
Case 0
Return False
End Select
Return True
Catch ex As Exception
Return False
MsgBox(ex.Message)
End Try
End Function
In this given function you can see that the image is resized and the original image is replaced with the resized one. And then the resize object is disposed but when the software try to delete the image after uploading it to database, an error occurs.
I don't know whats the problem.

How to mark a radio button as checked with no action performed?

I have a form with CheckBox and RadioButton controls. When people click on the buttons/boxes it saves the results to my DB. Later, I might want someone else to retrieve the form and make changes or continue using it.
When I pull the info back from the database into a new "search" form (that looks exactly like the form used to submit the data), I want the radio buttons to reflect the state they were in when the form was saved. So if a box was TRUE (checked) when saved, I want it to show TRUE (checked) on the new form.
HOWEVER, when I use
radiobutton.checked = True
It DOES mark my button as checked.... it ALSO acts like the button was clicked again. So I have duplicate results in my database. It will keep doing this every time the form is opened.
So, I want a way to mark the box as checked (the state it was in when saved) but NOT CAUSE THE EVENT AGAIN.
Here's an example:
str = "SELECT * from OpenEvents WHERE EventID = " & eventId & ""
Dim cmd6 As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd6.ExecuteReader()
While dr.Read()
contactType = dr("ContactType").ToString
abend = dr("Abend").ToString
paged = dr("Paged").ToString
shortSummary = dr("ShortSummary").ToString
eventNotes = dr("EventNotes").ToString
impacting = dr("Impacting").ToString
L1Engaged = dr("L1Engaged").ToString
L2Engaged = dr("L2Engaged").ToString
managerEngaged = dr("ManagerEngaged")
IncTicket = dr("IncTicket").ToString
End While
myConnection.Close()
If contactType = "Call" Then
CallRadioButton.Checked = True
ElseIf contactType = "IM" Then
IMRadioButton.Checked = True
ElseIf contactType = "Other" Then
OtherRadioButton.Checked = True
ElseIf contactType = "Alert" Then
AlertRadioButton.Checked = True
Else
End If
If abend = "True" Then AbendCheckBox.Checked = True
If paged = "True" Then PagedYes.Checked = True
ShortSummaryTextBox.Text = shortSummary
DetailsTextBox.Text = eventNotes
If impacting = "True" Then ImpactingYesRadioButton.Checked = True
If L1Engaged = "True" Then L1YesRadioButton.Checked = True
If L2Engaged = "True" Then L2YesRadioButton.Checked = True
If managerEngaged = "True" Then ManagerYesRadioButton.Checked = True
IncTicketTextBox.Text = IncTicket
Just to turn it into an answer:
Create a boolean to be used at the form load event. Start it's value as true by default and when the load finishes, set it's value to false.
At every radio/check event you check the value, if it's true just exit the sub, otherwise do whatever it's supposed to do.
Best regards.
You could use Click event instead of CheckedChanged event.
Doing so, if you want to raise the event linked to the RadioButton you can use PerformClick instead of Checked = True.
If you want to change checked status without raising the event you can use Checked = True.
Here a little example:
Private Sub RadioButton1_Click(sender As Object, e As EventArgs) Handles RadioButton1.Click, RadioButton2.Click, RadioButton3.Click
Select Case sender.name
Case Is = "RadioButton1"
Me.Label1.BackgroundColor = Color.Yellow
Me.Label2.BackgroundColor = Color.Gray
Me.Label3.BackgroundColor = Color.Gray
Case Is = "RadioButton2"
Me.Label1.BackgroundColor = Color.Gray
Me.Label2.BackgroundColor = Color.Yellow
Me.Label3.BackgroundColor = Color.Gray
Case Is = "RadioButton3"
Me.Label1.BackgroundColor = Color.Gray
Me.Label2.BackgroundColor = Color.Gray
Me.Label3.BackgroundColor = Color.Yellow
End Select
End Sub
Private Sub Btn_ChangeSelected_Click(sender As Object, e As EventArgs) Handles Btn_ChangeSelected.Click
Me.RadioButton1.Checked = True
End Sub
Private Sub Btn_PerformClick_Click(sender As Object, e As EventArgs) Handles Btn_PerformClick.Click
Me.RadioButton1.PerformClick()
End Sub
If you click Btn_ChangeSelected RadioButton1 will be checked but no event will be raised.
If you click Btn_PerformClick RadioButton1 will be checked and RadioButton1.click event will be raised.
I would save the state of the radiobuttons at form closing event to my.settings and read all back on the form load event. This way you keep the last settings of your radiobuttons before exiting the program.
In my case, I ended up moving my code from
_CheckedChanged
event, that was the default when clicking on the control from design view, to a
_MouseClick
event. Solved all my problems. Seems it was a bit silly that was the default, but lessons learned. And all working as I need now.
Thanks for all the input!!!

MS-Access hidding and showing a button conditionally through vba

I am developing a quality control system for my company, and I i want to connect it to a label printer, which is already taken care off, the problem now is with the button itself.
I want the print label button to only be enabled and visible after the whole check was made, what i've got now is this:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Button_label.Visible = False
Button_label.Enabled = False
End Sub
Private Sub Motor_OK_Change()
Dim ok As Boolean
ok = Motor_OK.Value
If ok = 1 Then
Button_label.Visible = True
Button_label.Enabled = True
End If
End Sub
It does work on conceiling the button, but it fails when trying to enable it again and making it visible upon making the check. It's important to refer that I have tried using If ok = True instead of If ok= 1, I dont know how important that is.
Thanks
You are not testing for both conditions (ok=0), perhaps the following will work:
Replace:
If ok = 1 Then
Button_label.Visible = True
Button_label.Enabled = True
End If
With:
Button_label.Visible = ok
Button_label.Enabled = ok
New Solution:
Instead of using the Form Before_Update event, use Form_Current event. If there is only 1 record, you can also use the Form_Load event. I would also suggest that you remove the Visible property and use only the Enabled one so users can see their is print functionality.
Private Sub Form_Current()
'button_Label.Visible = False
button_Label.Enabled = False
End Sub
Instead of using the Motor_OK Before_Updateevent, use After_Update event
Private Sub Motor_OK_AfterUpdate()
Dim ok As Boolean ' defaults to False
If IsNumeric(Motor_OK.Value) Then ok = True ' remove this statement if Motor_OM is
' alpha numeric.
If Not IsNull(Motor_OK.Value) Then ok = True ' otherwise following statement
' to avoid NULL error
'button_Label.Visible = ok
button_Label.Enabled = ok
End Sub
Let me know if I can be of further help.