本文整理匯總了VB.NET中System.Threading.Tasks.Task.FromResult<TResult>方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET Task.FromResult<TResult>方法的具體用法?VB.NET Task.FromResult<TResult>怎麽用?VB.NET Task.FromResult<TResult>使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Threading.Tasks.Task
的用法示例。
在下文中一共展示了Task.FromResult<TResult>方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: Example
' 導入命名空間
Imports System.Collections.Generic
Imports System.IO
Imports System.Threading
Imports System.Threading.Tasks
Module Example
Public Sub Main()
Dim args() As String = Environment.GetCommandLineArgs()
If args.Length > 1 Then
Dim tasks As New List(Of Task(Of Long))
For ctr = 1 To args.Length - 1
tasks.Add(GetFileLengthsAsync(args(ctr)))
Next
Try
Task.WaitAll(tasks.ToArray())
' Ignore exceptions here.
Catch e As AggregateException
End Try
For ctr As Integer = 0 To tasks.Count - 1
If tasks(ctr).Status = TaskStatus.Faulted Then
Console.WriteLine("{0} does not exist", args(ctr + 1))
Else
Console.WriteLine("{0:N0} bytes in files in '{1}'",
tasks(ctr).Result, args(ctr + 1))
End If
Next
Else
Console.WriteLine("Syntax error: Include one or more file paths.")
End If
End Sub
Private Function GetFileLengthsAsync(filePath As String) As Task(Of Long)
If Not Directory.Exists(filePath) Then
Return Task.FromException(Of Long)(
New DirectoryNotFoundException("Invalid directory name."))
Else
Dim files As String() = Directory.GetFiles(filePath)
If files.Length = 0 Then
Return Task.FromResult(0L)
Else
Return Task.Run( Function()
Dim total As Long = 0
Dim lockObj As New Object
Parallel.ForEach(files, Sub(fileName)
Dim fs As New FileStream(fileName, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite,
256, True)
Dim length As Long = fs.Length
Interlocked.Add(total, length)
fs.Close()
End Sub)
Return total
End Function )
End If
End If
End Function
End Module
' When launched with the following command line arguments:
' subdir . newsubdir
輸出:
0 bytes in files in 'subdir' 2,059 bytes in files in '.' newsubdir does not exist