2014-10-03 1 views
0

스크립팅 경험 (VBscript, PowerShell, 배치, 셸 등)을 사용하는 동안 프로그래머가 아닙니다. . 나는 네가 친절 해달라고 부탁한다.

Microsoft.Bcl.Async가 포함 된 Visual Studio 2013의 .NET 4.0으로 Await/Async를 변환하십시오.

짧은 :
VB.NET 매우 새로운이기 때문에, 나는이 프로젝트에 Microsoft.Bcl.Async를 포함 한 후 .NET 4.0 호환 코드로 도움을 재 작성 기다리고 있습니다/비동기 .NET 4.5 코드가 필요합니다. 여기에 손질의 작업 .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 


는 :
나는 비주얼 스튜디오 프리미엄 2013 년 콘솔 전용/명령 줄 기반 응용 프로그램에 대한 매우 간단한 GUI 래퍼를 썼다. 실제로는 사용자 입력을위한 몇 개의 텍스트 상자와 동작을 수행하는 2 개의 버튼이있는 VB Windows Form 응용 프로그램 이상입니다. 두 버튼 중 하나를 누르면 텍스트 상자에서 가져온 매개 변수로 명령을 실행하고 실행 중일 때 선택 윤곽 진행 막대를 표시합니다 (something I needed help with recently).

위대한 작품, 전율이 매우 높고 매우 thankful입니다. 그러나, 방금 사용한 컴퓨터에 에는 .NET 4.0 만 있고 .NET 4.5는 출시되지 않습니다. 내 프로젝트에서 대상 프레임 워크를 변경할 수있는 곳을 볼 수 있지만 일부 리소스 (Link 1, Link 2, Link 3)를 살펴본 후에 Microsoft.Bcl.Async를 사용하여 코드를 다시 작성하는 방법을 모르겠습니다.

+1

코드가 컴파일됩니까? 그렇지 않은 경우 오류는 무엇입니까? – svick

답변

2

Microsoft.Bcl.Async NuGet 패키지에서 많은 구성원이 TaskEx 유형에 배치되었습니다 (물론 NuGet 패키지는 Task 유형을 변경할 수 없으므로).

귀하의 경우 Task.RunTaskEx.Run으로 변경할 수 있습니다.

더 많은 조언을 원하면 Async Sub 메서드는 이벤트 처리기로만 사용해야합니다. 따라서 RunProcessTask으로 반환하고 AwaitAsync Sub buttonName_Click에서 가져 오는 것이 더 적절합니다. 자세한 내용은 내 MSDN article on async best practices을 참조하십시오.

관련 문제