You could just use Quartz.NET. You'd just implement IInteruptableJob in A,B,C, defining how they stop. Then another job at 10:00AM to stop the others.

By on 3/6/2011 5:33 PM ()

Hi,
Thank you very much for your idea. However, I have never used Quartz.net. Do you have any sample code?
By the way, I think may be F# has some built-in features for this, unfortunately, the sample code for F# on this topic is very limited, so until now I can not find good examples for my issue.

By on 3/7/2011 9:55 AM ()
By on 3/9/2011 4:49 AM ()

Hi,

I'm not an expert on async workflows, but here is some sample code using async with cancellation tokens:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
open System
open System.Threading


let tasks = [
              // job 1 sleeps 5 seconds
              async { printfn "start job 1"

                      // in long running computation without use of Async,
                      // check "myToken.IsCancellationRequested" regularly
                      let! myToken = Async.CancellationToken

                      // Async.Sleep itself cannot be cancelled;
                      // that's why we sleep in small steps in this example;
                      // we don't need to check "myToken.IsCancellationRequested"
                      // in the loop because "do!" inside the async workflow does
                      // that automatically
                      for _ in 1..5 do
                        do! Async.Sleep 1000
                      printfn "end job 1" }

              // job 2 sleeps 10 seconds
              async { printfn "start job 2"
                      for _ in 1..10 do
                        do! Async.Sleep 1000
                      printfn "end job 2" }
            ]

let cts = new CancellationTokenSource()

// start the actual tasks asynchronously (so that we can afterwards start the extra job)
tasks |> Seq.iter (fun task -> Async.Start(task, cts.Token))

// extra job: cancel the actual tasks after 7 seconds
async { printfn "waiting 7 secs until cancel"
        do! Async.Sleep 7000
        cts.Cancel()
        printfn "cancelled"
      }
|> Async.RunSynchronously // start cancel job and wait

printfn "Press enter to end program"
Console.ReadLine() |> ignore

You will probably have to modify your classes to take a cancellation token as an argument and check it regularly.

Hope this helps.

By on 3/7/2011 12:18 PM ()

Hello,
Thank you very much for your code, your code seem working well.
However, my real program is a little complicated. As I have 3 classes, each has around 300 lines of code, it is not easy as here in your code, which each task has only a few lines.
Therefore, it could take me some time to study your code and try to apply your idea to my code.
By the way, can you change your code so that the 2 tasks are in different modules in different classes?
Thanks,

By on 3/8/2011 1:06 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper