ITPUB??ì3
12月微软Hyper-V虚拟化沙龙主题征集
ITPUB论坛 » WEB程序开发 » [C# .Net]Why the current process has exited?

新一届的微软MVP评选已经开始,欢迎各位推荐!

标题: [C# .Net]Why the current process has exited?
离线 Arraygrace_limi
老会员



精华贴数 0
个人空间 0
技术积分 1742 (972)
社区积分 81 (3902)
注册日期 2002-3-20
论坛徽章:2
会员2006贡献徽章授权会员    
      

发表于 2004-7-5 16:10 
[C# .Net]Why the current process has exited?

I have used the following method in C# .Net to set the running priority for a
process and its affiliated threads. It is noticable at statement (*)
that if the process has already exited, the method will return
immediately without further operations. However, sometimes the method
may generate the following exception:

===========================================================
03/07/04 3:48:06 AM auto_rep.Run() error:
System.InvalidOperationException: The request can not be processed
because the thread (2428) has exited.
   at System.Diagnostics.ProcessManager.OpenThread(Int32 threadId, Int32
access)
   at System.Diagnostics.ProcessThread.OpenThreadHandle(Int32 access)
   at System.Diagnostics.ProcessThread.set_PriorityBoostEnabled(Boolean
value)
   at lib.libs.set_proc_priority(Process proc, Int32 level)
   at DBSample.auto_rep.Run()
===========================================================

The contextual codes calling the method are:

===========================================================
if( libs.work_hour() )
  libs.set_proc_priority( Process.GetCurrentProcess(), 1 );
else
  libs.set_proc_priority( Process.GetCurrentProcess(), 2 );
===========================================================

The full codes of the method are listed as below:

==========================================================
public static void set_proc_priority( Process proc, int level )
{
  int k;

  if( proc.HasExited )                              (*)
    return;
  proc.PriorityBoostEnabled = true;
  switch( level )
  {
    case( -2 ):
      proc.PriorityClass = ProcessPriorityClass.Idle;
      for( k = 0; k < proc.Threads.Count; k++ )
      {
    proc.Threads[k].PriorityBoostEnabled = true;
    proc.Threads[k].PriorityLevel = ThreadPriorityLevel.Idle;
      }
      break;
    case( -1 ):
      proc.PriorityClass = ProcessPriorityClass.BelowNormal;
      for( k = 0; k < proc.Threads.Count; k++ )
      {
    proc.Threads[k].PriorityBoostEnabled = true;
    proc.Threads[k].PriorityLevel = ThreadPriorityLevel.BelowNormal;
      }
      break;
    case( 0 ):
      proc.PriorityClass = ProcessPriorityClass.Normal;
      for( k = 0; k < proc.Threads.Count; k++ )
      {
    proc.Threads[k].PriorityBoostEnabled = true;
    proc.Threads[k].PriorityLevel = ThreadPriorityLevel.Normal;
      }
      break;
    case( 1 ):
      proc.PriorityClass = ProcessPriorityClass.AboveNormal;
      for( k = 0; k < proc.Threads.Count; k++ )
      {
    proc.Threads[k].PriorityBoostEnabled = true;
    proc.Threads[k].PriorityLevel = ThreadPriorityLevel.AboveNormal;
      }
      break;
    case( 2 ):
      proc.PriorityClass = ProcessPriorityClass.High;
      for( k = 0; k < proc.Threads.Count; k++ )
      {
    proc.Threads[k].PriorityBoostEnabled = true;
    proc.Threads[k].PriorityLevel = ThreadPriorityLevel.Highest;
      }
      break;
    case( 3 ):
      proc.PriorityClass = ProcessPriorityClass.RealTime;
      for( k = 0; k < proc.Threads.Count; k++ )
      {
    proc.Threads[k].PriorityBoostEnabled = true;
    proc.Threads[k].PriorityLevel = ThreadPriorityLevel.TimeCritical;
      }
      break;
  }
  return;
}
==========================================================


Any solution to the problem?


只看该作者    顶部
离线 omencathay
菜田守望者


精华贴数 7
个人空间 80
技术积分 5173 (268)
社区积分 7016 (222)
注册日期 2002-10-21
论坛徽章:7
现任管理团队成员     
      

发表于 2004-7-5 16:39 
来自.net framwork文档

当方法调用对于对象的当前状态无效时引发的异常。

有关此类型所有成员的列表,请参阅 InvalidOperationException 成员。

System.Object
   System.Exception
      System.SystemException
         System.InvalidOperationException
            System.Net.ProtocolViolationException
            System.Net.WebException
            System.ObjectDisposedException

[Visual Basic]
<Serializable>
Public Class InvalidOperationException
   Inherits SystemException

[C#]
[Serializable]
public class InvalidOperationException : SystemException

[C++]
[Serializable]
public __gc class InvalidOperationException : public
   SystemException

[JScript]
public
   Serializable
class InvalidOperationException extends SystemException

线程安全
此类型的所有公共静态(Visual Basic 中为 Shared)成员对多线程操作而言都是安全的。但不保证任何实例成员是线程安全的。

备注
如果调用方法失败不是由无效参数造成的,则使用 InvalidOperationException。例如,对于 InvalidOperationException 而言:

如果创建枚举数后修改了集合中的对象,则由 MoveNext 引发该异常。
如果执行方法调用前关闭了资源集,则由 GetString 引发该异常。
如果方法调用失败是由无效参数造成的,则应改为引发 ArgumentException 或其派生类 ArgumentNullException 或 ArgumentOutOfRangeException。

Ldflda Microsoft 中间语言 (MSIL) 指令引发 InvalidOperationException。
InvalidOperationException 使用值为 0x80131509 的 HRESULT COR_E_INVALIDOPERATION。

有关 InvalidOperationException 的实例的初始属性值列表,请参见 InvalidOperationException 构造函数。


只看该作者    顶部
 
    

相关内容


CopyRight 1999-2006 itpub.net All Right Reserved.
北京皓辰广域网络信息技术有限公司. 版权所有
E-mail:Webmaster@itpub.net
京ICP证:010037号 联系我们 法律顾问