Windows Workflow Foundation (WF4) - Workflow Services and Auto-Start

This sample application demonstrates how you can create a Workflow Service that will automatically start and periodically wake up to do some work. The sample also demonstrates a UI that shows the progress of the work and provides control and query capabilities.

C# (249.0 KB)
 
 
 
 
 
4 Star
(6)
4,298 times
Add to favorites
4/28/2011
E-mail Twitter del.icio.us Digg Facebook
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Default.aspx.cs" company="Microsoft">
//   Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <summary>
//   The default page
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace BatchWeb
{
    using System;
    using System.Web.UI;

    using BatchWeb.Services;

    using BatchWorker.Contracts;

    /// <summary>
    /// The default page
    /// </summary>
    public partial class Default : Page
    {
        #region Constants and Fields

        /// <summary>
        ///   The _batch job data.
        /// </summary>
        private BatchJobData batchJobData;

        #endregion

        #region Methods

        /// <summary>
        /// The link button refresh click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The event args.
        /// </param>
        protected void LinkButtonRefreshClick(object sender, EventArgs e)
        {
            this.Response.Redirect(this.Request.RawUrl);
        }

        /// <summary>
        /// The link button submit click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The event args.
        /// </param>
        protected void LinkButtonSubmitClick(object sender, EventArgs e)
        {
            int count;
            if (Int32.TryParse(this.TextBoxCount.Text, out count))
            {
                var startAt = this.GetStartAt();

                // Don't start if start at can't be determined
                if (startAt == DateTime.MinValue)
                {
                    return;
                }

                var request = new BatchRequest
                    {
                        CountTo = Int32.Parse(this.TextBoxCount.Text), 
                        Delay = TimeSpan.FromSeconds(Int32.Parse(this.TextBoxDelay.Text)), 
                        StartAt = startAt
                    };

                var proxy = new BatchWorkerClient();
                var response = proxy.SubmitJob(request);
                this.LabelMsg.Text = "Job submitted at " + DateTime.Now.ToUniversalTime() + " UTC";
                this.batchJobData.BatchJobs.Add(response);
                this.RefreshJobs();
            }
        }

        /// <summary>
        /// The page_ load.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The event args.
        /// </param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.TextBoxCount.Text = "100";
                this.TextBoxDelay.Text = "1";
                this.CheckBoxNow.Checked = true;
                this.TextBoxStart.Text = DateTime.Now.ToShortDateString();
                this.TextBoxTime.Text = (DateTime.Now + TimeSpan.FromMinutes(5)).ToShortTimeString();
                this.ValidateStartIsFuture.ValueToCompare = DateTime.Now.ToShortDateString();
            }

            this.batchJobData = this.Session["BatchJobs"] as BatchJobData;

            if (this.batchJobData == null)
            {
                this.batchJobData = new BatchJobData();
                this.Session["BatchJobs"] = this.batchJobData;
            }

            this.RefreshJobs();
        }

        /// <summary>
        /// The get start at.
        /// </summary>
        /// <returns>
        /// The date and time to start the batch job
        /// </returns>
        private DateTime GetStartAt()
        {
            if (!this.CheckBoxNow.Checked)
            {
                DateTime date;
                DateTime time;

                if (DateTime.TryParse(this.TextBoxStart.Text, out date) &&
                    DateTime.TryParse(this.TextBoxTime.Text, out time))
                {
                    date = date.AddHours(time.Hour);
                    date = date.AddMinutes(time.Minute);
                    date = date.AddSeconds(time.Second);
                    return date;
                }
                else
                {
                    this.LabelMsg.Text = "Unable to convert start date/time";
                    return DateTime.MinValue;
                }
            }

            return DateTime.Now;
        }

        /// <summary>
        /// The refresh jobs.
        /// </summary>
        private void RefreshJobs()
        {
            this.GridViewJobs.DataSource = this.batchJobData;
            this.batchJobData.QueryJobs();
            this.GridViewJobs.DataBind();
        }

        #endregion
    }
}