Tuesday, May 31, 2016

Modal center - web application

 (function ($) {
        "use strict";
        function centerModal() {
            $(this).css('display', 'block');
            var $dialog = $(this).find(".modal-dialog"),
            offset = ($(window).height() - $dialog.height()) / 2,
            bottomMargin = parseInt($dialog.css('marginBottom'), 10);

            if (offset < bottomMargin) offset = bottomMargin;
            $dialog.css("margin-top", offset);
        }

        $(document).on('show.bs.modal', '.modal', centerModal);
        $(window).on("resize", function () {
            $('.modal:visible').each(centerModal);
        });
    }(jQuery));

Parallelism in C#

Types of Parallelism in C#


C# supports two main models of parallelism:
  1. Data parallelism: where an operation is applied to each element in a collection.
  2. Task parallelism: where independent computations are executed in parallel.
A sequential for loop in C#:
int n = ...
for (int i = 0; i<=n; i++)
{
// ...
});

A parallel for loop in C#:
int n = ...
Parallel.For(0, n, i =>
{
// ...
});

  • The language construct for is translated into a(higher-order) function Parallel.For.
  • The argument to Parallel.For is an anonymous method, specifying the code to be performed in each loop iteration.
  • The arguments to this anonymous method are the start value, the end value and the iteration variable.


We can limit the degree of parallelism like this:
var options = new ParallelOptions() {
MaxDegreeOfParallelism = 2 };
Parallel.For(0, n, options, i =>
{
fibs[i] = Fib(i);
});


Parallel loops have two ways to break or stop a loop
instead of just one.
  1. Parallel break, loopState.Break(), allows all steps with indices lower than the break index to run before terminating the loop.
  2. Parallel stop, loopState.Stop(), terminates the loop without allowing any new steps to begin. 



Monday, May 23, 2016

C# Random repeating issue - solution

using System;
using System.Threading;

namespace SubProj
{
    public static class RandomProvider
    {
        private static int seed = Environment.TickCount;

        private static ThreadLocal<Random> randomWrapper = new ThreadLocal<Random>
            (() => new Random(Interlocked.Increment(ref seed)));

        public static Random GetThreadRandom()
        {
            return randomWrapper.Value;
        }
    }
}


calling:

private string RandomString(int length)
        {
            const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
            var random = RandomProvider.GetThreadRandom();
            return new string(Enumerable.Repeat(chars, length)
              .Select(s => s[random.Next(s.Length)]).ToArray());
        }

        private string RandomBoolean()
        {
            string[] strData = { "true", "false" };
            Random r = RandomProvider.GetThreadRandom();
            int index = r.Next(strData.Length);
            return strData[index];
        }

        private string RandomInt(int length)
        {
            const string chars = "0123456789";
            var random = RandomProvider.GetThreadRandom();
            return new string(Enumerable.Repeat(chars, length)
              .Select(s => s[random.Next(s.Length)]).ToArray());
        }

Encrypt/Decrypt the App.Config

Program.cs using System; using System.Diagnostics; using System.IO; namespace EncryptAppConfig {     internal class Program     {         pr...