相关疑难解决方法(0)

C#线程安全快速(est)计数器

在C#中以最佳性能获取线程安全计数器的方法是什么?

这很简单:

public static long GetNextValue()
{
    long result;
    lock (LOCK)
    {
        result = COUNTER++;
    }
    return result;
}
Run Code Online (Sandbox Code Playgroud)

但是有更快的选择吗?

c# multithreading counter thread-safety

129
推荐指数
4
解决办法
7万
查看次数

Powershell:如何将结果添加到数组(ForEach-Object -Parallel)

我知道,通过参数,$using:foo我可以在 Powershell 7 及更高版本中运行时使用来自不同运行空间的变量ForEach-Object -Parallel

但是如何将结果添加回变量呢?通用参数+=$using:不会起作用。

例如:

$AllSubs = Get-AzSubscription
$Date = (Get-Date).AddDays(-2).ToString("yyyy-MM-dd")
$Costs = @()

$AllSubs | Sort-Object -Property Name | ForEach-Object -Parallel {
    Set-AzContext $_.Name | Out-Null
    Write-Output "Fetching Costs from '$($_.Name)' ..."
    $using:Costs += Get-AzConsumptionUsageDetail -StartDate $using:Date -EndDate $using:Date -IncludeAdditionalProperties -IncludeMeterDetails -ErrorAction SilentlyContinue
}
Run Code Online (Sandbox Code Playgroud)

输出:

The assignment expression is not valid. The input to an assignment operator must be an object that is
     | able to accept assignments, such …
Run Code Online (Sandbox Code Playgroud)

powershell

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

标签 统计

c# ×1

counter ×1

multithreading ×1

powershell ×1

thread-safety ×1