Tag: .net

调用批处理文件后,服​​务在WaitForExit处挂起

我有一个服务有时会调用批处理文件。 批处理文件需要5-10秒才能执行: System.Diagnostics.Process proc = new System.Diagnostics.Process(); // Declare New Process proc.StartInfo.FileName = fileName; proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; proc.StartInfo.CreateNoWindow = true; proc.Start(); proc.WaitForExit(); 当我在控制台中运行相同的代码时,该文件确实存在并且代码可以正常工作。 但是当它在服务内部运行时,它会在WaitForExit()处挂起。 我必须从Process中删除批处理文件才能继续。 (我确定文件存在,因为我可以在进程列表中看到它。) 我该如何修复这个挂断? 更新#1: 凯文的代码允许我获得输出。 我的一个批处理文件仍然挂起。 “C:\ EnterpriseDB \ Postgres \ 8.3 \ bin \ pg_dump.exe”-i -h localhost -p 5432 -U postgres -F p -a -D -v -f“c:\ backupcasecocher \ backupdateevent2008.sql”-t […]

如何在超时期限后取消任务等待

我正在使用此方法以编程方式实例化Web浏览器,导航到URL并在文档完成时返回结果。 如果文档加载时间超过5秒,我将如何能够停止Task并让GetFinalUrl()返回null ? 我见过很多使用TaskFactory例子,但是我无法将它应用于这段代码。 private Uri GetFinalUrl(PortalMerchant portalMerchant) { SetBrowserFeatureControl(); Uri finalUri = null; if (string.IsNullOrEmpty(portalMerchant.Url)) { return null; } Uri trackingUrl = new Uri(portalMerchant.Url); var task = MessageLoopWorker.Run(DoWorkAsync, trackingUrl); task.Wait(); if (!String.IsNullOrEmpty(task.Result.ToString())) { return new Uri(task.Result.ToString()); } else { throw new Exception(“Parsing Failed”); } } // by Noseratio – http://stackoverflow.com/users/1768303/noseratio static async Task DoWorkAsync(object[] […]

为什么switch for enum接受隐式转换为0但是对于任何其他整数都没有?

有一个: enum SomeEnum { A = 0, B = 1, C = 2 } 现在编译器允许我写: SomeEnum x = SomeEnum.A; switch(x) { case 0: // <— Considered SomeEnum.A break; case SomeEnum.B: break; case SomeEnum.C: break; default: break; } 0被认为是SomeItems.A 。 但我写不出来: SomeEnum x = SomeEnum.A; switch(x) { case 0: break; case 1: // <— Here is […]

使用reflection来调用重写的基本方法

如何使用reflection调用被派生类重写的基本方法? class Base { public virtual void Foo() { Console.WriteLine(“Base”); } } class Derived : Base { public override void Foo() { Console.WriteLine(“Derived”); } } public static void Main() { Derived d = new Derived(); typeof(Base).GetMethod(“Foo”).Invoke(d, null); Console.ReadLine(); } 此代码始终显示“派生”…

BinaryWriter Endian问题

我正在使用BinaryWriter类将二进制文件写入磁盘。 当我调用Write方法时,传递一个unsigned short值,它以little-endian格式写入。 例如: bw.Write(0xA000); 将二进制文件中的值写为0x00 0xA0。 有没有办法让BInaryWriter使用Big Endian? 如果没有,是否可以创建一个新类,inheritanceBinaryWriter并重载Write函数以使其写入大端?

C#Windows窗体应用程序 – 从另一个线程AND类更新GUI?

我搜索了很多,但我似乎无法找到与我的具体问题有关的任何内容。 我希望能够从另一个类(SocketListener)更新我的MainUI表单,并且我有一个处理网络的线程(clientThread)。 现在我可以从网络线程运行简单的输出,例如写入调试器输出和创建MessageBox。 但我真正想做的是能够调用clientThread中的代码,该代码将在我的MainUI实例上执行操作。 我怎样才能做到这一点? 此外,如果有人想要代码的特定部分,那么我可以发布它,以帮助您更好地理解我的要求。 最好的祝福!

Parallel.ForEach比ForEach慢

这是代码: using (var context = new AventureWorksDataContext()) { IEnumerable _customerQuery = from c in context.Customers where c.FirstName.StartsWith(“A”) select c; var watch = new Stopwatch(); watch.Start(); var result = Parallel.ForEach(_customerQuery, c => Console.WriteLine(c.FirstName)); watch.Stop(); Debug.WriteLine(watch.ElapsedMilliseconds); watch = new Stopwatch(); watch.Start(); foreach (var customer in _customerQuery) { Console.WriteLine(customer.FirstName); } watch.Stop(); Debug.WriteLine(watch.ElapsedMilliseconds); } 问题是, Parallel.ForEach比常规foreach大约需要400ms,大约需要40ms。 我究竟做错了什么,为什么这不能像我期望的那样工作?

如何重新抛出InnerException而不会丢失C#中的堆栈跟踪?

我通过反思调用一个可能导致exception的方法。 如何在没有包装器reflection的情况下将exception传递给调用者? 我正在重新抛出InnerException,但这会破坏堆栈跟踪。 示例代码: public void test1() { // Throw an exception for testing purposes throw new ArgumentException(“test1”); } void test2() { try { MethodInfo mi = typeof(Program).GetMethod(“test1”); mi.Invoke(this, null); } catch (TargetInvocationException tiex) { // Throw the new exception throw tiex.InnerException; } }

在不使用GetMethods的情况下获取通用方法

我想得到方法System.Linq.Queryable.OrderyBy(the IQueryable source, Expression<Func> keySelector)方法,但我一直提出空值。 var type = typeof(T); var propertyInfo = type.GetProperty(group.PropertyName); var propertyType = propertyInfo.PropertyType; var sorterType = typeof(Func).MakeGenericType(type, propertyType); var expressionType = typeof(Expression).MakeGenericType(sorterType); var queryType = typeof(IQueryable); var orderBy = typeof(System.Linq.Queryable).GetMethod(“OrderBy”, new[] { queryType, expressionType }); /// is always null. 有没有人有任何见解? 我宁愿不循环GetMethods结果。

WPF:如何循环窗口中的所有控件?

如何在WPF窗口中循环所有控件?