Sharepoint 2010 如何从Sharepoint计时器作业中浮动异常?

Sharepoint 2010 如何从Sharepoint计时器作业中浮动异常?,sharepoint-2010,Sharepoint 2010,我有一个类MyMainApplication,它是一个SPIisWebServiceApplication。因此,它是在IIS下托管和运行的 我有一个自定义sharepoint计时器作业,比如说命名为CustomTimerJob类,它是从SPJobDefinition类派生的。因此,计时器作业在OWSTimer.exe下运行 我有两个问题:[请参阅下面的代码,以关联这些问题] CutomTimerJob中的变量是否可以从调用方访问,在我的示例中是var job=new CustomTimerJo

我有一个类MyMainApplication,它是一个SPIisWebServiceApplication。因此,它是在IIS下托管和运行的

我有一个自定义sharepoint计时器作业,比如说命名为CustomTimerJob类,它是从SPJobDefinition类派生的。因此,计时器作业在OWSTimer.exe下运行

我有两个问题:[请参阅下面的代码,以关联这些问题]

  • CutomTimerJob中的变量是否可以从调用方访问,在我的示例中是var job=new CustomTimerJob();job.RunNow(),varibale作业是否会引用正在运行的customeTimerJob,并能够获取job.status的值?我已经看到,人们使用[persistend]关键字标记变量,以保持状态。如果有人能详细说明,我将不胜感激。为什么会使用它,它实际上是如何工作的,它会坚持到哪里

  • 异常是否可以浮回被调用方? 据我所知,异常不会浮回被调用方是合乎逻辑的,因为计时器作业在单独的进程上运行。但问题是,当它执行这个var job=new CustomTimerJob();,作业变量指向什么

  • 我编写的代码如下所示:

    >     Class MyMainApplication : SPIiWebServiceApplication
    >     {
    >     // something
    >     .
    >     .
    >     .
    >     void some_function()
    >     {
    >     // Create and run the timer job immediately
    >     
    >     var job = new CustomTimerJob()
    >     job.RunNow();
    >     
    >     // Give it a bit of time before checking the status
    >     Thread.Sleep(5000);
    >     
    >     // Want to print the status to see if it was changed when it ran 
    >     Console.Writeln( job.Status );
    >     
    >     }
    >     
    >     }
    >     
    >     ----------------------
    >     
    >     class CustomTimerJob : SPJobDefinition
    >     {
    >     
    >     public Boolean status;
    >     // something
    >     
    >     public override void Execute(Guid contentDbId)
    >     {
    >     status = true;
    >     try {
    >     // do some processing
    >     } catch (Exception) {
    >     
    >     // Can I throw the exception up from here ? And will the calle get the exception
    >     // throw new CustomException(e);
    >     
    >     }
    >     
    >     }
    
    我真的很感激那些有心一直读到这一行的读者。我的荣誉


    提前感谢。

    方法
    SPJobDefinition.RunNow()
    只安排执行,然后在
    OWSTimer.exe
    进程的上下文中执行。因此,你的问题的答案如下:

  • 不,它们是不可接近的。更准确地说,您创建的实例与将要执行的实例不同,它甚至存在于另一个进程中
  • 不,出于同样的原因,异常不会传播到您调用
    RunNow()
    的位置。通常,允许未经处理的异常在自定义计时器作业之外传播是一种不好的做法,因为该作业会被视为失败。除非这是正确处理异常所需要的

  • 确定CustomTimerJob何时完成执行的最佳方法是什么?(这是runonce计时器作业,由RunNow触发)