I'm trying to get my code to run when the page is finished loading in CefSharp, but when I plug my code into the CefSharp event in any way it won't work and the program will not responding.
This is my code:
Private Sub Web_FrameLoadEnd(sender As Object, e As FrameLoadEndEventArgs) Handles Web.FrameLoadEnd
If Login = False Then
Try
Web.ExecuteScriptAsync("document.getElementById('txt_Username').value='user'")
Web.ExecuteScriptAsync("document.getElementById('txt_Password').value='pass'")
Web.ExecuteScriptAsync("document.getElementById('loginbutton').click()")
Login = True
Catch ex As Exception
Login = False
End Try
Else
If Not Web.Address.Contains("bbsp") Then
Web.Load("http://192.168.1.1/html/bbsp/userdevinfo/userdevinfosmart.asp?type=wifidev")
Else
GetNames()
End If
End If
End Sub
Private Sub GetNames()
Try
For B As Int32 = List1.Items.Count To 0 Step -1
List1.Items.RemoveAt(B)
Next
Catch ex As Exception
End Try
Dim i = 0
Do
Try
Dim IDname = "divDevName_" & i
Dim name = Web.EvaluateScriptAsync("document.getElementById('" & IDname & "').innerHTML")
Dim response = name.Result
If response.Success = True Then
List1.Items.Add(i & "- " & response.Result.ToString)
Else
Exit Do
End If
Catch ex As Exception
Exit Do
End Try
i += 1
Loop
End Sub
But when I run GetNames() with a button it works perfectly,
where did I go wrong?
Related
I am trying to make a button visible only if a key (example Control) is press down.
My code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 17 Then
Me.btn1.Visible = True
Else
Me.btn1.Visible = False
End If
End Sub
Private Sub Form_Load()
Me.btn1.Visible = False
End Sub
I need to have visible and active the button only when the CONTROL key is press down and the user click on btn1.
Thank you.
Update Code:
Option Compare Database
Option Explicit
Private Sub btnHide_Click()
DoCmd.Close acForm, "frmDemo", acSaveYes
End Sub
Private Sub btnFake_KeyDown(KeyCode As Integer, Shift As Integer)
On Error GoTo Err_Handler
If vbKeyControl = 17 Then
Me.btnHide.Visible = True
FormTimer
Me.btnFake.SetFocus
Me.btnHide.Visible = False
Else
Me.btnHide.Visible = False
End If
Exit_This_Sub:
Exit Sub
Err_Handler:
If Err = 2467 Then '<== Form is closed.
Resume Exit_This_Sub
Else
MsgBox "Error #: " & Err.Number & " " & Err.Description
End If
Resume Exit_This_Sub
End Sub
Private Sub Form_Load()
Me.btnHide.Visible = False
Me.btnFake.SetFocus
End Sub
Sub FormTimer()
Dim PauseTime, Start, Finish, TotalTime
PauseTime = 0.1 ' Set duration.
Start = Timer ' Set start time.
Do While Timer < Start + PauseTime
DoEvents ' Yield to other processes.
Loop
End Sub
I test it and it is working fine. Simple code.
If CONTROL is press btnHide is visible for 0.1sec.
Any ideas?
I have created a class based on this msdn page the only problem my process.exited event is never reached.
The .exe for the process is a console application that I have created. And it closes it self when it's done. But my event does not get reached and I have to wait 30s for the code to resume.
Is there something that i am doing wrong? For my test case the .exe only takes +- 5 seconds to complete.
I assume when a console application closes after the code is done the process is exited. My this assumption is wrong?
Imports System
Imports System.Diagnostics
Imports System.Threading
Public Class CopyDesignProcessClass
Private WithEvents oProcess As New Process()
Private elapsedTime As Integer
Private eventHandled As Boolean
Public Event Exited As EventHandler
Shared Sub Start(ByVal oArgument As String)
' Verify that an argument has been entered.
If oArgument Is Nothing Or oArgument = "" Then
Exit Sub
End If
' Create the process and copy the design.
Dim myCopyDesignProcess As New CopyDesignProcessClass
myCopyDesignProcess.CopyDesign(oArgument)
End Sub
' Print a file with any known extension.
Sub CopyDesign(ByVal oArgument As String)
elapsedTime = 0
eventHandled = False
Try
' Start a process to copy a design and raise an event when done.
oProcess.StartInfo.FileName = "C:\Program Files\Autodesk\CopyDesign Tool\CopyDesignTool.exe"
oProcess.StartInfo.Arguments = oArgument
oProcess.Start()
Catch ex As Exception
MsgBox("An error occurred trying to copydesign " & oArgument & " :" &
vbCrLf & ex.Message, MsgBoxStyle.Exclamation, "CopyDesign Failed")
Return
End Try
' Wait for Exited event, but not more than 30 seconds.
Const SLEEP_AMOUNT As Integer = 100
Do While Not eventHandled
elapsedTime += SLEEP_AMOUNT
If elapsedTime > 30000 Then
Exit Do
End If
Thread.Sleep(SLEEP_AMOUNT)
Loop
End Sub
' Handle Exited event and display process information.
Private Sub myProcess_Exited(ByVal sender As Object,
ByVal e As System.EventArgs) Handles oProcess.Exited
eventHandled = True
Debug.Print("Exit time: {0}" & vbCrLf &
"Exit code: {1}" & vbCrLf &
"Elapsed time: {2}",
oProcess.ExitTime, oProcess.ExitCode, elapsedTime)
End Sub
End Class
Additional:
The answer may be given in this post but it's in c# and this I don't understand.
c# possible answer
Corrected code, but used another solution posted as answer.
Imports System
Imports System.Diagnostics
Imports System.Threading
Public Class CopyDesignProcessClass
Private WithEvents oProcess As New Process()
Private elapsedTime As Integer
Private eventHandled As Boolean
Public Event Exited As EventHandler
Shared Sub Start(ByVal oArgument As String)
' Verify that an argument has been entered.
If oArgument Is Nothing Or oArgument = "" Then
Exit Sub
End If
' Create the process and copy the design.
Dim myCopyDesignProcess As New CopyDesignProcessClass
myCopyDesignProcess.CopyDesign(oArgument)
End Sub
' Print a file with any known extension.
Sub CopyDesign(ByVal oArgument As String)
elapsedTime = 0
eventHandled = False
Try
' Start a process to copy a design and raise an event when done.
oProcess.StartInfo.FileName = "C:\Program Files\Autodesk\CopyDesign Tool\CopyDesignTool.exe"
oProcess.StartInfo.Arguments = oArgument
oProcess.EnableRaisingEvents = True
oProcess.Start()
Catch ex As Exception
MsgBox("An error occurred trying to copydesign " & oArgument & " :" &
vbCrLf & ex.Message, MsgBoxStyle.Exclamation, "CopyDesign Failed")
Return
End Try
' Wait for Exited event, but not more than 30 seconds.
Const SLEEP_AMOUNT As Integer = 100
Do While Not eventHandled
elapsedTime += SLEEP_AMOUNT
If elapsedTime > 30000 Then
Exit Do
End If
Thread.Sleep(SLEEP_AMOUNT)
Loop
End Sub
' Handle Exited event and display process information.
Private Sub myProcess_Exited(ByVal sender As Object,
ByVal e As System.EventArgs) Handles oProcess.Exited
eventHandled = True
Debug.Print("Exit time: {0}" & vbCrLf &
"Exit code: {1}" & vbCrLf &
"Elapsed time: {2}",
oProcess.ExitTime, oProcess.ExitCode, elapsedTime)
End Sub
End Class
you can replace this part of the code:
Try
' Start a process to copy a design and raise an event when done.
oProcess.StartInfo.FileName = "C:\Program Files\Autodesk\CopyDesign Tool\CopyDesignTool.exe"
oProcess.StartInfo.Arguments = oArgument
oProcess.Start()
Catch ex As Exception
MsgBox("An error occurred trying to copydesign " & oArgument & " :" &
vbCrLf & ex.Message, MsgBoxStyle.Exclamation, "CopyDesign Failed")
Return
End Try
' Wait for Exited event, but not more than 30 seconds.
Const SLEEP_AMOUNT As Integer = 100
Do While Not eventHandled
elapsedTime += SLEEP_AMOUNT
If elapsedTime > 30000 Then
Exit Do
End If
Thread.Sleep(SLEEP_AMOUNT)
Loop
with:
Try
' Start a process to copy a design and raise an event when done.
oProcess.StartInfo.FileName = "C:\Program Files\Autodesk\CopyDesign Tool\CopyDesignTool.exe"
oProcess.StartInfo.Arguments = oArgument
oProcess.Start()
oProcess.WaitForExit()
Catch ex As Exception
MsgBox("An error occurred trying to copydesign " & oArgument & " :" &
vbCrLf & ex.Message, MsgBoxStyle.Exclamation, "CopyDesign Failed")
Return
End Try
waitforexit method is very useful, it will freeze your code until given software is done. If running longer processes background worker may be necessary.
I am creating a visual basic application for a friend. Anyway I am trying to create a function that returns "exit sub" to the sub calling the function. I have seen some ways around this by returning a value like 1 or 2 and inserting a if in calling sub. just wondering if there is a short hand for returning exit sub that I haven't learned yet.
Private Sub Button1.click() Handles Button1.Click
tryactive()
endsub
Private Function tryactive()
Try
AppActivate("your aplication")
Catch ex As Exception
Dim msgboxresponse = MsgBox("please start your application", 0, "Can't find your application")
If msgboxresponse = MsgBoxResult.Ok Then
Exit Sub <------ this is the problem i want to send this back to calling sub
End If
End Try
End Function
Code is much bigger and a lot more buttons. That's why i'm asking if there's a better way to do this. Any help is appreciated.
First of all, you cannot use an Exit Sub inside a Function. It should be an Exit Function. But based on what you want to happen (I guess), try this.
Private Sub Button1_Click() Handles Button1.Click
If TryActive() = False Then
Exit Sub
End If
'Your code you want to execute if TryActive() is True
End Sub
Private Function TryActive() as Boolean
Try
AppActivate("your aplication")
Return True
Catch ex As Exception
Dim msgboxresponse = MsgBox("please start your application", 0, "Can't find your application")
If msgboxresponse = MsgBoxResult.Ok Then
Return False
End If
End Try
End Function
I have a BackgroundWorker that includes a class ExcelOutput, used to output various data to a workbook, and I should mention straight away that bw.WorkerSupportsCancellation = True is set.
At each stage of the output I'm checking for errors in ExcelOutput using Try/Catch, and if necessary displaying an error (using a function called ErroReport().
In conjunction with the error message, I want to cancel the BackgroundWorker to avoid further errors. To that end I have added the OutputWorker property to the ExcelOutput class and I set that to be a copy of my BackgroundWorker in the bw_DoWork() method.
However, the cancellation carried out in ExcelOutput.ErroReport() is not working, and I don't know why.
Note that I've tested the value of bw.CancellationPending and it is set to True after an error. I've also tested that the If condition following is working by showing a message box, and that also works. For some reason it seems as though the Exit Sub command is ignored though.
Can anyone suggest what I am doing wrong? Thanks.
Here is how the bw_DoWork() function from the BackgroundWorker class is set up -
Private Sub bw_DoWork(ByVal sender As Object,
ByVal e As DoWorkEventArgs)
Dim Excel As New ExcelOutput ' Create a new instance of the ExcelOutput class
Dim CurrentRow As Integer = 4 ' Set the first output row
'** Include a copy of the OutputWorker in the ExcelOutput (so that the OutputWorker can be cancelled)
Excel.OutputWorker = Me
If bw.CancellationPending = True Then
e.Cancel = True
Exit Sub
Else
Excel.Prepare()
End If
If bw.CancellationPending = True Then
e.Cancel = True
Exit Sub
Else
CurrentRow = Excel.OutputGroup("General", Headers, Data, 4)
End If
' More stuff here...
End Sub
Here is how the ErrorReport() function from the ExcelOutput class is set up -
Private Sub ErrorReport(ByVal Ex As Exception,
Optional ByVal CustomMessage As String = "")
Call Me.ResetRange() ' Destroy the 'Range' object
Dim ErrorMessage As String = "Message: " & Ex.Message ' Set the default message
If CustomMessage <> "" Then ErrorMessage = CustomMessage & vbCrLf & vbCrLf & Ex.Message
Dim Result As Integer = MessageBox.Show(ErrorMessage,
"An Error Has Occured",
MessageBoxButtons.OK,
MessageBoxIcon.Stop)
'** Close the workbook (if it's open) and stop the OutputWorker *'
Try
Call Me.WB.Close(SaveChanges:=False)
If Me.OutputWorker.WorkerSupportsCancellation = True Then
Me.OutputWorker.CancelAsync()
End If
Catch
End Try
End Sub
You should try to add the DoWorkEventsArgs as parameter to your ErrorReport function.
Private Sub ErrorReport(ByVal Ex As Exception,
Optional ByVal CustomMessage As String = "",
ByVal e As DoWorkEventsArgs)
Call Me.WB.Close(SaveChanges:=False)
If e.WorkerSupportsCancellation = True Then
e.CancelAsync()
End If
You'll be able to cancel the Backgroundworker.
I am making a directory cleaner that uses the following code
Label2.Text = "Cleaned."
If CheckBox1.Checked = True Then
On Error Resume Next
Kill("C:\Nexon\Combat Arms\*.txt")
End If
If CheckBox2.Checked = True Then
On Error Resume Next
Kill("C:\Nexon\Combat Arms\*.jpg")
End If
If CheckBox3.Checked = True Then
On Error Resume Next
Kill("C:\Nexon\Combat Arms\*.v3d")
End If
If CheckBox4.Checked = True Then
On Error Resume Next
Kill("C:\Nexon\Combat Arms\*.bin")
End If
If CheckBox5.Checked = True Then
On Error Resume Next
Kill("C:\Nexon\Combat Arms\*.dmp")
End If
If CheckBox6.Checked = True Then
On Error Resume Next
Kill("C:\Nexon\Combat Arms\*.dump")
End If
If CheckBox7.Checked = True Then
On Error Resume Next
System.IO.File.SetAttributes("C:\Nexon\Combat Arms\EndingBanner.exe", System.IO.FileAttributes.Normal)
Kill("C:\Nexon\Combat Arms\EndingBanner.exe")
End If
If CheckBox8.Checked = True Then
On Error Resume Next
System.IO.File.SetAttributes("C:\Nexon\Combat Arms\CAV.exe", System.IO.FileAttributes.Normal)
Kill("C:\Nexon\Combat Arms\CAV.exe")
End If
If CheckBox9.Checked = True Then
On Error Resume Next
If Dir("C:\Nexon\Combat Arms\MOVIES") <> "" Then
Rename("C:\Nexon\Combat Arms\Game\MOVIES", "C:\Nexon\Combat Arms\Game\CLEANED")
End If
End If
But instead of stating the directory in the program I want the user to select it , "C:\Nexon\Combat Arms\" using SelectDirectory; I have this but, cant make it work
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim MyFolderBrowser As New System.Windows.Forms.FolderBrowserDialog
Dim dlgResult As DialogResult = MyFolderBrowser.ShowDialog()
Me.FileReference.Text = MyFolderBrowser.SelectedPath
End Sub
The folderbrowser code works fine. Are you having a problem implementing this?
Kill(Me.FileReference.Text & "\*.txt")
Try using a message box to display the selected path. See if is giving you what you want. If this isn't working try another property like selected directory or sorts. I don't have VS open right now to look myself but what you have should work as long as selected path is right. I have a feeling that if you got the selected path if that is what you want and assign it to a string variable first, it might work. Could be a formatting problem with the path. Just try the message box first to see if the path is right.