本文整理汇总了VB.NET中System.Threading.Tasks.TaskFactory.StartNew方法的典型用法代码示例。如果您正苦于以下问题:VB.NET TaskFactory.StartNew方法的具体用法?VB.NET TaskFactory.StartNew怎么用?VB.NET TaskFactory.StartNew使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Threading.Tasks.TaskFactory 的用法示例。
在下文中一共展示了TaskFactory.StartNew方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的VB.NET代码示例。
示例1: Example
' 导入命名空间
Imports System.Collections.Generic
Imports System.Threading
Imports System.Threading.Tasks
Module Example
Public Sub Main()
Dim cts As New CancellationTokenSource()
Dim token As CancellationToken = cts.Token
Dim tasks As New List(Of Task)()
Dim rnd As New Random()
Dim lockObj As New Object()
Dim words6() As String = { "reason", "editor", "rioter", "rental",
"senior", "regain", "ordain", "rained" }
For Each word6 in words6
tasks.Add(Task.Factory.StartNew( Sub(word)
Dim chars() As Char = word.ToString().ToCharArray()
Dim order(chars.Length - 1) As Double
Dim wasZero As Boolean = False
SyncLock lockObj
For ctr As Integer = 0 To order.Length - 1
order(ctr) = rnd.NextDouble()
If order(ctr) = 0 Then
If Not wasZero Then
wasZero = True
Else
cts.Cancel()
End If
End If
Next
End SyncLock
token.ThrowIfCancellationRequested()
Array.Sort(order, chars)
Console.WriteLine("{0} --> {1}", word,
new String(chars))
End Sub, word6))
Next
Try
Task.WaitAll(tasks.ToArray())
Catch e As AggregateException
For Each ie In e.InnerExceptions
If TypeOf ie Is OperationCanceledException
Console.WriteLine("The word scrambling operation has been cancelled.")
Exit For
Else
Console.WriteLine(ie.GetType().Name + ": " + ie.Message)
End If
Next
Finally
cts.Dispose()
End Try
End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System.Threading.Tasks,代码行数:54,代码来源:TaskFactory.StartNew 输出:
regain --> irnaeg
ordain --> rioadn
reason --> soearn
rained --> rinade
rioter --> itrore
senior --> norise
rental --> atnerl
editor --> oteird
示例2: Example
' 导入命名空间
Imports System.Collections.Generic
Imports System.IO
Imports System.Threading
Imports System.Threading.Tasks
Module Example
Public Sub Main()
Dim tokenSource As New CancellationTokenSource()
Dim token As CancellationToken = tokenSource.Token
Dim files As New List(Of Tuple(Of String, String, Long, Date))()
Dim t As Task = Task.Factory.StartNew( Sub()
Dim dir As String = "C:\Windows\System32\"
Dim obj As New Object()
If Directory.Exists(dir)Then
Parallel.ForEach(Directory.GetFiles(dir),
Sub(f)
If token.IsCancellationRequested Then
token.ThrowIfCancellationRequested()
End If
Dim fi As New FileInfo(f)
SyncLock(obj)
files.Add(Tuple.Create(fi.Name, fi.DirectoryName, fi.Length, fi.LastWriteTimeUtc))
End SyncLock
End Sub)
End If
End Sub, token)
tokenSource.Cancel()
Try
t.Wait()
Console.WriteLine("Retrieved information for {0} files.", files.Count)
Catch e As AggregateException
Console.WriteLine("Exception messages:")
For Each ie As Exception In e.InnerExceptions
Console.WriteLine(" {0}:{1}", ie.GetType().Name, ie.Message)
Next
Console.WriteLine()
Console.WriteLine("Task status: {0}", t.Status)
Finally
tokenSource.Dispose()
End Try
End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System.Threading.Tasks,代码行数:44,代码来源:TaskFactory.StartNew 输出:
Exception messages:
TaskCanceledException: A task was canceled.
Task status: Canceled
示例3: Example
' 导入命名空间
Imports System.Collections.Generic
Imports System.Threading.Tasks
Public Module Example
Public Sub Main()
Dim rnd As New Random()
Dim tasks As New List(Of Task)
' Execute the task 10 times.
For ctr As Integer = 1 To 9
tasks.Add(Task.Factory.StartNew(Sub()
Dim utf32 As Integer
SyncLock(rnd)
' Get UTF32 value.
utf32 = rnd.Next(0, &hE01F0)
End SyncLock
' Convert it to a UTF16-encoded character.
Dim utf16 As String = Char.ConvertFromUtf32(utf32)
' Display information about the character.
Console.WriteLine("0x{0:X8} --> '{1,2}' ({2})",
utf32, utf16, ShowHex(utf16))
End Sub))
Next
Task.WaitAll(tasks.ToArray())
End Sub
Private Function ShowHex(value As String) As String
Dim hexString As String = Nothing
' Handle only non-control characters.
If Not Char.IsControl(value, 0) Then
For Each ch In value
hexString += String.Format("0x{0} ", Convert.ToUInt16(ch))
Next
End If
Return hexString.Trim()
End Function
End Module
开发者ID:VB.NET开发者,项目名称:System.Threading.Tasks,代码行数:37,代码来源:TaskFactory.StartNew 输出:
0x00097103 --> ' |
请发表评论