C#到F#和相互依赖的函数

时间:2017-04-24 17:07:04

标签: c# f# translate forward-declaration

如果有人帮我翻译成F#,我将不胜感激。当然不是类的形式:1)如果我先声明ThreadProc,那么ThreadProc应该使用thread1和thread2,后者在构造函数中使用ThreadProc在main中定义;如果我首先声明main,则初始化将使用尚未定义的函数。 2)如果我在定义函数ThreadProc之前声明顶级thread1和thread2(例如,让thread1 = new Thread(fun() - >())),那么这是ThreadProc最终将使用的版本而不是之后的版本在主要宣布。

using System;
using System.Threading;

public class Example
{
    static Thread thread1, thread2;

    public static void Main()
    {
        thread1 = new Thread(ThreadProc);
        thread1.Name = "Thread1";
        thread1.Start();

        thread2 = new Thread(ThreadProc);
        thread2.Name = "Thread2";
        thread2.Start();   
    }   

    private static void ThreadProc()
    {
        Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
        if (Thread.CurrentThread.Name == "Thread1" && 
            thread2.ThreadState != ThreadState.Unstarted)
                thread2.Join();

        Thread.Sleep(4000);
        Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
        Console.WriteLine("Thread1: {0}", thread1.ThreadState);
        Console.WriteLine("Thread2: {0}\n", thread2.ThreadState);
   }
}

2 个答案:

答案 0 :(得分:1)

这证明了我在评论中提到的内容。它使用ParameterizedThreadStart通过F#记录将信息传递给线程。

您有责任确保传入的对象与线程proc中预期的类型相同。线程proc的参数必须是obj类型,因此编译器无法为您检查类型。但为方便起见,您可以使用args创建let args = args :?> Args的适当类型阴影。

open System
open System.Threading

type Args = { Thread1: Thread; Thread2: Thread }

let threadProc (args: obj) =
    let args = args :?> Args

    printfn "\n\nCurrent Thread: %s" Thread.CurrentThread.Name

    if Thread.CurrentThread.Name = "Thread 1" && args.Thread2.ThreadState <> ThreadState.Unstarted then
        args.Thread2.Join ()

    Thread.Sleep(4000)
    Console.WriteLine( "\n\nCurrent thread: {0}", Thread.CurrentThread.Name )
    Console.WriteLine("Thread 1: {0}", args.Thread1.ThreadState)
    Console.WriteLine("Thread 2: {0}\n", args.Thread2.ThreadState)

let thread1 = new Thread(ParameterizedThreadStart(threadProc))
thread1.Name <- "Thread 1"

let thread2 = new Thread(ParameterizedThreadStart(threadProc))
thread2.Name <- "Thread 2"

let main () =
    let args = { Thread1 = thread1; Thread2 = thread2 }
    thread1.Start(args)
    thread2.Start(args)
    System.Console.ReadKey () |> ignore

do main ()

答案 1 :(得分:0)

我提出了以下代码。但是,我会感谢任何有关使其更好的建议,即更聪明/更短的方式和/或以某种方式不使用可变的&#39;值:

open System
open System.Threading

let mutable thread1 = new Thread( fun () -> () )
let mutable thread2 = new Thread( fun () -> () )

let threadProc () =
    printfn "\n\nCurrent Thread: %s" Thread.CurrentThread.Name

    if ( Thread.CurrentThread.Name = "Thread 1" && 
         thread2.ThreadState <> ThreadState.Unstarted ) then 
             thread2.Join ();

    Thread.Sleep(4000)
    Console.WriteLine( "\n\nCurrent thread: {0}", 
                        Thread.CurrentThread.Name )
    Console.WriteLine("Thread 1: {0}", thread1.ThreadState)
    Console.WriteLine("Thread 2: {0}\n", thread2.ThreadState)

thread1 <- new Thread(threadProc)
thread1.Name <- "Thread 1"

thread2 <- new Thread(threadProc)
thread2.Name <- "Thread 2"

let main () = 
    thread1.Start()
    thread2.Start()
    System.Console.ReadKey () |> ignore

do main ()