小编ole*_*egk的帖子

异步/等待性能

我正在研究广泛使用async/await功能的程序的性能优化.一般来说,它通过HTTP并行下载数千个json文档,解析它们并使用这些数据构建一些响应.我们遇到一些性能问题,当我们同时处理许多请求时(例如下载1000个jsons),我们可以看到一个简单的HTTP请求可能需要几分钟.

我写了一个小型控制台应用程序来测试它的简化示例:

class Program
{
    static void Main(string[] args)
    {
        for (int i = 0; i < 100000; i++)
        {
            Task.Run(IoBoundWork);
        }

        Console.ReadKey();
    }

    private static async Task IoBoundWork()
    {
        var sw = Stopwatch.StartNew();

        await Task.Delay(1000);

        Console.WriteLine(sw.Elapsed);
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以在这里看到类似的行为:

在此输入图像描述

问题是为什么"等待Task.Delay(1000)"最终需要23秒.

c# asynchronous async-await

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

切换参数和powershell.exe -File参数

微软称:

在极少数情况下,您可能需要为switch参数提供布尔值.要在File参数的值中为switch参数提供布尔值,请将参数名称和值括在花括号中,如下所示:-File.\ Get-Script.ps1 {-All:$ False}

我有一个简单的脚本:

[CmdletBinding()] 
Param
(
    [switch] $testSwitch
)
$testSwitch.ToBool()
Run Code Online (Sandbox Code Playgroud)

接下来我试图以这种方式运行它:

powershell -file .\1.ps1 {-testSwitch:$false}
Run Code Online (Sandbox Code Playgroud)

结果我收到一个错误: 在此输入图像描述

但如果相信微软它应该工作.

如果我删除[CmdletBinding]属性,则不会发生此错误,但由于某些原因$testSwitch.ToBool()返回False,尽管我是否通过$True$False.

为什么?这种行为的原因是什么?

powershell

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

避免LSP违规的最佳方法

让我们考虑以下示例.我有这样的类的层次结构:

abstract class Base
{
    public abstract void DoSomething();
}

class Foo : Base
{
    public override void DoSomething()
    {
        Console.WriteLine("Foo. DoSomething...");
    }
}

class Bar : Base
{
    public override void DoSomething()
    {
        Console.WriteLine("Bar. DoSomething...");

        if (ShouldDoSomethingElse)
        {
            DoSomethingElse();
        }
    }

    public void DoSomethingElse()
    {
        Console.WriteLine("Bar. DoSomething else...");
    }

    public bool ShouldDoSomethingElse { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我的客户端是这样的:

class Program
{
    static void Main(string[] args)
    {
        var foo = new Foo();
        var bar = new Bar();
        var items = …
Run Code Online (Sandbox Code Playgroud)

c# oop solid-principles

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