Question 1:
public void AsycStartDispatcher(Dispatcher dispatcher)
{
var thread = new Thread(
(input) =>
{
var dispatch = input as Dispatcher;
if (dispatch != null) dispatch.Start();
});
thread.Start(dispatcher);
}
Consider the above Code. We can see that we have written so many lines of code. In fact, it is just a simple task to complete, that is, to execute dispatch in a separate thread. the Start () method; is there a better way? Can I use only one row as below to complete this function?
public void AsycStartDispatcher(Dispatcher dispatcher)
{
dispatcher.AsycExec(obj => obj.Start());
}
Problem 1 solution:
If we use Lambda expressions, extension methods, and generics to add a method to an object, we can solve this problem. The specific code is as follows:
public static class ObjectExtensions
{
public static void AsycExec<T>(this T obj, Action<T> action)
{
action.BeginInvoke(obj, null, null);
}
}
However, we have not considered thread Exception Handling here.
Question 2:
public void StartAllDispatcher(IEnumerable<Dispatcher> dispatchers)
{
foreach (var dispatcher in dispatchers)
{
dispatcher.Start();
}
}
public void AsycStartAllDispatcher(IEnumerable<Dispatcher> dispatchers)
{
foreach (var dispatcher in dispatchers)
{
var thread = new Thread(
(input)
=>
{
var dispatch = input as Dispatcher;
if (dispatch != null) dispatch.Start();
});
thread.Start(dispatcher);
}
}
Can we directly execute a method for the entire Enumerable object without using Foreach for the above Code? The following is the example:
public void TestForeach(IEnumerable<Dispatcher> dispatchers)
{
dispatchers.ExecEach(obj => obj.Start());
dispatchers.AsycExecEach(obj => obj.Start());
}
Problem 2 solution:
public static class ObjectExtensions
{
public static void ExecEach<T>(this IEnumerable<T> objs, Action<T> action)
{
foreach (var obj in objs)
{
action(obj);
}
}
public static void AsycExecEach<T>(this IEnumerable<T> objs, Action<T> action)
{
foreach (var obj in objs)
{
action.BeginInvoke(obj, null, null);
}
}
}
Question 3:
lock (s_ObjectCachePools)
{
if (!s_ObjectCachePools.ContainsKey(poolName))
{
s_ObjectCachePools.Add(poolName, new List<Object>());
}
s_ObjectCachePools[poolName].Add(result);
}
For the s_ObjectCachePools object above, the lock (s_ObjectCachePools) statement is used for each operation. Can we write another statement? As shown below:
s_ObjectCachePools.LockExec(objs =>
{
if (!objs.ContainsKey(poolName))
{
objs.Add(poolName, new List<Object>());
}
objs[poolName].Add(result);
});
Problem 3 solution:
public static class ObjectExtensions
{
public static void LockExec<T>(this T obj, Action<T> action) where T : class
{
lock (obj)
{
action(obj);
}
}
}
Total Solution
public static class ObjectExtensions
{
public static void Exec<T>(this T obj, Action<T> action)
{
action(obj);
}
public static void AsycExec<T>(this T obj, Action<T> action)
{
action.BeginInvoke(obj,null,null);
}
public static void ExecEach<T>(this IEnumerable<T> objs, Action<T> action)
{
foreach (var obj in objs)
{
action(obj);
}
}
public static void AsycExecEach<T>(this IEnumerable<T> objs, Action<T> action)
{
foreach (var obj in objs)
{
action.BeginInvoke(obj,null,null);
}
}
public static void LockExec<T>(this T obj, Action<T> action) where T:class
{
lock (obj)
{
action(obj);
}
}
}