Как асинхронно выполнить функцию? - PullRequest
0 голосов
/ 15 января 2020

Я работаю над приложением vb. net и изо всех сил пытаюсь заставить эту функцию работать параллельно. Это устаревшее приложение, мне сказали, что оно использует асинхронный шаблон на основе событий, но я не совсем понимаю, что это значит для этой проблемы.

Существует функция для l oop, которая выполняет функцию последовательно, но я бы хотел запустить ее параллельно.

    Public Function Execute() As Boolean

        Dim ret As Boolean = True

        ' If output files are not specified, then they will be generated based on the study name
        If CancelExecution Then Return CancelCleanUp()

        If ValidateInputFiles() Then
            If CancelExecution Then Return CancelCleanUp()

            Select Case _Name
                Case "Main Single"
                    ' This is the for loop I would like to run in parallel
                    For Each cc As IOFile In _CCards
                        If ValidateOutputFiles(cc.FileName) Then
                            ret = RunModel(cc)
                            If CancelExecution Then Return CancelCleanUp()
                        Else
                            ret = False
                        End If
                    Next
                ' The other Cases are irrelevant 
            End Select
        Else
            ret = False
        End If

        Return ret
    End Function

    Private Function RunModel(cCard As IOFile) As Boolean

        Dim uFile As String
        Dim ret As Boolean = True

        If CancelExecution Then Return CancelCleanUp()

        ' create the uconfiguration file
        If _RunNumber > 0 Then
            uFile = MakeUFile(cCard, _InputFiles, _OutputFiles, _RunNumber)
        Else
            uFile = MakeUFile(cCard, _InputFiles, _OutputFiles)
        End If

        ' Input: was not part of the vb 6 IInputModel class, but added in this version
        ' Main: delete files mainmodel is about to create (even the optional files) (calls CleanUpSingleRun)
        ret = KillIOFiles(_OutputFiles)

        ' execute the application
        If File.Exists(uFile) Then

            Dim arg As String = """" & uFile & """"
            If _Name = "Main Single Sensitivity" Then
                ' 7/27/2105 add an argument for either single or production run - this is a single run module
                arg = arg & " SINGLE" & " " & "NOVIZ"
            End If

            ret = RunApplication(arg)
        Else
            Return False
        End If

        'execute? did it terminate properly?

        ' kill the ufile
        Try
            File.Delete(uFile)
            Dim cleanupFiles = Directory.GetFiles(_SensitivityFolder).Where(Function(e) e.Contains(".spw") Or Path.GetFileName(e).StartsWith("SIMU")).ToList()

            For Each fileToDelete As String In cleanupFiles
                File.Delete(fileToDelete)
            Next

            ' clean up temp combined files
            If CombinedFile <> "" Then System.IO.File.Delete(CombinedFile)

        Catch ex As Exception

        End Try

        ret = VerifyOutputFiles(cCard.FileName)

        Return ret

    End Function

Любая помощь очень ценится, спасибо, что нашли время, чтобы прочитать мой пост.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...