23 November 2011

Barrier: synchronizes threads in freamwork 4

Question:

How to build a barrier like a a traffic light for threads and synchronize them?

Solution:

That's what brought us to the barrier of framework 4!



I built code that gets three threads and trying to sync.
There are three types of situations:
1. Green - do not get their delayed and continue normal (driving).
2. Yellow - paused for a second and then goes through the green.
3. Red - delayed for 2 seconds and then goes through the yellow color.


The following code shows a good example of a barrier. Sync object is a barrier. It causes delay by type situation. Join activates the trigger of the action (TrafficLightsAction).

using System;
using
 System.Collections.Generic;
using
 System.Linq;
using
 System.Text;
using
 System.Threading;
using
 System.Threading.Tasks;
namespace
 BarrierTrafficLights
{
     class Program

     {
         static Barrier sync;
         static void Main(string[] args)
         {
             sync = new Barrier(participantCount: 3);
             var Car1 = new Thread( () =>
             {
                 TrafficLightsAction("Car 1 (green light source)"colorTrafficLights.green, true);
             });
             Car1.Start();
             var Car2 = new Thread(() =>
             {
                 TrafficLightsAction("Car 2 (yellow light source)"colorTrafficLights.yellow, true);
             });
             Car2.Start();
             var Car3 = new Thread(() =>
             {
                 TrafficLightsAction("Car3 (red light source)"colorTrafficLights.red, true);
             });
             Car3.Start();


             Car1.Join();
             Car2.Join();
             Car3.Join();

             sync.Dispose();
             Console.ReadKey();
         }
         static void TrafficLightsAction(string name, colorTrafficLights color, bool firstTime)
         {
             if (firstTime)
                 Console.WriteLine("{0} has reached the intersection with traffic lights", name, color);
             switch (color)
             {
                 case colorTrafficLights.green:
                     Console.WriteLine("{0} drove a green light", name);
                     break;
                 case colorTrafficLights.yellow:
                     Console.WriteLine("{0} is at a yellow light ", name);
                     Thread.Sleep(1);
                     TrafficLightsAction(name, colorTrafficLights.green, false);
                     break;
                 case colorTrafficLights.red:
                     Console.WriteLine("{0} is at a red light ", name);
                     Thread.Sleep(2);
                     TrafficLightsAction(name, colorTrafficLights.yellow, false);
                     break;
             }
             sync.SignalAndWait();
         }
     }
     enum colorTrafficLights

     {
         green, yellow, red
     }
}

Hope you understood,
Roi

04 November 2011

Server is too busy

This time I came across a seemingly simple problem
I received the following message at the portal

Server is too busy


CPU processor of Windows SharePoint Server yield was 100% !!!

CPU 100%

Thanks to RRaveen - How to resolve the “Server Too Busy” error in asp.net, I understand what the problem.
Previously explained how to solve a problem of "large data transfer in web service"
I showed how to change executionTimeout on the web.config

To correct the error - You need to change the variable "executionTimeout" to 3600 (this an hour because it a number of seconds - 60 * 60 = 3600)

Go to the web.config and set the tag httpRuntime the following code on the "system.web" tag.

<system.web>
  <httpRuntime executionTimeout="3600" maxRequestLength="51200">
...
This should solve the problem. If not,
Should reduce the number (the default is 110 seconds).

thanks,
Roi