本文整理汇总了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