Using WHILE Activity in WWF!
This is in continuation with my last post. Now create a new workflow sequential class library. Add One CODE Activity, name it as CA_EnterMinMaxNumber, then add a WHILE Activity and put a CODE activity inside it, name it as CA_ShowPrimeNo. After putting everything on workflow, your workflow would looks like this :-
Now add public boolean variable (KeepRunning) in the class.this variable to be used by WHILE activity to keep running. Configure the while activity window like this.
Now add function for both code activity and paste following code:-
public sealed partial class WhileWF: SequentialWorkflowActivity
{
public WhileWF()
{
InitializeComponent();
}
private bool _bKeepRunning=true;
public bool KeepRunning
{
get { return _bKeepRunning; }
set { _bKeepRunning= value; }
}
int iMin = -1, iMax = -1;
private void CA_EnterMinMaxNumber_ExecuteCode(object sender, EventArgs e)
{
string sLine;
Console.WriteLine();
Console.Write("Do you want to generate PrimeNo [Y/N]");
if (Console.ReadKey(false).Key == ConsoleKey.Y)
{
Console.WriteLine();
Console.WriteLine("Enter Minimum Number:");
sLine = Console.ReadLine();
if (!int.TryParse(sLine, out iMin))
{
iMin = 1;
}
Console.WriteLine("Enter Maximum Number:");
sLine = Console.ReadLine();
if (!int.TryParse(sLine, out iMax))
{
iMax = 100;
}
if (iMin > iMax)
{
int tmp = iMax;
iMax = iMin;
iMin = tmp;
}
}
else
KeepRunning = false;
}
private void CA_ShowPrimeNo_ExecuteCode(object sender, EventArgs e)
{
Console.WriteLine("Prime No between {0} to {1} are:-", iMin,iMax);
bool bisPrime = true;
for (int i = iMin; i < iMax; i++)
{
for (int xStart = 2; xStart < i - 1; xStart++)
{
if (i % xStart == 0)
{
bisPrime = false;
break;
}
}
if (bisPrime)
{
Console.WriteLine("{0}", i);
}
bisPrime = true;
}
CA_EnterMinMaxNumber_ExecuteCode(this, null);
}
}
Output:-
Do you want to generate PrimeNo [Y/N]y Enter Minimum Number: 5 Enter Maximum Number: 20 Prime No between 5 to 20 are:- 5 7 11 13 17 19 Do you want to generate PrimeNo [Y/N]n
For running library, refer code from linked article.
Using Window WF [IF-ELSE ACTIVITY] with Managed VC
Due to change in server, I lost almost one month blog data (some of the WF posts also). Anyways here again I am posting it again, this time writing WF library in C# and running it using managed C++.
Program topic: - According to latest guidelines by Government of India “Hard drinks would be served only to person equal or above 25 years”. So our WF application will ask incoming guest there AGE and respond accordingly!
Create C# based Sequential Workflow Library, something like this:-
After selecting the project name, following window will open:-
Now put Code activity and name it as CA_EnterAge and then add IF-ELSE activity, also add two code activity into the IF-ELSE activity for each true and false condition and name CA_AgeAbove25 and CA_AgeBelow25 activity respectivly. The workflow now look likes this:-
Now, place following code into Workflow source file after generating function for each code activity:-
public sealed partial class Workflow1: SequentialWorkflowActivity
{
public Workflow1()
{
InitializeComponent();
}
private bool _AgeBarrierOk = false;
public bool AgeBarrierOk
{
get { return _AgeBarrierOk; }
set { _AgeBarrierOk = value; }
}
private void CA_EnterAge_ExecuteCode(object sender, EventArgs e)
{
Console.WriteLine("Welcome to Beer Bar");
Console.WriteLine("Before choosing drink, Enter your Age:");
string sLine = Console.ReadLine();
int iAge = 0;
int.TryParse(sLine, out iAge);
if (iAge >= 25) { AgeBarrierOk = true; } else AgeBarrierOk = false;
}
private void CA_AgeAbove25_ExecuteCode(object sender, EventArgs e)
{
Console.WriteLine("Welcome, you allowed to take drinks");
}
private void CA_AgeBelow25_ExecuteCode(object sender, EventArgs e)
{
Console.WriteLine("Sorry, you haven't crossed harddrink age limit");
Console.WriteLine("You can only have Softdrink or Juice");
}
}
Now add declarative rule condition for IF-ELSE activity for selecting true or false branch. Something like this:-
If you see in snapshot I am using Boolean variable AgeBarrierOk to check whether to select TRUE branch or FALSE branch. Now compile and remove any compiler errors.
Now Add CLR Console based project into our project, add reference of the library and Workflow assemblies.
Following code snippet will cater/manage our workflow:-
using namespace System;
using namespace System::Threading;
using namespace System::Workflow::Runtime;
using namespace System::Workflow::Runtime::Hosting;
using namespace SeqIFElseLibrary;
ref class WorkflowRunner
{
static AutoResetEvent ^waitHandle = gcnew AutoResetEvent(false);
WorkflowRuntime ^workflowRuntime;
public:
WorkflowRunner()
{
workflowRuntime = gcnew WorkflowRuntime();
}
void RunWorkflow()
{
Type^ t = Workflow1::typeid;
workflowRuntime->WorkflowCompleted +=
gcnew EventHandler<WorkflowCompletedEventArgs^>(&WorkflowRunner::OnWorkflowCompleted);
WorkflowInstance^ instance = workflowRuntime->CreateWorkflow(t);
instance->Start();
waitHandle->WaitOne();
}
static void OnWorkflowCompleted(Object ^ Sender,WorkflowCompletedEventArgs^ e)
{
waitHandle->Set();
}
};
int main(array<System::String ^> ^args)
{
WorkflowRunner^ wfr = gcnew WorkflowRunner();
wfr->RunWorkflow();
return 0;
}
Output [TRUE Condition]:-
Welcome to Beer Bar
Before choosing drink, Enter your Age:
28
Welcome, you allowed to take drinks
Output [FALSE Condition]:-
Welcome to Beer Bar
Before choosing drink, Enter your Age:
21
Sorry, you haven't crossed harddrink age limit
You can only have Softdrink or Juice
Download code Visualcpp_org_IFELSE_Act.zip
Is there is any way to convert ASM program into C or C++ code?
Yes you can, there are many third party software available on internet, some of them are :-
This is not a advertisement, there might be more tool available on Internet. would update the list once find more!
C# typeof in Managed C++
There are two ways to find type of any DataType, those are :-
- __typeof
- DataType::typeid
Read more here
Enumerating files in any particular directory!
Enumerating files in any particular directory is task almost every programmer had done at-least once in his programming lifetime whether it is project demand or learning.
I still remember getting all files of a particular directory on console screen in old Turbo Cpp days, though it crashed afterward due to my own silly mistake
. Anyways here code snippet of enumerating file from directory from both Un-Managed and Managed world.
Code Snippet (Un-Managed C++ using MFC Class):-
// CFileFind is MFC class for finding files in Window OS
CFileFind _FileFinder;
/// Search in C:\Temp
if(_FileFinder.FindFile(L"C:\\temp\\*.*"))
{
while(_FileFinder.FindNextFileW())
{
// Output all the file on debug screen
OutputDebugString(_FileFinder.GetFilePath());
}
}
// Close file find handle.
_FileFinder.Close();
Code Snippet (Managed C++):-
using namespace System;
using namespace System::IO;
// Search in C:\Temp
DirectoryInfo ^ drInfo = gcnew DirectoryInfo(L"C:\\temp");
// Get all file information from DirectoryInfo Object
cli::array<FileInfo> ^ flINo = drInfo->GetFiles();
for(int i=0;i<flINo->;Length;i++)
{
//Output all the file on debug screen
Console::WriteLine(flINo[i]->FullName);
}
8 Features of Badly Designed Software!
Very good post for programmer from Christopher Nadeau ,
Read more here
Getting current running executable directory!
these code snippet will show, different between two generation of programming:-
In good ol' MFC days:-
TCHAR szModulePath[MAX_PATH]={0};
//Get Path of module executable
GetModuleFileName(NULL,szModulePath,MAX_PATH);
// remove file spec
PathRemoveFileSpec(szModulePath);
//Show name
MessageBox(szModulePath);</pre>
In VC.Net (Dot Net 2.0)
// Get Running executable Path String ^sPath= System::Reflection::Assembly::GetCallingAssembly()->Location; FileInfo ^fiPath = gcnew FileInfo(sPath); // Get Directory Name Console::WriteLine(fiPath->DirectoryName);





