当我创建一个任务时
Task task = Task.Factory.StartNew(() => someMethod(args));
Run Code Online (Sandbox Code Playgroud)
在C#4.0+中,我如何获得此任务的线程的引用?
是否可能在创建任务的同一线程中执行任务或生成多个线程?
更新:
原因是:
创建的任务是否始终在与创建任务的线程不同的线程中执行?
它是一个,零个还是多个线程?
它是在同一个核心上执行的吗?
重要的是要知道,因为,例如,我可以睡觉主线程,认为我冻结后台工作人员
更新:
有用的答案:
C#4.0 WPF应用程序,请参阅下面的代码,在启动时显示:

我怎样才能按顺序排序aaa,bbb,ccc?
C#代码:
public MainWindow()
{
InitializeComponent();
listBox1.Items.Add("ccc");
listBox1.Items.Add("aaa");
listBox1.Items.Add("bbb");
}
private void btnSort_Click(object sender, RoutedEventArgs e)
{
listBox1.Items.SortDescriptions.Add(
new System.ComponentModel.SortDescription("Content",
System.ComponentModel.ListSortDirection.Ascending));
}
private void listBox1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
listBox1.Items.RemoveAt
(listBox1.Items.IndexOf(listBox1.SelectedItem));
}
Run Code Online (Sandbox Code Playgroud)
XAML:
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox Height="100" HorizontalAlignment="Left" Margin="8,43,0,0" Name="listBox1" VerticalAlignment="Top" Width="120" MouseDoubleClick="listBox1_MouseDoubleClick" />
<Button Content="Sort" Height="23" HorizontalAlignment="Left" Margin="140,94,0,0" Name="btnSort" VerticalAlignment="Top" Width="75" Click="btnSort_Click" />
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
更新:
嗯,我只是按照文章"排序WPF ListBox项目"
那么,我按照属性"内容"进行排序的顺序是什么,以及该属性"内容"在哪里,我想知道(试图将其改为任意"fff"而不是"内容"已经变得相同,如第二个截图,结果?
阅读文章"匿名方法"(作为文章系列"C#3.0中的代理和Lambda表达式"的一部分)中的短语:
" 高级主题:无参数匿名方法
...允许匿名方法省略参数列表(delegate { return Console.ReadLine() != ""}例如).这是非典型的,但它确实允许相同的匿名方法出现在多个场景中,即使委托类型可能会有所不同 "*
我变得有点困惑.
IMO(现在找不到但据我记得),类型由参数列表确定,但不是由方法的返回类型确定.这是对的吗?
那么,无参数方法或委托的类型如何不同?
任何(最简单的)代码示例说明了相同的匿名方法的不同无参数委托类型将是值得赞赏的.
考虑下面的C#Console应用程序的代码,使用
我应该如何修改它以替换该行:
foreach (Product product in productsByCategory[category])
Run Code Online (Sandbox Code Playgroud)
通过代码行
foreach (Product product in productsByCategory[category][Id])
Run Code Online (Sandbox Code Playgroud)
?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace myQuestion
{
class Program
{
static void Main(string[] args)
{
var products = new List<Product>
{
new Product { Id = 1, Category = "Garden", Value = 15.0 },
new Product { Id = 1, Category = "Garden", Value = 40.0 },
new Product { Id = 3, Category = "Garden", Value = 210.3 },
new Product …Run Code Online (Sandbox Code Playgroud) 我试图运行Eric Lippert的博客文章中的代码"为什么递归的lambda导致明确的赋值错误?"
但不是运行(或给出编译"明确的分配错误",我得到:
名为't'的局部变量不能在此范围内声明,因为它会给't'赋予不同的含义,'t'已在'父或当前'范围内用于表示其他内容
为什么?
它在父母或当前范围内已经在哪里使用?
试图重命名它有相同的错误
我应该如何更改代码以启动此代码?
using System;
namespace LippertRecursiveLambdaError
{
class Program
{
static void Main(string[] args)
{
Tree<int> tree = new Tree<int>
(3, new Tree<int>
(4, new Tree<int>(1), null),
new Tree<int>(5));
PrintTree(tree);
Console.ReadLine();
}
delegate void Action<A>(A a);
delegate void Traversal<T>(Tree<T> t, Action<T> a);
static void DepthFirstTraversal<T>(Tree<T> t, Action<T> a)
{
if (t == null) return;
a(t.Value);
DepthFirstTraversal(t.Left, a);
DepthFirstTraversal(t.Right, a);
}
static void Traverse<T>(Tree<T> t, Traversal<T> f, Action<T> a)
{
f(t, a); …Run Code Online (Sandbox Code Playgroud) msdn文章Thread Synchronization(C#编程指南)指定:
lock (x)
{
DoSomething();
}
Run Code Online (Sandbox Code Playgroud)
相当于:
System.Object obj = (System.Object)x;
System.Threading.Monitor.Enter(obj);
try
{
DoSomething();
}
finally
{
System.Threading.Monitor.Exit(obj);
}
Run Code Online (Sandbox Code Playgroud)
然后那个:
"使用lock关键字通常比直接使用Monitor类更受欢迎,... 因为锁可以确保释放底层监视器,即使受保护的代码抛出异常 "
这句话是否意味着使用监视器的最后一个代码片段不能确保"即使受保护的代码抛出异常,也会释放底层监视器"?
为什么?
好吧,我感到困惑的是相互矛盾的断言"等价"而不是(一种用法保险,另一种用途,等价物,不等同).
MSDN "线程安全集合.NET Framework 4"声明:
"一些并发集合类型使用轻量级同步机制,如SpinLock,SpinWait, SemaphoreSlim和CountdownEvent,它们是.NET Framework 4中的新增功能"
虽然MSDN网站告诉SpinWaitwas可用于.NET 1.1,而另一篇MSDN文章从.NET 4.0启动SpinWaitwas
好吧,好奇心来自Lee Grissom的评论,以回答SynchronizedCollection与其他并发集合之间的区别是什么?:
"@Matt,.NET4并发类使用
SpinWait对象来解决线程安全而不是Monitor.Enter/Exit(又称Critical部分)?"
以及第一个NSDN引用说明这SpinWait是.NET 4.0的新功能
那么,它是新的还是不是?
如果新的那么如何?
从Parallel.ForEach运行代码可以通过一些我的修改来生成 新线程
带注释行的输出:
//threadsRemaining = Interlocked.Decrement(ref concurrentThreads);
Run Code Online (Sandbox Code Playgroud)
是"显而易见的",即预期的一个:
[00:00] Job 0 complete. 2 threads remaining. unsafeCount=2
[00:00] Job 1 complete. 1 threads remaining. unsafeCount=1
[00:00] Job 2 complete. 3 threads remaining. unsafeCount=3
[00:00] Job 3 complete. 4 threads remaining. unsafeCount=4
[00:00] Job 4 complete. 5 threads remaining. unsafeCount=5
[00:00] Job 5 complete. 6 threads remaining. unsafeCount=6
[00:01] Job 6 complete. 7 threads remaining. unsafeCount=7
[00:01] Job 8 complete. 8 threads remaining. unsafeCount=8
[00:01] Job 7 complete. …Run Code Online (Sandbox Code Playgroud) MSDN告诉我,使用锁相当于使用监视器,但更简洁,更不容易出错.
你能给我一个简单的(单个过程)例子为什么我会被迫使用,Monitor因为它无法完成lock?
我在Visual Studio 2010,Windows XP SP3中运行下面的C#代码.
在"无需调试启动"(通过Ctrl + F5或从菜单中),输出:
"从调试开始"(通过F5或从菜单)显示相反的顺序:
多次检查.它具有可重复性和可重复性.
为什么?
using System;
using System.Threading;
namespace _5NamingThreads
{
class ThreadNaming
{
static void Main()
{
Thread.CurrentThread.Name = "main";
Thread worker = new Thread(Go);
worker.Name = "worker";
worker.Start();
Go();
Console.ReadLine();
}
static void Go()
{
Console.WriteLine("Hello from " + Thread.CurrentThread.Name);
}
}
}
Run Code Online (Sandbox Code Playgroud)
更新:
第二天,重新启动计算机后,我总是会观察在所有模式下发布和调试都具有相当可重现性的顺序:
后续问题:
在调试时,是否可以确保输出而不进入每行代码?
我试图在 VS2012 (Window 7) 中运行问题“ Understanding ICommandimplementation without MVVM ”中的代码,但出现错误:“WindowsPresentationFoundation(WPF)项目中不支持AppCommand”
好吧,出了什么问题或者我应该做什么才能运行该问题的代码?
MainWindow.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace seWPFUnderstandingICommandWithout_MVVM
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
AppCommands.AnyCommand.CanExecuteChanged += MyEventHandler;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
// Nothing for the moment
}
private void MyEventHandler(object sender, EventArgs e)
{
Console.WriteLine("MyEventHandler called");
}
} …Run Code Online (Sandbox Code Playgroud)