0

I am using rehosted-designer of WF 4.5 to give an interface for the users to create a workflow with custom activities and some of the inbuilt activities of the framework.

I am using AsyncCodeActivity model (perfect fit for my requirement), and hence is bound to execute a workflow as below, i mean execution is triggered at once without the possibility to iterate the activities in the workflow:

WorkflowApplication wf = new WorkflowApplication(activeFlowChart);
............
............
var result = wf.BeginRun(null,null);
while (!result.IsCompleted)
{
Thread.Sleep(1);
continue;
}
wf.EndRun(result);

My workflow can have multiple "WriteLine" activities scattered in the workflow. I want to retrieve the message of a particular "WriteLine" and show in the trace window as in the order how it appears in the workflow.

I have tried as below on completion, which displays all the "WriteLine" messages appended at the end of the execution:

wf.Extensions.Add(writer);
wf.Completed = arg =>
{
if (!string.IsNullOrEmpty(writer.ToString()))
{
//display
}
};

I am looking for a way to get the "WriteLine" message immediately when it occurs in the workflow, not on completion.

Pankaj_Sen
  • 61
  • 9

1 Answers1

0

The below link helped me to think about writing custom TextWriter to solve the above usecase:

Redirecting Console.WriteLine() to Textbox

Instead of appending the characters to the "writer" object and print it to the display window when the workflow is completed (refer the code in my question), i wrote a custom TextWriter to print the message of each "WriteLine" activity when a new line("\r\n") is encountered. example below:

in constructor:

var writer = new WriteLineTextWriter(DisplayTraceMessage);
Console.SetOut(writer);

custom TextWriter:

public class WriteLineTextWriter : TextWriter
{
private Action<string, Brush> WriteTraceMessageToGUI { get; set; }
private StringBuilder line;
private byte counter;

public WriteLineTextWriter(Action<string, Brush> action)
{
WriteTraceMessageToGUI = action;
line = new StringBuilder();
}

public override System.Text.Encoding Encoding
{
get { return System.Text.Encoding.Unicode; }
}

public override void Write(char value)
{
if (value == '\r' || value == '\n')
{
counter++;
}
else
{
line.Append(value.ToString());
}
if (counter == 2)
{
WriteTraceMessageToGUI(line.ToString(), Brushes.BlueViolet);
line.Clear();
counter = 0;
}
}

public override void Write(string value)
{
WriteTraceMessageToGUI(value, Brushes.BlueViolet);
}
}
Pankaj_Sen
  • 61
  • 9