创建新进程时是否有任何事件.我正在编写检查某些进程的ac#应用程序,但我不想写一个无限循环来连续迭代所有已知进程.相反,我宁愿检查每个创建的进程,也可以遍历事件触发的所有当前进程.有什么建议?
Process[] pArray;
while (true)
{
pArray = Process.GetProcesses();
foreach (Process p in pArray)
{
foreach (String pName in listOfProcesses) //just a list of process names to search for
{
if (pName.Equals(p.ProcessName, StringComparison.CurrentCultureIgnoreCase))
{
//do some stuff
}
}
}
Thread.Sleep(refreshRate * 1000);
}
Run Code Online (Sandbox Code Playgroud) 在实现返回关于抛出异常的Task的方法时,是否存在MS"最佳实践"或合同协议?这是在编写单元测试时出现的,我试图弄清楚我是否应该测试/处理这种情况(我认识到答案可能是"防御性编码",但我不希望这是答案).
即
方法必须始终返回一个Task,该Task应该包含抛出的Exception.
方法必须始终返回一个Task,除非该方法提供无效参数(即ArgumentException).
方法必须始终返回一个任务,除非开发人员变得流氓并做他/她想要的事情(jk).
Task Foo1Async(string id){
if(id == null){
throw new ArgumentNullException();
}
// do stuff
}
Task Foo2Async(string id){
if(id == null){
var source = new TaskCompletionSource<bool>();
source.SetException(new ArgumentNullException());
return source.Task;
}
// do stuff
}
Task Bar(string id){
// argument checking
if(id == null) throw new ArgumentNullException("id")
try{
return this.SomeService.GetAsync(id).ContinueWith(t => {
// checking for Fault state here
// pass exception through.
})
}catch(Exception ex){
// handling more Fault state here.
// defensive code.
// return …Run Code Online (Sandbox Code Playgroud)