我正在寻找实现通用方法的好主意,让一行(或匿名委托)代码执行超时.
TemperamentalClass tc = new TemperamentalClass();
tc.DoSomething(); // normally runs in 30 sec. Want to error at 1 min
Run Code Online (Sandbox Code Playgroud)
我正在寻找一个可以在我的代码与气质代码交互的许多地方(我无法改变)优雅地实现的解决方案.
另外,如果可能的话,我想让违规的"超时"代码停止执行.
我正在寻找的是相当于System.Windows.SystemParameters.WorkArea窗口当前所在的监视器.
澄清: 有问题的窗口WPF不是WinForm.
我正在尝试获取bash脚本的引用参数,以便安全地通过嵌套脚本接收.有任何想法吗?
test.sh
#!/bin/bash
echo $*
bash myecho.sh $*
Run Code Online (Sandbox Code Playgroud)
myecho.sh
#!/bin/bash
echo $1
echo $2
echo $3
echo $4
Run Code Online (Sandbox Code Playgroud)
样品:
bash test.sh aaa bbb '"ccc ddd"'
Run Code Online (Sandbox Code Playgroud)
结果:
aaa bbb "ccc ddd"
aaa
bbb
"ccc
ddd"
Run Code Online (Sandbox Code Playgroud)
想要结果
aaa bbb "ccc ddd"
aaa
bbb
ccc ddd
Run Code Online (Sandbox Code Playgroud) 索引器何时可以自动将项添加到集合/字典中?这是合理的,还是违背了最佳做法?
public class I { /* snip */ }
public class D : Dictionary<string, I>
{
public I this[string name]
{
get
{
I item;
if (!this.TryGetValue(name, out item))
{
item = new I();
this.Add(name, item);
}
return item;
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何在集合中使用它的示例:
public class I
{
public I(string name) {/* snip */}
public string Name { get; private set; }
/* snip */
}
public class C : Collection<I>
{
private Dictionary<string, I> nameIndex = new Dictionary<string, I>();
public …Run Code Online (Sandbox Code Playgroud) 我目前正在查看OperationContect.Current属性.是否有(嵌套)属性将始终返回客户端的机器名称?我目前正在使用net.tcp绑定,但希望将来支持其他绑定.
使用.NET 3.5 SP1
我正在尝试实现一个用于WCF的字典.我的要求是:
System.StringComparer.InvariantCultureIgnoreCase我试图在WCF主机和客户端项目共享的公共项目中使用此类:
[Serializable]
public class MyDictionary : Dictionary<string, object>
{
public MyDictionary()
: base(System.StringComparer.InvariantCultureIgnoreCase)
{ }
public new void Add(string key, object value)
{ /* blah */ }
public override string ToString()
{ /* blah */ }
}
[DataContract]
[KnownType(typeof(MyDictionary))]
[KnownType(typeof(object[]))]
[KnownType(typeof(double[]))]
[KnownType(typeof(string[]))]
[KnownType(typeof(DateTime[]))]
public class ResultClass
{
public object Value{ get; set; }
/* More properties */
}
public class ParmData
{
public object Value{ get; set; }
/* More properties */ …Run Code Online (Sandbox Code Playgroud) 我为ObservableCollection创建了一个包装器集合,它订阅每个PropertyChanged事件并将其重新抛出为自己的事件ItemPropertyChanged.我使用与我在此描述的方法类似的方法做到了这一点.有没有更好的办法?我错过了另一个已经有这种行为的.NET集合吗?
我的目标是为FlowDocumentScrollViewer创建可重用的附加行为,以便每当FlowDocument更新(附加)时,查看器自动滚动到末尾.
到目前为止的问题
我意识到那些可能是3个独立的问题(也就是问题).但是它们彼此依赖,并且我尝试了这种行为的整体设计.我问这是一个单一的问题,以防我以错误的方式解决这个问题.如果我是,那么正确的方法是什么?
/// Attached Dependency Properties not shown here:
/// bool Enabled
/// DependencyProperty DocumentProperty
/// TextRange MonitoredRange
/// ScrollViewer ScrollViewer
public static void OnEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d == null || System.ComponentModel.DesignerProperties.GetIsInDesignMode(d))
return;
DependencyProperty documentProperty = null;
ScrollViewer scrollViewer = null;
if (e.NewValue is bool && (bool)e.NewValue)
{
// Using reflection so that this will work with similar types.
FieldInfo documentFieldInfo = d.GetType().GetFields().FirstOrDefault((m) => m.Name == "DocumentProperty");
documentProperty = documentFieldInfo.GetValue(d) as DependencyProperty;
// …Run Code Online (Sandbox Code Playgroud) 我想获取/创建所有当前正在执行的操作的集合并选择元数据(可从OperationContext获得).
我有一个布尔函数,用于许多其他函数的决策.并且每次都向用户提供消息框或允许其继续,具体取决于该函数的返回值.所以我的伪代码可能如下所示:
private bool IsConsented()
{
//some business logic
}
private void NotReal()
{
if (IsConsented())
{
//call function A
}
else
{
MessageBox.Show("Need consent first.");
}
}
private void NotReal2()
{
if (IsConsented())
{
//call function B
}
else
{
MessageBox.Show("Need consent first.");
}
}
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种更简单的方法,而不是将if-else逻辑硬编码到我的每个函数中.我希望能够有这样的功能:
private void CheckConsent(function FunctionPointer)
{
if (IsConsented())
{
//call the function
FunctionPointer();
}
else
{
MessageBox.Show("Need consent first.");
}
}
Run Code Online (Sandbox Code Playgroud)
这样我就可以将指针传递给函数.我真的怀疑这与委托有关,但我不知道语法,我不明白如何使用委托传递参数.
c# ×8
wcf ×3
c#-3.0 ×2
wpf ×2
.net-4.0 ×1
asynchronous ×1
bash ×1
c#-4.0 ×1
collections ×1
dictionary ×1
indexing ×1
timeout ×1
unix ×1