Execute方法Activity的入口
Activity的Execute就跟应用程序的Main一样,是入口函数,由加载者自动调用,入口函数的格是一个与加载者契约,不能修改
先看一下应用程序的入口函数,我用了一个有返回值的入口函数,这是与常用的
static
void Main()有些不同,是入口函数的另一个版本
class
Program
{
//一个的返回值的入口函数
static
int Main()
{
System.Console.WriteLine("wxwinter");
return 0; //返回给调用者
}
} |
运行结果

|
再看一下Activity的入口函数
//自定义Activity
public
class
myActivity : Activity
{
protected
override
ActivityExecutionStatus Execute(ActivityExecutionContext eac)
{
System.Console.WriteLine("wxwinter");
return
ActivityExecutionStatus.Closed;
}
}
//宿主
class
Program
{
static
void Main()
{
WorkflowRuntime workflowRuntime = new
WorkflowRuntime();
workflowRuntime.WorkflowCompleted +=new
EventHandler<WorkflowCompletedEventArgs>(workflowRuntime_WorkflowCompleted);
workflowRuntime.WorkflowIdled += new
EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowIdled);
WorkflowInstance ins= workflowRuntime.CreateWorkflow(typeof(myActivity));
ins.Start();
System.Console.Read();
}
static
void workflowRuntime_WorkflowIdled(object sender, WorkflowEventArgs e)
{
System.Console.WriteLine("WorkflowIdled");
}
static
void workflowRuntime_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e)
{
System.Console.WriteLine("WorkflowCompleted");
}
} |

|
我创建了一个Activity,并将它直接交给引擎,由引擎创建实例,当实例Start时,Execute被执行,当Execute返回Closed时流程完成
Activity的生命周期
初始化
生看一下应用程序
入口函数并不是第一个被执行的,第一个被执行的是静态构造函数
class
Program
{
static Program()
{
System.Console.WriteLine("静态构造函数");
}
Program()
{
//在本例中由于没有 new 过Program,不会被执行
System.Console.WriteLine("构造函数");
}
//一个的返回值的入口函数
static
int Main()
{
System.Console.WriteLine("wxwinter");
return 0; //返回给调用者
}
} |

|
再看一下Activity
//自定义Activity
public
class
myActivity : Activity
{
protected
override
void Initialize(IServiceProvider provider)
{
System.Console.WriteLine("Initialize");
base.Initialize(provider);
}
protected
override
ActivityExecutionStatus Execute(ActivityExecutionContext eac)
{
System.Console.WriteLine("wxwinter");
return
ActivityExecutionStatus.Closed;
}
}
//宿主
class
Program
{
static
void Main()
{
WorkflowRuntime workflowRuntime = new
WorkflowRuntime();
workflowRuntime.WorkflowCompleted +=new
EventHandler<WorkflowCompletedEventArgs>(workflowRuntime_WorkflowCompleted);
workflowRuntime.WorkflowIdled += new
EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowIdled);
WorkflowInstance ins= workflowRuntime.CreateWorkflow(typeof(myActivity));
ins.Start();
System.Console.Read();
}
static
void workflowRuntime_WorkflowIdled(object sender, WorkflowEventArgs e)
{
System.Console.WriteLine("WorkflowIdled");
}
static
void workflowRuntime_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e)
{
System.Console.WriteLine("WorkflowCompleted");
}
} |

|
在Activity中,就更明显了,Initialize在实例创建时就被调用,Execute只是在才被调用,这里有一点要注意,构造函数是隐示调用基类的构造函数,
Initialize要显示调用基类的Initialize
应用程序与Activity的生命周期都是:
开始准备 -> 准备完成 -> 开始执行 -> 完成关闭
(这里我们先不谈Activity的Canceling,Compensating,Faulting 以及析构与回收)
执行完成的标记
先看一下应用程序
class
Program
{
static
int Main()
{
test();
System.Console.Read();
return 0;
}//注意这个花括号
static
void test()
{
System.Console.WriteLine("wxwinter");
System.Threading.Thread.Sleep(1000);
System.Console.WriteLine("lzm");
System.Threading.Thread.Sleep(1000);
System.Console.WriteLine("wxd");
System.Threading.Thread.Sleep(1000);
}
} |

