public class DispatchQueue
{
Queue<Action> queue = new Queue<Action> ();
static object key = new object ();
SingleThreadDispatcher dispatcher;
public DispatchQueue(SingleThreadDispatcher dispatcher) {
this.dispatcher = dispatcher;
}
public void QueueWork(Action work){
lock (key) {
queue.Enqueue (work);
}
if (this.dispatcher.Executed) {
this.dispatcher.Dispatch (this.Dequeue (), SynchronizationContext.Current, this.Callback);
}
}
private Action Dequeue(){
lock (key) {
if (queue.Count <= 0) {
return null;
}
return queue.Dequeue ();
}
}
private void Callback(object state) {
var work = this.Dequeue ();
if (work == null) return;
this.dispatcher.Dispatch (work, SynchronizationContext.Current, this.Callback);
}
}
public class SingleThreadDispatcher
{
Task task = null;
public void Dispatch(Action work, SynchronizationContext context, Action<object> callback) {
if (work == null || context == null || callback == null || !this.Executed) {
return;
}
Action safeWork = () => this.Execute (work);
this.task = new Task (safeWork);
this.task.ContinueWith ((result) => {
context.Post(new SendOrPostCallback(callback), null);
});
this.task.Start ();
}
public bool Executed {
get{
return (task == null) || (task != null && task.IsCompleted);
}
}
private void Execute(Action work)
{
try {
work();
}
catch(Exception e) {
//
}
} }
Wednesday, 21 January 2015
Tuesday, 30 September 2014
DFS
public class Node
{
public int Id { get; set; }
public String Name { get; set; }
public String Parent { get; set; }
[JsonIgnore]
public bool Visited { get; set; }
[JsonIgnore]
public List Children { get; set; }
}
public static class TreeManager
{
static Node tree = null;
static TreeManager()
{
tree = new Node();
}
public static Node Get()
{
return tree;
}
}
public static class TestData
{
static int id = 1;
public static Node Initialise()
{
var root = TreeManager.Get();
root.Id = Identifier;
root.Name = "Root";
root.Children = CreateDepartment();
CreateChildren(root.Children, "aisle");
CreateChildren(root.Children.SelectMany(c => c.Children).ToList(), "shelve");
//CreateChildren(root.Children.SelectMany(c => c.Children).SelectMany(c => c.Children).ToList(), "sub_shelve");
return root;
}
private static int Identifier
{
get
{
return id++;
}
}
private static List CreateDepartment()
{
string[] departments = new string[]
{
"Fresh Food", "Level 1 Mos Test", "Food cupboard test",
"Frozen food", "Drinks", "Drugstore", "Baby", "Pets", "Home and entertainment"
};
return Create(departments);
}
private static void CreateChildren(List nodes, string childName)
{
Random seeder = new Random(1);
foreach (var node in nodes)
{
int looper = seeder.Next(1, 10);
List names = new List();
while ((looper--) > 0)
{
names.Add(node.Name + "-" + childName + looper.ToString());
}
node.Children = new List();
node.Children.AddRange(Create(names.ToArray()));
}
}
private static List Create(string[] names)
{
List nodes = new List();
foreach (var name in names)
{
nodes.Add(new Node { Id = Identifier, Name = name });
}
return nodes;
}
}
//http://www.codeproject.com/Articles/32212/Introduction-to-Graph-with-Breadth-First-Search-BF
//http://stackoverflow.com/questions/687731/breadth-first-vs-depth-first
public static class DFSTraverse
{
static List orderedNodes = new List();
public static void Traverse(Node tree)
{
orderedNodes.Clear();
Stack stack = new Stack();
tree.Visited = true;
Push(stack, tree);
Print(stack.Peek());
while (!stack.IsEmpty())
{
Node node = GetChildNode(stack.Peek());
if (node == null) stack.Pop();
else
{
node.Visited = true;
Push(stack, node, stack.Peek().Name);
Print(stack.Peek());
}
}
}
public static string OrderedList()
{
return JsonConvert.SerializeObject(orderedNodes);
}
public static Node RebuildHierarchy(Stack stack)
{
Node root = stack.Peek();
BuildHierarchy(stack, stack.Pop());
return root;
}
private static void BuildHierarchy(Stack stack, Node parent)
{
if (parent == null) return;
Node addedNode = null;
while (!stack.IsEmpty())
{
if (stack.Peek().Parent.Equals(parent.Name))
{
if (parent.Children == null) parent.Children = new List();
addedNode = stack.Pop();
parent.Children.Add(addedNode);
}
else if (stack.Peek().Parent.Equals(addedNode.Name)) BuildHierarchy(stack, addedNode);
else return;
}
}
private static void AddToOrderedList(Node node)
{
orderedNodes.Add(node);
}
private static void Push(Stack stack, Node node, string parent = null)
{
stack.Push(node);
node.Parent = parent;
AddToOrderedList(node);
}
private static Node GetChildNode(Node node)
{
if (!node.Visited) return node;
if (node.Children == null) return null;
return node.Children.FirstOrDefault(n => n.Visited == false);
}
private static void Print(Node node)
{
Console.WriteLine(node.Id + ":" + node.Name);
}
private static bool IsEmpty(this Stack stack)
{
return stack.Count <= 0;
}
}
class Program
{
const string file = "Naviagtion.txt";
static void DemoDFSTraverse(Node root)
{
DFSTraverse.Traverse(root);
if (File.Exists(file)) File.Delete(file);
File.WriteAllText(file, DFSTraverse.OrderedList());
}
static Node RebuildHierarchy()
{
var result = JsonConvert.DeserializeObject(File.ReadAllText(file));
Stack node = new Stack(result.Reverse());
return DFSTraverse.RebuildHierarchy(node);
}
static bool AreEquals(Node node1, Node node2)
{
if (node1.Id == node2.Id && node1.Name.Equals(node2.Name))
{
int item = 0;
if (node1.Children == null && node2.Children == null) return true;
foreach (var node in node1.Children)
{
var otherNode1 = node1.Children[item];
var otherNode2 = node2.Children[item];
item++;
return AreEquals(otherNode1, otherNode2);
}
}
return false;
}
static void Main(string[] args)
{
Node originalNode = TestData.Initialise();
DemoDFSTraverse(originalNode);
Node rebuilededNode = RebuildHierarchy();
Console.WriteLine(AreEquals(originalNode, rebuilededNode));
Console.Clear();
DemoDFSTraverse(rebuilededNode);
Console.ReadLine();
}
{
public int Id { get; set; }
public String Name { get; set; }
public String Parent { get; set; }
[JsonIgnore]
public bool Visited { get; set; }
[JsonIgnore]
public List
}
public static class TreeManager
{
static Node tree = null;
static TreeManager()
{
tree = new Node();
}
public static Node Get()
{
return tree;
}
}
public static class TestData
{
static int id = 1;
public static Node Initialise()
{
var root = TreeManager.Get();
root.Id = Identifier;
root.Name = "Root";
root.Children = CreateDepartment();
CreateChildren(root.Children, "aisle");
CreateChildren(root.Children.SelectMany(c => c.Children).ToList(), "shelve");
//CreateChildren(root.Children.SelectMany(c => c.Children).SelectMany(c => c.Children).ToList(), "sub_shelve");
return root;
}
private static int Identifier
{
get
{
return id++;
}
}
private static List
{
string[] departments = new string[]
{
"Fresh Food", "Level 1 Mos Test", "Food cupboard test",
"Frozen food", "Drinks", "Drugstore", "Baby", "Pets", "Home and entertainment"
};
return Create(departments);
}
private static void CreateChildren(List
{
Random seeder = new Random(1);
foreach (var node in nodes)
{
int looper = seeder.Next(1, 10);
List
while ((looper--) > 0)
{
names.Add(node.Name + "-" + childName + looper.ToString());
}
node.Children = new List
node.Children.AddRange(Create(names.ToArray()));
}
}
private static List
{
List
foreach (var name in names)
{
nodes.Add(new Node { Id = Identifier, Name = name });
}
return nodes;
}
}
//http://www.codeproject.com/Articles/32212/Introduction-to-Graph-with-Breadth-First-Search-BF
//http://stackoverflow.com/questions/687731/breadth-first-vs-depth-first
public static class DFSTraverse
{
static List
public static void Traverse(Node tree)
{
orderedNodes.Clear();
Stack
tree.Visited = true;
Push(stack, tree);
Print(stack.Peek());
while (!stack.IsEmpty())
{
Node node = GetChildNode(stack.Peek());
if (node == null) stack.Pop();
else
{
node.Visited = true;
Push(stack, node, stack.Peek().Name);
Print(stack.Peek());
}
}
}
public static string OrderedList()
{
return JsonConvert.SerializeObject(orderedNodes);
}
public static Node RebuildHierarchy(Stack
{
Node root = stack.Peek();
BuildHierarchy(stack, stack.Pop());
return root;
}
private static void BuildHierarchy(Stack
{
if (parent == null) return;
Node addedNode = null;
while (!stack.IsEmpty())
{
if (stack.Peek().Parent.Equals(parent.Name))
{
if (parent.Children == null) parent.Children = new List
addedNode = stack.Pop();
parent.Children.Add(addedNode);
}
else if (stack.Peek().Parent.Equals(addedNode.Name)) BuildHierarchy(stack, addedNode);
else return;
}
}
private static void AddToOrderedList(Node node)
{
orderedNodes.Add(node);
}
private static void Push(Stack
{
stack.Push(node);
node.Parent = parent;
AddToOrderedList(node);
}
private static Node GetChildNode(Node node)
{
if (!node.Visited) return node;
if (node.Children == null) return null;
return node.Children.FirstOrDefault(n => n.Visited == false);
}
private static void Print(Node node)
{
Console.WriteLine(node.Id + ":" + node.Name);
}
private static bool IsEmpty
{
return stack.Count <= 0;
}
}
class Program
{
const string file = "Naviagtion.txt";
static void DemoDFSTraverse(Node root)
{
DFSTraverse.Traverse(root);
if (File.Exists(file)) File.Delete(file);
File.WriteAllText(file, DFSTraverse.OrderedList());
}
static Node RebuildHierarchy()
{
var result = JsonConvert.DeserializeObject
Stack
return DFSTraverse.RebuildHierarchy(node);
}
static bool AreEquals(Node node1, Node node2)
{
if (node1.Id == node2.Id && node1.Name.Equals(node2.Name))
{
int item = 0;
if (node1.Children == null && node2.Children == null) return true;
foreach (var node in node1.Children)
{
var otherNode1 = node1.Children[item];
var otherNode2 = node2.Children[item];
item++;
return AreEquals(otherNode1, otherNode2);
}
}
return false;
}
static void Main(string[] args)
{
Node originalNode = TestData.Initialise();
DemoDFSTraverse(originalNode);
Node rebuilededNode = RebuildHierarchy();
Console.WriteLine(AreEquals(originalNode, rebuilededNode));
Console.Clear();
DemoDFSTraverse(rebuilededNode);
Console.ReadLine();
}
Monday, 1 September 2014
Json.Net - Sample functions
Just for my reference
namespace MyJsonExample
{
public class Movies
{
public Movie[] All { get; set; }
}
public class Movie
{
public String Name { get; set; }
public DateTime ReleasedOn { get; set; }
[JsonIgnore]
public String Director { get; set; }
public MovieRating Rating { get; set; }
}
public enum MovieRating
{
R,
U,
PG
}
public static class Demo
{
public static string Serialize()
{
Movie m = new Movie { Director = "Suresh", Name = "First Movie", ReleasedOn = DateTime.Now, Rating = MovieRating.PG };
return JsonConvert.SerializeObject(m);
}
public static string SerializeArray()
{
Movie m1 = new Movie { Director = "Suresh", Name = "First Movie1", ReleasedOn = DateTime.Now, Rating = MovieRating.PG };
Movie m2 = new Movie { Director = "Suresh", Name = "First Movie2", ReleasedOn = DateTime.Now, Rating = MovieRating.R };
Movie[] movies = new Movie[] { m1, m2 };
Movies ms = new Movies { All = movies };
return JsonConvert.SerializeObject(ms);
}
public static void Deserialize(string m)
{
var movie = JsonConvert.DeserializeObject(m);
movie.ToString();
}
public static void ParseMovie(string m)
{
var movie = JObject.Parse(m);
var name = movie["Name"];
}
public static void ParseMovies(string m)
{
var movies = JObject.Parse(m);
var all = movies["All"];
var name = all[0]["Name"];
name = all[1]["Name"];
}
public static void ParseMoviesUsingLinq(string m)
{
var movies = JObject.Parse(m);
var all = movies["All"];
var movienames = (from movie in all
select (string)movie["Name"]).ToList();
var movienames2 = (from movie in movies["All"]
select new { N = (string)movie["Name"], R = movie["Rating"]});
foreach (var moviename in movienames)
{
Console.WriteLine(moviename);
}
foreach (var moviename in movienames2)
{
Console.WriteLine(moviename.N + ":" + moviename.R);
}
}
public static string SerializeEnum()
{
Movie m1 = new Movie { Director = "Suresh", Name = "First Movie1", ReleasedOn = DateTime.Now, Rating = MovieRating.PG };
return JsonConvert.SerializeObject(m1, new Newtonsoft.Json.Converters.StringEnumConverter());
}
}
}
namespace MyJsonExample
{
public class Movies
{
public Movie[] All { get; set; }
}
public class Movie
{
public String Name { get; set; }
public DateTime ReleasedOn { get; set; }
[JsonIgnore]
public String Director { get; set; }
public MovieRating Rating { get; set; }
}
public enum MovieRating
{
R,
U,
PG
}
public static class Demo
{
public static string Serialize()
{
Movie m = new Movie { Director = "Suresh", Name = "First Movie", ReleasedOn = DateTime.Now, Rating = MovieRating.PG };
return JsonConvert.SerializeObject(m);
}
public static string SerializeArray()
{
Movie m1 = new Movie { Director = "Suresh", Name = "First Movie1", ReleasedOn = DateTime.Now, Rating = MovieRating.PG };
Movie m2 = new Movie { Director = "Suresh", Name = "First Movie2", ReleasedOn = DateTime.Now, Rating = MovieRating.R };
Movie[] movies = new Movie[] { m1, m2 };
Movies ms = new Movies { All = movies };
return JsonConvert.SerializeObject(ms);
}
public static void Deserialize(string m)
{
var movie = JsonConvert.DeserializeObject
movie.ToString();
}
public static void ParseMovie(string m)
{
var movie = JObject.Parse(m);
var name = movie["Name"];
}
public static void ParseMovies(string m)
{
var movies = JObject.Parse(m);
var all = movies["All"];
var name = all[0]["Name"];
name = all[1]["Name"];
}
public static void ParseMoviesUsingLinq(string m)
{
var movies = JObject.Parse(m);
var all = movies["All"];
var movienames = (from movie in all
select (string)movie["Name"]).ToList
var movienames2 = (from movie in movies["All"]
select new { N = (string)movie["Name"], R = movie["Rating"]});
foreach (var moviename in movienames)
{
Console.WriteLine(moviename);
}
foreach (var moviename in movienames2)
{
Console.WriteLine(moviename.N + ":" + moviename.R);
}
}
public static string SerializeEnum()
{
Movie m1 = new Movie { Director = "Suresh", Name = "First Movie1", ReleasedOn = DateTime.Now, Rating = MovieRating.PG };
return JsonConvert.SerializeObject(m1, new Newtonsoft.Json.Converters.StringEnumConverter());
}
}
}
Monday, 7 July 2014
WAAD/ADAL/JWT References
http://msdn.microsoft.com/en-US/library/azure/jj554350.aspx
http://msdn.microsoft.com/library/azure/jj573266.aspx
http://www.cloudidentity.com/blog/2012/11/20/introducing-the-developer-preview-of-the-json-web-token-handler-for-the-microsoft-net-framework-4-5-2/
https://github.com/AzureADSamples
http://blogs.technet.com/b/ad/archive/2013/06/26/improved-windows-azure-active-directory-integration-with-asp-net-amp-visual-studio.aspx
http://blogs.office.com/2014/05/12/net-and-javascript-libraries-for-office-365-apis/
http://msdn.microsoft.com/library/azure/dn151790.aspx
http://msdn.microsoft.com/library/azure/jj573266.aspx
http://www.cloudidentity.com/blog/2012/11/20/introducing-the-developer-preview-of-the-json-web-token-handler-for-the-microsoft-net-framework-4-5-2/
https://github.com/AzureADSamples
http://blogs.technet.com/b/ad/archive/2013/06/26/improved-windows-azure-active-directory-integration-with-asp-net-amp-visual-studio.aspx
http://blogs.office.com/2014/05/12/net-and-javascript-libraries-for-office-365-apis/
http://msdn.microsoft.com/library/azure/dn151790.aspx
Wednesday, 7 May 2014
EclipseLink SqlExpress Connection Refused error
if you encounter this error “java.sql.SQLException: Network error IOException: Connection refused: connect SQL Express” while using EclipseLink to connect to SQL Express, just specifying the default SQL Port 1433 in SQL Server Configuration Manager

Subscribe to:
Posts (Atom)