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访问异常。