使用Microsoft.Bcl.Async将.NET 4.5中的wait/Async转换为Visual Studio 2013中的.NET 4.0
虽然我有一些脚本编写经验(VBscript、PowerShell、batch、shell等),但我不是程序员。我只是请求你善待我使用Microsoft.Bcl.Async将.NET 4.5中的wait/Async转换为Visual Studio 2013中的.NET 4.0,.net,vb.net,.net-4.0,visual-studio-2013,async-await,.net,Vb.net,.net 4.0,Visual Studio 2013,Async Await,虽然我有一些脚本编写经验(VBscript、PowerShell、batch、shell等),但我不是程序员。我只是请求你善待我 简短: 由于VB.NET非常新,在将Microsoft.Bcl.Async纳入项目后,我需要帮助将Await/Async.NET 4.5代码重新写入与.NET 4.0兼容的代码。下面是修剪后的.NET 4.5工作代码: Imports System Imports System.IO Imports System.Diagnostics Imports System.
简短:
由于VB.NET非常新,在将Microsoft.Bcl.Async纳入项目后,我需要帮助将Await/Async.NET 4.5代码重新写入与.NET 4.0兼容的代码。下面是修剪后的.NET 4.5工作代码:
Imports System
Imports System.IO
Imports System.Diagnostics
Imports System.Security.Principal
Private Sub buttonName_Click(sender As Object, e As EventArgs) Handles buttonName.Click
// Do a bunch of stuff
// like assign values of text boxes to variables
// validate input to a certain degree
// Run command
RunProcess("someexe.exe", "plenty of argments")
End Sub
Private Async Sub RunProcess(ByVal Command As String, Optional ByVal Arguments As String = "NOTSET")
// Function to disable all the fields and buttons
LockUI()
// Sow the progress bar
// Start the progress marquee so people know something's happening
ProgressBar1.Visible = True
ProgressBar1.Style = ProgressBarStyle.Marquee
ProgressBar1.MarqueeAnimationSpeed = 60
ProgressBar1.Refresh()
// Prepare process
Dim Execute As Process
If Arguments = "NOTSET" Then
Execute = Process.Start(Command)
Else
Execute = Process.Start(Command, Arguments)
End If
// Run the process and wait for it to finish
Await Task.Run(Sub() Execute.WaitForExit())
// Do some other stuff like check return code etc.
// Display positive msgbox for success
// Display failure message box for, well, failures
// Hide progress bar since we're done
ProgressBar1.Visible = False
// Unlock all the fields
UnlockUI()
End Sub
长:
我在Visual Studio Premium 2013中为一个仅控制台/基于命令行的应用程序编写了一个非常简单的GUI包装器。它只不过是一个VB Windows窗体应用程序,有几个文本框供用户输入,还有两个执行操作的按钮。当按下任一按钮时,它将使用从文本框中提取的参数执行命令,并在运行时显示一个字幕进度条
它工作得很好,我很激动,非常兴奋。然而,我刚刚了解到,将在上使用的机器只有.NET 4.0,没有时间推出.NET 4.5。我知道在我的项目中可以在哪里更改目标框架,但是在查看了一些资源(,)之后,我不确定如何重新编写代码以使用Microsoft.Bcl.Async。在
Microsoft.Bcl.Async
NuGet包中,许多成员被放置在TaskEx
类型上(当然,因为NuGet包不能更改任务
类型)
在您的情况下,您可以将Task.Run
更改为TaskEx.Run
如果您需要更多建议,
Async Sub
方法只能用作事件处理程序。因此,将RunProcess
定义为返回任务
,并让它从Async Sub按钮name\u单击
等待。有关更多信息,请参阅my.代码是否编译?如果否,请说明原因错误在哪里?