小编Sti*_*tif的帖子

SynchronizationContext在Task.Run上流动,但不在await上流动

在阅读了Stephen Toub关于SynchronizationContext的文章后,我留下了一个关于这段.NET 4.5代码输出的问题:

private void btnDoSomething_Click()
{
    LogSyncContext("btnDoSomething_Click");
    DoItAsync().Wait();
}
private async Task DoItAsync()
{
    LogSyncContext("DoItAsync");
    await PerformServiceCall().ConfigureAwait(false); //to avoid deadlocking
}
private async Task PerformServiceCall()
{
    LogSyncContext("PerformServiceCall 1");
    HttpResponseMessage message = await new HttpClient
    {
        BaseAddress = new Uri("http://my-service")
    }
    .GetAsync("/").ConfigureAwait(false); //to avoid deadlocking
    LogSyncContext("PerformServiceCall 2");
    await ProcessMessage(message);
    LogSyncContext("PerformServiceCall 3");
}

private async Task ProcessMessage(HttpResponseMessage message)
{
    LogSyncContext("ProcessMessage");
    string data = await message.Content.ReadAsStringAsync();
    //do something with data
}

private static void LogSyncContext(string statementId)
{
    Trace.WriteLine(String.Format("{0} {1}", statementId, …
Run Code Online (Sandbox Code Playgroud)

.net c# synchronizationcontext async-await

5
推荐指数
1
解决办法
1978
查看次数

为什么我感觉我的F#代码可能更简洁

我是一名经验丰富的C#开发人员,他试图自学F#.我花了一天或三天的时间阅读F#wikibook试图了解语法和F#基础知识.

作为练习,我正在尝试通过Project Euler问题来更好地理解语法并使用该语言.

我刚刚解决了问题5.但是我不太高兴我必须跳过这个箍来获得代表我的解决方案的数据结构.我已经使用这个博文来获得解决问题的算法.

我想知道是否有人可以给我一些关于如何改进这些代码的指示?我的猜测是,F#值的固有不变性导致我必须执行大量步骤才能获得我想要的确切数据...

这是我的代码:

let main argv =
//calculates the prime factors of a number
let findPrimeFactors x =
    let primes = [|2I;3I;5I;7I;11I;13I;17I;19I|]
    let rec loop acc counter = function
        | x when x = 1I -> failwith "A PRIME IS BY DEFINITION GREATER THAN 1"
        | x when primes |> Array.contains x -> x :: acc
        | x when counter = primes.Length -> failwith "MY LIST OF KNOWN PRIMES …
Run Code Online (Sandbox Code Playgroud)

f# immutability

1
推荐指数
1
解决办法
88
查看次数