ここでは、WF (Windows Workflow Foundation) を使用したアプリケーション開発の方法と、ワークフローサービスの活用のポイントをご説明します。
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using System.Windows.Forms;
namespace WorkflowLibrary1
{
public sealed partial class Workflow1: SequentialWorkflowActivity
{
public string notCompleteString = "まだ途中です";
public string completeString = "完了です";
public Workflow1()
{
InitializeComponent();
}
DialogResult res = DialogResult.No;
private void codeActivity1_ExecuteCode(object sender, EventArgs e)
{
res = MessageBox.Show("終了しますか", "ワークフローテスト", MessageBoxButtons.YesNo);
}
private void LoopCheck(object sender, ConditionalEventArgs e)
{
if (res == DialogResult.Yes)
e.Result = false;
else
e.Result = true;
}
}
[ExternalDataExchange]
public interface ITestExchange
{
void CallExternalMethodTest(string stat);
event EventHandler<ExternalDataEventArgs> handleExternalEventTest;
}
}using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Workflow.Runtime;
using WorkflowLibrary1;
using System.Workflow.Activities;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
WorkflowRuntime wr = new WorkflowRuntime();
TestExchangeService testServ;
private void Form1_Load(object sender, EventArgs e)
{
ExternalDataExchangeService exServ = new ExternalDataExchangeService();
wr.AddService(exServ);
testServ = new TestExchangeService(this);
exServ.AddService(testServ);
wr.StartRuntime();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
wr.StopRuntime();
}
Guid workflowId;
private void button1_Click(object sender, EventArgs e)
{
WorkflowInstance wi = wr.CreateWorkflow(typeof(Workflow1));
workflowId = wi.InstanceId;
wi.Start();
}
private void button2_Click(object sender, EventArgs e)
{
testServ.FireEvent(workflowId);
}
public void showMessage(string stat)
{
MessageBox.Show(stat);
}
delegate void asyncShowMessage(string stat);
public void invokeShowMessage(string stat)
{
Invoke(new asyncShowMessage(showMessage), new object[] { stat });
}
}
public class TestExchangeService : ITestExchange
{
Form1 targetForm;
public TestExchangeService(Form1 form)
{
targetForm = form;
}
public void CallExternalMethodTest(string stat)
{
targetForm.invokeShowMessage(stat);
}
public event EventHandler<ExternalDataEventArgs> handleExternalEventTest;
internal void FireEvent(Guid workflowId)
{
handleExternalEventTest.Invoke(null, new ExternalDataEventArgs(workflowId));
}
}
}