注意:它的错误是在WaitHandle.WaitAll(doneEvents)行; 我正在使用标准的WPF项目.
private void Search()
{
const int CPUs = 2;
var doneEvents = new ManualResetEvent[CPUs];
// Configure and launch threads using ThreadPool:
for (int i = 0; i < CPUs; i++)
{
doneEvents[i] = new ManualResetEvent(false);
var f = new Indexer(Paths[i], doneEvents[i]);
ThreadPool.QueueUserWorkItem(f.WaitCallBack, i);
}
// Wait for all threads in pool
WaitHandle.WaitAll(doneEvents);
Debug.WriteLine("Search completed!");
}
Run Code Online (Sandbox Code Playgroud)
更新:以下解决方案不适用于WPF应用程序!无法将主应用程序属性更改为MTAThreadAttribute.这将导致以下错误:
错误: "不支持在STA线程上使用多个句柄的WaitAll."
无论框架版本如何,C#都支持哪些内置设计模式?我正在考虑可以在接口IObservable中找到的Observer模式等模式.ObservableCollection,INotifyPropertyChanged等.
请在答案中提供模式的命名空间!
我正在使用带有Resharper的Visual Studio 2010.我的项目中的许多类都很大,我想知道是否有一个快捷方式可以在我的类的构造函数之间跳转.
什么是将bool转换为字节的最快方法?
我想要这个映射:False = 0,True = 1
注意:我不想使用任何if语句或其他条件语句.我不希望CPU停止或猜测下一个语句.
更新: 对于那些想要看到这个问题的人.此示例显示如何从代码中减少两个if语句.
byte A = k > 9 ; //If it was possible (k>9) == 0 || 1
c[i * 2] = A * (k + 0x37) - (A - 1) * (k + 0x30);
Run Code Online (Sandbox Code Playgroud) 我正在运行下面的代码并在下面获得例外.我是否被迫将此函数放入try catch中,还是以其他方式递归获取所有目录?我可以编写自己的递归函数来获取文件和目录.但我想知道是否有更好的方法.
// get all files in folder and sub-folders
var d = Directory.GetFiles(@"C:\", "*", SearchOption.AllDirectories);
// get all sub-directories
var dirs = Directory.GetDirectories(@"C:\", "*", SearchOption.AllDirectories);
Run Code Online (Sandbox Code Playgroud)
"拒绝访问路径'C:\ Documents and Settings \'."
规范模式是DDD中使用的常见模式,它封装了业务逻辑以响应一个问题.
public interface ISpecification<T>
{
bool IsSatisfiedBy(T aSource);
}
public class CustomerHaveDiscountSpec : ISpecification<Customer>
{
bool IsSatisfiedBy(Customer aCustomer)
{
/* ... */
}
}
Run Code Online (Sandbox Code Playgroud)
域驱动设计中常见的其他模式是什么?
无法在"绑定"类型的"源"属性上设置"绑定".'绑定'只能在DependencyObject的DependencyProperty上设置.
<TreeView Height="400" Width="400">
<TreeViewItem ItemsSource="{Binding Source={Binding Path=Data}, XPath=*,
Converter={StaticResource stringToXmlDataProviderConverter},ConverterParameter=/root }" Header="header" />
</TreeView>
Run Code Online (Sandbox Code Playgroud)
ItemsSource ="{Binding Source = {Binding Path = Data}?
Data = "<root><parm1>1</parm1><parm2>2</parm2><parm3>3</parm3></root>"
Run Code Online (Sandbox Code Playgroud)我尝试使用此代码示例.不同之处在于我想将ItemsSource绑定到datacontext中的数据.转换器没有问题.
编辑:
<TreeViewItem ItemsSource="{Binding Path=Data}" Header="Parameters" />
Run Code Online (Sandbox Code Playgroud)
使用一个元素(字符串)填充TreeView.所以datacontext是正确的.
编辑: 此代码更好.是否有一种在ThreeView中读取XML的通用方法?我不知道XML的结构.在我看到的所有示例中,您必须声明子节点类型.
<TreeViewItem DataContext="{Binding Path=Data, Converter={StaticResource stringToXmlDataProviderConverter}}" ItemsSource="{Binding .}" Header="Parameters" />
Run Code Online (Sandbox Code Playgroud) 我在考虑所有类型的游戏类别.我的经验是,没有任何开源游戏真正挑战商业游戏,考虑游戏价值,图形,声音等.
这是一个例子:
[ValueConversion(typeof(float), typeof(String))]
public class PercentConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null || string.IsNullOrEmpty(value.ToString()))
return string.Empty;
if (value is float) //Edited to support CultureInfo.CurrentCulture,
return string.Format(culture, "{0:n}{1}", ((float)value) * 100, "%");
//** Is it ok to put Exception here or should I return "something" here? **
throw new Exception("Can't convert from " + value.GetType().Name + ". Expected type if float.");
}
public object ConvertBack(object value, …Run Code Online (Sandbox Code Playgroud)