WF Learning Test 1-helloworld

Source: Internet
Author: User

I have never been in touch with and learn new technologies for a long time. I have to complain that my company is busy and I am making excuses for my laziness. People who work slowly are not the same as students, and sometimes have the same mentality! Think about it now and miss the hard work of hitting the keyboard late at night in the old age of students!
The process engine was not the first contact. Last year, a colleague and I were connected to the OA system of the 10 thousand section. At that time, the other party specified to use the k2.net process engine for development. We also provide a virtual machine with servers. At that time, I first came into contact with the process engine and had a more intuitive understanding of the concept of workflow !.
I went to the bookstore with my friend last Saturday and got two. net3.0 books. Chapter 1 describes how to use WF to create an order status management workflow. After reading it, I learned about WF and learned that Microsoft has integrated such a framework for vs2008. I personally feel that its application scope should be wider and wider. So I started to learn WF when I came back from the second world class.

Sit down and start with the question. At the beginning, I would like to introduce WF to a friend I don't understand (WWF is also known on the Internet, but Microsoft recommends WF)
Microsoft Windows Workflow Foundation (WF) is an extensible framework for developing workflow solutions on Windows platforms. It must be installed in vs2005. net3.0 framework and Visual Studio 2005 extensions for Windows workflow foundation.exe. WF is integrated in vs2008 by default.

I am not very fond of concepts or theoretical things. I like intuitive things and what I can see. helloworld is basically used to learn every kind of things.Program. Although old, I think it is useful. Today's note is written to the WF version of helloworld.
Step 2: Open vs2008 and create a new project workflow-> ordered workflow console program wfhelloworld

The project structure is as follows:

1. Where program. CS is the host program, that is, the console program can be understood as the main program!
2. workflow1.cs is a visual workflow designer. You can drag the activity controls in the toolbox to the designer, which is the main part of your workflow definition, the visual designer has the automatic detection function and prompts the corresponding exclamation point when the detection fails.
Here it is necessary to introduce the activity (activity) in WF ).
The main component of WF is activity, which forms the steps (or tasks) in the workflow. It can also be said that activity defines the workflow. we organize activities in order and layers, and then these activities will serve as instructions for the workflow engine to execute. all the activities in WF inherit a base class-activity, and WF operates on the ready-made activities in these basic activity libraries. windows workflow allows developers to expand the basic activity library and solve problems in specific fields by creating custom activities.
We can define actitvity by ourselves, and Microsoft also provides some actitvity

After learning about this, let's proceed.

Step 2: drag a codeactivity to workflow1.cs. Note that there is an exclamation point in the figure. Here is the detection function of the workflow designer mentioned above, for example:

This is because the code activity, we do not define its executecodeCode)

Enter a method name in executecode, and the program will automatically generate an event method.

 

/* **************************************** *******
// Author: fengyan
// Blog: Http://eflylab.cnblogs.com
// Date:
/*************************************** ******* */  
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;

namespace wfhelloworld
{< br> Public sealed partial class workflow1: sequentialworkflowactivity
{< br> Public workflow1 ()
{< br> initializecomponent ();
}

Private VoidExecutehello (ObjectSender, eventargs E)
{
System. Console. writeline ("Hello World");
}
}

}

Let's enter a code for the method, which is easy to output!
Then we will go back to the Code body in program. CS. Static   Void Main ( String [] ARGs)
{
Using (Workflowruntime =   New Workflowruntime ())
{
Autoresetevent waithandle =   New Autoresetevent ( False );
Workflowruntime. workflowcompleted + =   Delegate ( Object Sender, workflowcompletedeventargs e) {waithandle. Set ();};
Workflowruntime. workflowterminated + =   Delegate ( Object sender, workflowterminatedeventargs e)
{< br> console. writeline (E. exception. message);
waithandle. set ();
};

Workflowinstance instance=Workflowruntime. createworkflow (Typeof(Wfhelloworld. workflow1 ));
Instance. Start ();

Waithandle. waitone ();
}
}

This is automatically generated by the tool. Don't worry about them. Let's make some modifications at the end,

Static   Void Main ( String [] ARGs)
{
// Workflowruntime: provides an engine for running a workflow in the running environment (host) of the workflow.
Using (Workflowruntime =   New Workflowruntime ())
{
Autoresetevent waithandle =   New Autoresetevent ( False );
// The following two events are events after and after the completion of an instance in the engine
Workflowruntime. workflowcompleted + =   Delegate ( Object Sender, workflowcompletedeventargs e) {waithandle. Set ();};
Workflowruntime. workflowterminated + =   Delegate ( Object Sender, workflowterminatedeventargs E)
{
Console. writeline (E. Exception. Message );
Waithandle. Set ();
};

//Workflowinstance: A workflow instance running in the workflow engine.
Workflowinstance instance=Workflowruntime. createworkflow (Typeof(Wfhelloworld. workflow1 ));
Instance. Start ();

Waithandle. waitone ();

//Wait for user response to exit
Console. writeline ("Press any key to exit");
Console. Readline ();
}
}

Add two lines at the end to facilitate viewing! Run now, F5

Enter helloworld.
This may be the simplest wfhelloword. Add a slight modification to add a simple interaction function.
We add an attribute username in workflow1.

Private   String Username =   "" ;
Public   String Username
{
Get { Return Username ;}
Set {Username = Value ;}

}
PublicWorkflow1 ()
{
Initializecomponent ();
}

Private VoidExecutehello (ObjectSender, eventargs E)
{
System. Console. writeline ("{0}, hello World", Username );
}

Modifying program. CS   Static   Void Main ( String [] ARGs)
{
// Workflowruntime: provides an engine for running a workflow in the running environment (host) of the workflow.
Using (Workflowruntime =   New Workflowruntime ())
{
Autoresetevent waithandle =   New Autoresetevent ( False );
// The following two events are events after and after the completion of an instance in the engine
Workflowruntime. workflowcompleted + =   Delegate ( Object Sender, workflowcompletedeventargs e) {waithandle. Set ();};
Workflowruntime. workflowterminated + =   Delegate ( Object Sender, workflowterminatedeventargs E)
{
Console. writeline (E. Exception. Message );
Waithandle. Set ();
};


Console. writeline ( " Press enter your Username " );
Dictionary < String , Object > Dictionary =   New Dictionary < String , Object > ();
// Receive user input
String Username = Console. Readline ();
// Note that the following username must be exactly the same as the attribute name in workflow1.
Dictionary. Add ( " Username " , Username );
// Workflowinstance: A workflow instance running in the workflow engine.
// Create with overload
Workflowinstance instance = Workflowruntime. createworkflow ( Typeof (Wfhelloworld. workflow1), dictionary );
Instance. Start ();

Waithandle. waitone ();

//Wait for user response to exit
Console. writeline ("Press any key to exit");
Console. Readline ();
}
}

Running Effect

Downlaod demo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.