Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions snippets/fsharp/System.Threading.Tasks/Task/.ctor/Run11.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Run11
// <Snippet11>
open System.Threading
open System.Threading.Tasks

let showThreadInfo s =
printfn $"%s{s} thread ID: {Thread.CurrentThread.ManagedThreadId}"

showThreadInfo "Application"

let t = Task.Run(fun () -> showThreadInfo "Task")
t.Wait()

// The example displays the following output:
// Application thread ID: 1
// Task thread ID: 3
// </Snippet11>
71 changes: 71 additions & 0 deletions snippets/fsharp/System.Threading.Tasks/Task/.ctor/Run28.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
module Run28
// <Snippet28>
open System
open System.Collections.Generic
open System.Threading
open System.Threading.Tasks

let source = new CancellationTokenSource()
let token = source.Token
let mutable completedIterations = 0

let tasks =
[| for _ = 0 to 19 do
Task.Run(
(fun () ->
let mutable iterations = 0

for _ = 1 to 2000000 do
token.ThrowIfCancellationRequested()
iterations <- iterations + 1

Interlocked.Increment &completedIterations |> ignore

if completedIterations >= 10 then
source.Cancel()

iterations),
token
) |]

printfn "Waiting for the first 10 tasks to complete...\n"

try
tasks |> Seq.cast |> Array.ofSeq |> Task.WaitAll

with :? AggregateException ->
printfn "Status of tasks:\n"
printfn "%10s %20s %14s" "Task Id" "Status" "Iterations"

for t in tasks do
if t.Status <> TaskStatus.Canceled then
t.Result.ToString "N0"
else
"n/a"
|> printfn "%10i %20O %14s" t.Id t.Status

// The example displays output like the following:
// Status of tasks:
//
// Task Id Status Iterations
// 1 RanToCompletion 2,000,000
// 2 RanToCompletion 2,000,000
// 3 RanToCompletion 2,000,000
// 4 RanToCompletion 2,000,000
// 5 RanToCompletion 2,000,000
// 6 RanToCompletion 2,000,000
// 7 RanToCompletion 2,000,000
// 8 RanToCompletion 2,000,000
// 9 RanToCompletion 2,000,000
// 10 RanToCompletion 1,658,326
// 11 RanToCompletion 1,988,506
// 12 RanToCompletion 2,000,000
// 13 RanToCompletion 1,942,246
// 14 RanToCompletion 950,108
// 15 RanToCompletion 1,837,832
// 16 RanToCompletion 1,687,182
// 17 RanToCompletion 194,548
// 18 Canceled Not Started
// 19 Canceled Not Started
// 20 Canceled Not Started
// </Snippet28>
74 changes: 74 additions & 0 deletions snippets/fsharp/System.Threading.Tasks/Task/.ctor/Run7.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
module Run7
// <Snippet7>
open System
open System.Collections.Generic
open System.Threading
open System.Threading.Tasks

let source = new CancellationTokenSource()
let token = source.Token
let mutable completedIterations = 0

let tasks =
[| for _ = 0 to 19 do
Task.Run(
(fun () ->
let mutable iterations = 0

for _ = 1 to 2000000 do
token.ThrowIfCancellationRequested()
iterations <- iterations + 1

Interlocked.Increment &completedIterations |> ignore

if completedIterations >= 10 then
source.Cancel()

iterations),
token
)

|]

printfn "Waiting for the first 10 tasks to complete...\n"

try
tasks |> Seq.cast |> Array.ofSeq |> Task.WaitAll
with :? AggregateException ->
printfn "Status of tasks:\n"
printfn "%10s %20s %14s" "Task Id" "Status" "Iterations"

for t in tasks do
if t.Status <> TaskStatus.Canceled then
t.Result.ToString "N0"
else
"n/a"
|> printfn "%10i %20O %14s" t.Id t.Status


// The example displays output like the following:
// Waiting for the first 10 tasks to complete...
// Status of tasks:
//
// Task Id Status Iterations
// 1 RanToCompletion 2,000,000
// 2 RanToCompletion 2,000,000
// 3 RanToCompletion 2,000,000
// 4 RanToCompletion 2,000,000
// 5 RanToCompletion 2,000,000
// 6 RanToCompletion 2,000,000
// 7 RanToCompletion 2,000,000
// 8 RanToCompletion 2,000,000
// 9 RanToCompletion 2,000,000
// 10 Canceled n/a
// 11 Canceled n/a
// 12 Canceled n/a
// 13 Canceled n/a
// 14 Canceled n/a
// 15 Canceled n/a
// 16 RanToCompletion 2,000,000
// 17 Canceled n/a
// 18 Canceled n/a
// 19 Canceled n/a
// 20 Canceled n/a
// </Snippet7>
34 changes: 34 additions & 0 deletions snippets/fsharp/System.Threading.Tasks/Task/.ctor/ctor1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module ctor1
// <Snippet1>
open System.Collections.Concurrent
open System.IO
open System.Threading.Tasks

