1. host程序向workflow传送信息
1) 使用事件机制
host程序以winForm为例,WWF由状态机流程的handleExternalEventActivity为例,该Activity需要等待一个事件的触发才能够继续工作流的运行,而该事件的触发则由host程序上的BUTTON点击事件触发。
所有host与Workflow的通讯均要通一个实现了某个interface的class来进行,下面是interface的定义
[System.Workflow.Activities.ExternalDataExchange]//该接口必须标记有此属性
public interface IUserInterface
{
void SendMessage(IntPtr handle,DataFromWin args);//host将通过调用此方法来触发事件
event EventHandler<DataFromWin> SendNum;//该EventHandle负责向Workflow传送事件及数据,触发handleExternalEventActivity的执行,从而开始workflow。
}
public interface IUserInterface
{
void SendMessage(IntPtr handle,DataFromWin args);//host将通过调用此方法来触发事件
event EventHandler<DataFromWin> SendNum;//该EventHandle负责向Workflow传送事件及数据,触发handleExternalEventActivity的执行,从而开始workflow。
}
比较恶心的是,MS规定向workflow传送的数据必须继承于某个特殊的class格式如handleExternal必须继承自ExternalDataEventArgs
[Serializable]//实现之前必须有此属性,该类负责传输实例化后的workflow的instanceId,以及部分host需要向workflow传送的数据,例如str就是从host端传入的
public class DataFromWin : ExternalDataEventArgs
{
private Guid documentId;
private string str;
public class DataFromWin : ExternalDataEventArgs
{
private Guid documentId;
private string str;
public DataFromWin(Guid instanceId,string str)
: base(instanceId)
{
this.documentId = instanceId;
this.str = str;
}
: base(instanceId)
{
this.documentId = instanceId;
this.str = str;
}
public Guid DocumentId
{
get { return this.documentId; }
set { this.documentId = value; }
}
public string stre
{
get { return this.str; }
set { this.str = value; }
}
}
{
get { return this.documentId; }
set { this.documentId = value; }
}
public string stre
{
get { return this.str; }
set { this.str = value; }
}
}
下面的类负责实现上文提到的interface:
public class UserInterface : IUserInterface
{
public void SendMessage(IntPtr handle, DataFromWin args)
{//实际上IntPtr handle 也可以放在args中传送
Console.WriteLine("hello");
if (this.SendNum != null)
this.SendNum(handle,args);//该句负责向workflow传送事件及host的数据,一旦执行该命令,则handleExternalEventActivity将开始工作
}
{
public void SendMessage(IntPtr handle, DataFromWin args)
{//实际上IntPtr handle 也可以放在args中传送
Console.WriteLine("hello");
if (this.SendNum != null)
this.SendNum(handle,args);//该句负责向workflow传送事件及host的数据,一旦执行该命令,则handleExternalEventActivity将开始工作
}
public event EventHandler<DataFromWin> SendNum;
}
}
下面转到vs的workflow设计界面,选中handleExternalEventActivity,在properties中有如下几个选项:
EventName:SendNum//选择触发该activity执行的事件,该事件是在interface中定义的
InterfaceType:Iuserinterface//选择该activity需要执行的interface
Invoked:Runit//在该activity执行过程中,需要执行的方法,注意该方法在workflow类中定义,并且签名要满足该activity的定义,如:
public void Runit(Object sender, ExternalDataEventArgs e)
{
Console.WriteLine(((DataFromWin)e).stre);
newText((IntPtr)sender);
}
{
Console.WriteLine(((DataFromWin)e).stre);
newText((IntPtr)sender);
}
下面在host中编写代码触发该activity的执行
private WorkflowLibrary1.UserInterface xx= new WorkflowLibrary1.UserInterface();
首先实例化这个负责host和workflow间数据传送的类,再次声明,此类的运行机制很重要,基本代表了wwf中各种activity的触发和数据传送机制。
workflowRuntime = new WorkflowRuntime();
ExternalDataExchangeService mmm = new ExternalDataExchangeService();
workflowRuntime.AddService(mmm);
mmm.AddService(xx);
workflowRuntime.AddService(mmm);
mmm.AddService(xx);
workflowRuntime.StartRuntime();
中间3句比较有意思,wwf中,activity与外界的交流貌似都要通过各种service来实现,有得可以直接添加相应的接口实现类就可以了,有的必须要首先添加规定的service,比如这个例子,首先要向runtime中添加ExternalDataExchangeService的实例,再向ExternalDataExchangeService中添加负责数据交换的UserInterface类。有的情况下直接添加UserInterface类就ok了,搞不懂,就当这是规定吧,谁叫俺不是设计这个咚咚的人呢。
然后实例化workflow,触发workflow开始工作的代码如下
private void button1_Click(object sender, EventArgs e)
{
xx.SendMessage(this.Handle,new WorkflowLibrary1.DataFromWin(workflowInstance.InstanceId,this.textBox1.Text));
{
xx.SendMessage(this.Handle,new WorkflowLibrary1.DataFromWin(workflowInstance.InstanceId,this.textBox1.Text));
}
host调用UserInterface实例的方法,传入Handle和符合ExternalDataEventArgs的类,其中主要有2个信息,一是该workflow的实例号,二是winform界面输入的字符。感觉之所以强制数据交换类符合ExternalDataEventArgs的要求,就是为了获得workflow的实例号,防止多个workflow实例运行时消息的误传递。
紧接着this.SendNum(handle,args);将数据传入workflow,同时该事件的触发导致handleExternalEventActivity的执行从而开启整个workflow,获得启动后handleExternalEventActivity同时还将执行回调函数Runit处理传送进的数据。至此,整个从host向workflow传递数据的过程结束了。
2) 使用属性
Workflow:
public partial class Workflow1 : SequentialWorkflowActivity
{
private void codeActivity1_CodeHandler(object sender, EventArgs e)
{
System.Windows.Forms.MessageBox.Show(
"Hello, World!: " + firstName + " " + lastName);
}
{
private void codeActivity1_CodeHandler(object sender, EventArgs e)
{
System.Windows.Forms.MessageBox.Show(
"Hello, World!: " + firstName + " " + lastName);
}
private string firstName;
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
{
get { return firstName; }
set { firstName = value; }
}
private string lastName;
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
{
get { return lastName; }
set { lastName = value; }
}
Host宿主程序:
if (wr == null)
{
wr = new WorkflowRuntime();
wr.StartRuntime();
}
{
wr = new WorkflowRuntime();
wr.StartRuntime();
}
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("FirstName", txtFirstName.Text);
parameters.Add("LastName", txtLastName.Text);
parameters.Add("FirstName", txtFirstName.Text);
parameters.Add("LastName", txtLastName.Text);
WorkflowInstance instance = wr.CreateWorkflow(typeof(HelloWorldWorkflow.Workflow1), parameters);
instance.Start();
instance.Start();
2. 从workflow向host传送数据的流程如下:
首先我的目的是在workflow运行到某一阶段的时候触发一个事件,在winform的textbox2上显示一条文本。这需要向workflow传送一个该form的handle,有2种方式传送该handle:
一是将此handle 封装在符合ExternalDataEventArgs要求的类中,也就是DataFormWin类,这比较简单也比较直观,就不说了
二是在workflow中定义一个IntPtr类型的属性,在初始化workflow的时候传入
public static DependencyProperty HandleProperty = System.Workflow.ComponentModel.DependencyProperty.Register("Handle", typeof(IntPtr), typeof(Workflow1));
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public IntPtr Handle
{
get
{
return ((IntPtr)(base.GetValue(Workflow1.HandleProperty)));
}
set
{
base.SetValue(Workflow1.HandleProperty, value);
}
}//workflow中的属性代码,so麻烦,懒得看,照着用就是了
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public IntPtr Handle
{
get
{
return ((IntPtr)(base.GetValue(Workflow1.HandleProperty)));
}
set
{
base.SetValue(Workflow1.HandleProperty, value);
}
}//workflow中的属性代码,so麻烦,懒得看,照着用就是了
在初始化的时候
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("Handle", this.Handle);//建立参数,将handle 传入workflow
workflowInstance = workflowRuntime.CreateWorkflow(typeof(WorkflowLibrary1.Workflow1),parameters);//初始化
workflowInstance.Start();
parameters.Add("Handle", this.Handle);//建立参数,将handle 传入workflow
workflowInstance = workflowRuntime.CreateWorkflow(typeof(WorkflowLibrary1.Workflow1),parameters);//初始化
workflowInstance.Start();
之后在vs的workflow设计界面handleExternalEventActivity的sender属性上选择Handle就ok了。
数据传入后,以下的处理都差不多
首先,定义delegate:public delegate void textBoxuseEventHandler(IntPtr Handle);
其次,在workflow类中初始化:public static textBoxuseEventHandler newText;
再次,在Runit中触发newText(((DataFromWin)e).Handle);
在host端订阅WorkflowLibrary1.Workflow1.newText = TextShow;
public void TextShow(IntPtr handle)
{
Form1 form = (Form1)(Form1.FromHandle(handle));
if (form != null) form.MessageReceive("12133dsfdsf");
}
{
Form1 form = (Form1)(Form1.FromHandle(handle));
if (form != null) form.MessageReceive("12133dsfdsf");
}
public void MessageReceive( string message)
{
if (this.InvokeRequired)
{
MessageDisplayDelegate messageDisplay = new MessageDisplayDelegate(this.MessageReceive);
this.Invoke(messageDisplay, message);
}
else
{
this.textBox2.Text += message;
}
}
{
if (this.InvokeRequired)
{
MessageDisplayDelegate messageDisplay = new MessageDisplayDelegate(this.MessageReceive);
this.Invoke(messageDisplay, message);
}
else
{
this.textBox2.Text += message;
}
}
特别注意黑体的代码,如果没有这行,则会出现cross-thread访问异常。