I can fill a value of a specific field with the code below
nav.EvaluateScriptAsync("document.getElementById('email').value='teste';")
but I can't return the value of this field
Dim placeholder As Task(Of JavascriptResponse) = nav.EvaluateScriptAsync("document.getElementById('email').value")
Task.WaitAll()
MsgBox(placeholder.Result.ToString)
both (placeholder.Result.ToString) and (placeholder.ToString) return "CefSharp.JavascriptResponse"
You can obtain the value from JavascriptResponse.Result property. Check the Success property first, if the script evaluation failed the error will be contained with the Message property.
http://cefsharp.github.io/api/97.1.x/html/Properties_T_CefSharp_JavascriptResponse.htm
I got it that way.
Public Async Function testeAsync() As Task(Of String)
Dim placeholder2 As JavascriptResponse = Await nav.EvaluateScriptAsync("document.getElementById('email').placeholder")
If (placeholder2.Success) Then
MsgBox(placeholder2.Result)
End If
End Function
But when I do a return in the function and call the function in a struct, how do I call waiting for the async function to complete?
This way the sub prints the result before the asynchronous process ends.
Sub testeResultado()
Dim returnedTaskTResult As Task(Of String) = testeAsync()
MsgBox(returnedTaskTResult.ToString)
End Sub
Related
I am trying to parse through some xml files and return the results to a datagrid in UWA. The code builds fine but when running it returns
Unable to cast object of type 'System.Threading.Tasks.Task1[System.Collections.Generic.List1[Festplatten_Archiv_Client.Drive]]' to type 'System.Collections.IEnumerable'.
In my XAML.vb I am only calling the class to create the files and set the results as fileSource:
Public Sub New()
InitializeComponent()
dataGrid.ItemsSource = Drive.Drives
End Sub
Which works fine if I only add a sample Drive with
drivelist.Add(New Drive("Name",0, 0, 0), "location", "date"))
But as I want to parse through the XMLs, this is my code.
This is my drives class:
Public Shared Async Function Drives() As Task(Of List(Of Drive))
Dim drivelist As New List(Of Drive)
Dim folderpicked As StorageFolder
Try
folderpicked = Await StorageApplicationPermissions.FutureAccessList.GetItemAsync(ReadSetting("folderStorageToken"))
Catch ex As Exception
Debug.WriteLine("Fehler: " & ex.Message)
folderpicked = Nothing
End Try
Dim xmlfiles As List(Of StorageFile) = Await folderpicked.GetFilesAsync()
For Each file In xmlfiles
''Process files
Next
Return Await Task.Run(Function() drivelist)
End Function
It might be something with async programming, but I am very new to this. Thanks for any help!
You can make a blocking call to an Async routine from the ctor as follows:
Dim drivesResult = Drives().GetAwaiter().GetResult()
This is effectively forcing the routine to execute synchronously. If that's not what you want, then you'll need to explore a different alternative, e.g. the suggestion in the comments.
So this is the method I would like to Mock:
Function ReturnPasswordHistoryAsync(passwordChangeHistory As PasswordChangeHistory) As Task(Of IList(Of PasswordChangeHistory))
And this is my unit test:
<TestMethod()> Public Async Function ValidateNewPassword_NewPasswordHasAlreadyBeenUsed_PasswordIsNotValid() As Task
'Arrange
Dim newPass = "newPass"
Dim oldPass = "oldPass"
Dim confirmPass = newPass
Dim passwordValid As PasswordValid
Dim ret As Task(Of IList(Of PasswordChangeHistory))
'Errors here with null object reference error
_mockChangePasswordRepo.Setup(Function(x) x.ReturnPasswordHistoryAsync(New PasswordChangeHistory())).Returns(ret)
'Act
passwordValid = Await _changePasswordManager.ValidateNewPassword(oldPass, newPass, confirmPass, Nothing)
'Assert
Assert.IsFalse(passwordValid.IsValid, "New password and old passord equal one another yet the test passed")
End Function
I believe it is erroring due to the return, but I'm unsure how to mock the return type. Does anyone know how to mock the return type of Task(Of IList(Of PasswordChangeHistory))
looks like you are using Moq. for mocking async return values you can use ReturnsAsync
Dim ret As IList(Of PasswordChangeHistory) = New List(Of PasswordChangeHistory)
'Setup member to expect any value of PasswordChangeHistory and return a list after an async call
_mockChangePasswordRepo.Setup(Function(x) x.ReturnPasswordHistoryAsync(It.IsAny(Of PasswordChangeHistory))).ReturnsAsync(ret)
This will allow the mock to flow through Async/Await calls.
If (results Is Nothing And results.Count > 0) Then
Return False
End If
Turns out that it's actually this IF statement. Results was Nothing and so I thought that the IF would've evaluated the second side and if it returned false, then skipped the second part. But it doesn't, so the count was throwing an exception.
So this is the fix:
If results IsNot Nothing OrElse results.Count = 0 Then
Return False
End If
And - Evaluates both sides
AndElse - One Side then determines is the IF is valid
Similar with Or and OrElse.
Coming from C# to VB is strange, I miss my syntactic sugar
I would like to start a background task without using the Await keyword. Instead I want to monitor the task at various points and restart it when necessary to update information in the background. Here is the method I am trying to call:
Public Async Function UpdateVehicleSummaries(p_vehicleID As Int32) As Task(Of Boolean)
Dim tempVehicle As Koolsoft.MARS.BusinessObjects.Vehicle
For Each tempVehicle In Vehicles
If p_vehicleID = 0 Or p_vehicleID = tempVehicle.VehicleID Then
Await UpdateVehicleStats(tempVehicle)
End If
Next
Return True
End Function
The code I am trying to start the task doesn't seem to work and I'm not sure how to provide the parameter. I get an error that "Task(Of Boolean) cannot be converted to System.Action and or an error on the parameter"
Dim tempTask As Task
tempTask = New Task(UpdateVehicleSummaries(tempVehicleID))
tempTask.Start()
Any help would be appreciated.
Since UpdateVehicleSummaries is already asynchronous, you should be abel to just do:
Dim tempTask As Task(Of Boolean) = UpdateVehicleSummaries(tempVehicleID)
The returned Task(Of T) will be "hot" (running), but shouldn't block, as the Await call will immediately return control flow to the caller at that point.
A more typical use of this method, if you need to perform other work while this runs, would be to do the following:
Dim tempTask = UpdateVehicleSummaries(tempVehicleID)
' Do your other work
Dim success = Await tempTask ' Get the boolean result asynchronously...
' use the result
I have been trying to get a function to work asynchronously with a GUI window, and having very limited success.
The code below is my function, XXX, with a delegate and Callback - to allow stopping the GUI thread, without getting an error.
It works though - if it is a Sub, not a function, and if it doesn't have any arguments.
I don't know how to change it so I can check its return value... I have found a little help in some examples, http://msdn.microsoft.com/en-us/library/system.iasyncresult.asyncstate.aspx - but they print the return value in the callback, and I can't see how to get it in the caller.
I can't find any way to use arguments in my function.
Private Function XXX_Callback(ByVal ia As IAsyncResult)
Dim d As XXXDelegate = CType(CType(ia, Runtime.Remoting.Messaging.AsyncResult).AsyncDelegate, XXXDelegate)
d.EndInvoke(ia)
Dim result As AsyncResult = CType(ia, AsyncResult)
Dim caller As XXXDelegate = CType(result.AsyncDelegate, XXXDelegate)
Dim returnValue As Boolean = caller.EndInvoke(ia)
XXX_Finish() ' needs the "vvv argument, I don't know how to get it
' The returnValue is here but I don't know how to send it to the caller
End Function
'Private Function XXX_Finish(ByVal vvv as Boolean) As Boolean
' this probably needs to return something, I don't know what/ how to get it
Private Function XXX_Finish() As Boolean
' something
myGui.Finish()
End Function
' Private Delegate Function XXXDelegate(ByVal vvv As Integer) As Boolean
' Public Function XXX(ByVal vvv As Integer) As Boolean ' This is what I would like
Private Delegate Sub XXXDelegate()
Public Sub XXX()
'
myGui.Update()
'
End Sub
Public Sub Caller()
'
myGui = New SomeGui()
myGui.Begin()
Dim t As New XXXDelegate(AddressOf XXX)
t.BeginInvoke(AddressOf XXX_Callback, Nothing)
' more code, another call
End Sub
Private myGui As SomeGui
Please, could someone help me get this into a better shape, or get some examples that will help ? With the ones I have found in the past two days, I have reached a dead end...
Thank you.
After searching through lots of posts on the web, and reading a couple of books, I have found the best resource, with examples for each case that one might encounter:
http://support.microsoft.com/kb/315582
The answers to this particular question are in the above link, "Sample 2: Calling A Method Asynchronously by Using the EndInvoke() Call Pattern" and "Sample 5: Executing a Callback When an Asynchronous Method Completes."
The best thing about it though is the simple and organized fashion in which the options to use Invoke, EndInvoke, Callback are explained.
One thing to note: for my question above, the essential breakthrough was when I read the words "BeginInvoke() returns immediately and does not wait for the asynchronous call to complete."
So... trying to call another method, after it, was the wrong approach.
This is how i made an asyncronous function calls.
I declare the function in the module.
Private Delegate Function fnBolAsyncCallVerificacionNuevaVersion(ByVal pIntModo As Integer, ByVal pIntAccion As Integer) As Boolean
Private Delegate Function fnBolAsyncCallActualizacionTipoCambio(ByVal pIntActualizacionMandatoria As clsBusinessBoxData.tblTipoCambio.enumActualizacionMandatoria) As typBolResultadoFuncion
The functions receive some parameters and the first returns a boolean and the second returns an structure with 2 data, one boolean and one string.
In the load event of the form I call the functions.
sAsyncVerificaVersion = New fnBolAsyncCallVerificacionNuevaVersion(AddressOf fnBolVerificaActualizacion)
sAsyncVerificaVersion.BeginInvoke(enumDisplayGUIMode.Silent, typApplicationUpdate.CheckOnly, New AsyncCallback(AddressOf fnBolTerminaVerificacionVersion), Nothing)
sAsyncActualizaTiposCambio = New fnBolAsyncCallActualizacionTipoCambio(AddressOf fnBolActualizaTiposCambioYahoo)
sAsyncActualizaTiposCambio.BeginInvoke(clsBusinessBoxData.tblTipoCambio.enumActualizacionMandatoria.No, New AsyncCallback(AddressOf fnBolTerminaActualizacionTipoCambio), Nothing)
These calls will execute the function asyncronous and will callback the function defined when finished.
Then i receive the results in the callback functions defined.
Private Function fnBolTerminaVerificacionVersion(ByVal pIarResultado As IAsyncResult) As Boolean
Dim sClsResultado = CType(pIarResultado, Messaging.AsyncResult)
Dim sIarResultado As fnBolAsyncCallVerificacionNuevaVersion = CType(sClsResultado.AsyncDelegate, fnBolAsyncCallVerificacionNuevaVersion)
Dim sBolExisteNuevaVersion As Boolean = False
CheckForIllegalCrossThreadCalls = False
sBolExisteNuevaVersion = sIarResultado.EndInvoke(pIarResultado)
mnuBajarActualizacion.Enabled = sBolExisteNuevaVersion
CheckForIllegalCrossThreadCalls = True
Return True
End Function
Private Function fnBolTerminaActualizacionTipoCambio(ByVal pIarResultado As IAsyncResult) As Boolean
Dim sBolActualizacionExitosa As typBolResultadoFuncion
Dim sClsResultado = CType(pIarResultado, Messaging.AsyncResult)
Dim sIarResultado As fnBolAsyncCallActualizacionTipoCambio = CType(sClsResultado.AsyncDelegate, fnBolAsyncCallActualizacionTipoCambio)
CheckForIllegalCrossThreadCalls = False
sBolActualizacionExitosa = sIarResultado.EndInvoke(pIarResultado)
CheckForIllegalCrossThreadCalls = True
Return True
End Function
I hope they help.
I want to do something like the following in VB.NET, is it possible?
Function task(value as Object, toType as Type)
Return DirectCast(value, toType)
End Function
Yes. There is System.Type. You may actually want to do a Generic however.
Function SomeFunction(Of T)(obj As Object) As T
'' Magic
End Function
Great Answer - Here is a generic function to do the same:
Public Sub BindListControlToEnum(Of T)(ListCtrl As ListControl)
Dim itemValues As Array = System.Enum.GetValues(GetType(T))
Dim itemNames As Array = System.Enum.GetNames(GetType(T))
For i As Integer = 0 To itemNames.Length - 1
Dim item As New ListItem(itemNames(i), itemValues(i))
ListCtrl.Items.Add(item)
Next
End Sub
Call it like this:
BindDropdownToEnum(Of MyClass.MyEnum)(MyRadioButtonListControl)
you want to use the
function task(of myType)(value as myType) as MyType
''stuff
return value
end function
Yes, though, depending on your requirements, you may want to use CType to do any type casting/conversion. CType will work so long as there is a valid type conversion, whereas DirectCast requires that value be of the type toType.
I had to do something similar today (essentially filling in the '' magic from the accepted answer):
Private Function Convert_Value_Or_Fallback(Of T)(ByRef value As Object, ByRef fallback As Object) As Object
Try
Return DirectCast(value, T)
Catch ex As Exception
Return fallback
End Try
End Function
'call it like this:'
Convert_Value_Or_Fallback(Of Double)(value, 0)