|
如果你清楚的知道程序地内存栈中的工作方式,你就会知道"{" 与 "}" 的含意
入口函数的"{" 与 "}" 包含程序的全部生命周期,一般我们认为入口函数的 "}"完成,程序就完成了,其实还有一个工作要做,就是从 "}"向
"{"出栈,当然这是由管理器来做的,所以入口函数的 "}"就是执行完成的标记
看一下Activity,[return ActivityExecutionStatus.Closed]就是执行完成的标记
protected
override
ActivityExecutionStatus Execute(ActivityExecutionContext eac)
{
return
ActivityExecutionStatus.Closed;
} |
现在有个问题,如果在[入口函数]或Execute中调用了其它线程怎么办
看一下的程序实现方式
class
Program
{
static System.Threading.AutoResetEvent waitHandle = new System.Threading.AutoResetEvent(false);
static
int Main()
{
System.Threading.Thread th = new System.Threading.Thread(test);
th.Start();
waitHandle.WaitOne();
return 0;
}
static
void test()
{
System.Console.WriteLine("wxwinter");
System.Threading.Thread.Sleep(1000);
System.Console.WriteLine("lzm");
System.Threading.Thread.Sleep(1000);
System.Console.WriteLine("wxd");
System.Threading.Thread.Sleep(1000);
waitHandle.Set();
}
} |
waitHandle就是一个待标记,Activity使用的是ActivityExecutionStatus.Executing;
看一下例子
//自定义Activity
public
class
myActivity : Activity
{
protected
override
ActivityExecutionStatus Execute(ActivityExecutionContext eac)
{
test();
return
ActivityExecutionStatus.Executing;
}
void test()
{
System.Console.WriteLine("wxwinter");
}
}
//宿主
class
Program
{
static
void Main()
{
WorkflowRuntime workflowRuntime = new
WorkflowRuntime();
workflowRuntime.WorkflowCompleted +=new
EventHandler<WorkflowCompletedEventArgs>(workflowRuntime_WorkflowCompleted);
workflowRuntime.WorkflowIdled += new
EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowIdled);
WorkflowInstance ins= workflowRuntime.CreateWorkflow(typeof(myActivity));
ins.Start();
System.Console.Read();
}
static
void workflowRuntime_WorkflowIdled(object sender, WorkflowEventArgs e)
{
System.Console.WriteLine("WorkflowIdled");
}
static
void workflowRuntime_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e)
{
System.Console.WriteLine("WorkflowCompleted");
}
} |

|
ActivityExecutionStatus.Executing标记Activity进入等执行等待
ActivityExecutionStatus.Closed标记Activity完成
现在还的一个问题,如何将ActivityExecutionStatus.Executing设为ActivityExecutionStatus.Closed
可以用Activity的方法Invoke与ActivityExecutionContext的CloseActivity方法实现
看例子
//自定义Activity
public
class
myActivity : Activity
{
protected
override
ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
this.Invoke(test, new
EventArgs());
return
ActivityExecutionStatus.Executing;
}
void test(object sender, EventArgs e)
{
System.Console.WriteLine("wxwinter");
ActivityExecutionContext aec = sender as
ActivityExecutionContext;
aec.CloseActivity(); //将Activity的执行状态设为Closed
}
}
//宿主
class
Program
{
static
void Main()
{
WorkflowRuntime workflowRuntime = new
WorkflowRuntime();
workflowRuntime.WorkflowCompleted +=new
EventHandler<WorkflowCompletedEventArgs>(workflowRuntime_WorkflowCompleted);
workflowRuntime.WorkflowIdled += new
EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowIdled);
WorkflowInstance ins= workflowRuntime.CreateWorkflow(typeof(myActivity));
ins.Start();
System.Console.Read();
}
static
void workflowRuntime_WorkflowIdled(object sender, WorkflowEventArgs e)
{
System.Console.WriteLine("WorkflowIdled");
}
static
void workflowRuntime_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e)
{
System.Console.WriteLine("WorkflowCompleted");
}
}
|

|
总结:
ACE提供了将Activity设为ActivityExecutionStatus.Closed的实现
本文对Activity的生命周期的介绍只是简单的,具体的介绍将在后面的工作流生命周期中介绍