let main =
task {
let list = ConcurrentBag<string>()
let dirNames = [ "."; ".." ]
let tasks = ResizeArray()

for dirName in dirNames do
let t =
new Task(fun () ->
for path in Directory.GetFiles dirName do
list.Add path)

tasks.Add t
t.Start()

do! tasks.ToArray() |> Task.WhenAll

for t in tasks do
printfn $"Task {t.Id} Status: {t.Status}"

printfn $"Number of files read: {list.Count}"
}

// The example displays output like the following:
// Task 1 Status: RanToCompletion
// Task 2 Status: RanToCompletion
// Number of files read: 23
// </Snippet1>
18 changes: 18 additions & 0 deletions snippets/fsharp/System.Threading.Tasks/Task/.ctor/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="ctor1.fs" />
<Compile Include="run2.fs" />
<Compile Include="run4.fs" />
<Compile Include="startnew3.fs" />
<Compile Include="Run11.fs" />
<Compile Include="run6.fs" />
<Compile Include="run41.fs" />
<Compile Include="run31.fs" />
<Compile Include="Run7.fs" />
<Compile Include="Run28.fs" />
</ItemGroup>
</Project>
32 changes: 32 additions & 0 deletions snippets/fsharp/System.Threading.Tasks/Task/.ctor/run2.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module run2
// Example for Task.Run(Action) method.

// <Snippet1>
open System.Collections.Concurrent
open System.IO
open System.Threading.Tasks

let list = ConcurrentBag<string>()
let dirNames = [ "."; ".." ]
let tasks = ResizeArray()

for dirName in dirNames do
let t =
Task.Run(fun () ->
for path in Directory.GetFiles dirName do
list.Add path)

tasks.Add t

tasks.ToArray() |> Task.WaitAll

for t in tasks do
printfn $"Task {t.Id} Status: {t.Status}"

printfn $"Number of files read: {list.Count}"

// The example displays output like the following:
// Task 1 Status: RanToCompletion
// Task 2 Status: RanToCompletion
// Number of files read: 23
// </Snippet1>
34 changes: 34 additions & 0 deletions snippets/fsharp/System.Threading.Tasks/Task/.ctor/run31.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module run31
// <Snippet2>
open System
open System.IO
open System.Text.RegularExpressions
open System.Threading.Tasks

let pattern = @"\p{P}*\s+"
let titles = [| "Sister Carrie"; "The Financier" |]

let tasks =
Array.map (fun title ->
Task.Run(fun () ->
// Create filename from title.
let fn = title + ".txt"

if File.Exists fn then
use sr = new StreamReader(fn)
let input = sr.ReadToEndAsync().Result
Regex.Matches(input, pattern).Count
else
0)) titles

tasks |> Seq.cast |> Array.ofSeq |> Task.WaitAll

printfn "Word Counts:\n"

for i = 0 to tasks.Length - 1 do
printfn $"%s{titles.[i]}: %10d{tasks.[i].Result} words"

// The example displays the following output:
// Sister Carrie: 159,374 words
// The Financier: 196,362 words
// </Snippet2>
59 changes: 59 additions & 0 deletions snippets/fsharp/System.Threading.Tasks/Task/.ctor/run4.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module run4
// Illustrates the Task.Run(Action, CancellationToken) overload.

// <Snippet4>
open System
open System.IO
open System.Threading
open System.Threading.Tasks

let main =
task {
use tokenSource = new CancellationTokenSource()
let token = tokenSource.Token
let files = ResizeArray()

let t =
new Task(
(fun () ->
let dir = @"C:\Windows\System32\"
let obj = obj ()

if Directory.Exists dir then
Parallel.ForEach(
Directory.GetFiles dir,
fun f ->
if token.IsCancellationRequested then
token.ThrowIfCancellationRequested()

let fi = FileInfo f
lock obj (fun () -> files.Add(fi.Name, fi.DirectoryName, fi.Length, fi.LastWriteTimeUtc))
)
|> ignore),
token
)

t.Start()
tokenSource.Cancel()

try
do! t
printfn $"Retrieved information for {files.Count} files."

with :? AggregateException as e ->
printfn "Exception messages:"

for ie in e.InnerExceptions do
printfn $" {ie.GetType().Name}: {ie.Message}"

printfn $"Task status: {t.Status}"
}

main.Wait()

// The example displays the following output:
// Exception messages:
// TaskCanceledException: A task was canceled.
//
// Task status: Canceled
// </Snippet4>
Loading