venerdì 31 maggio 2013

How to: C# Lazy ( Side effect )

Recentemente mi sono confrontato con un cordiale discorso di performance.

La maggior parte delle nostre applicazione vive l'ansia di dover caricare dei dati, e a dispetto delle mille possibilità  (leggasi "entity framework" ) non sempre possiamo rivolgerci ad estrattori specifici, o di contro questi sono rappresentati da elementi che wrappano i vare ef.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LazyTester
{
    public class entity
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }    
    }

    public class entites : List<entity>
    {
        public entites()
        {
            this.Add( new entity(){
                    ID=1,
                    Name = "Fabio",
                    Surname ="Arosio"});

            this.Add( new entity(){
                    ID=2,
                    Name = "Claudio",
                    Surname ="Arosio"});
            
            this.Add( new entity(){
                    ID=3,
                    Name = "Lorenzo",
                    Surname ="Arosio"});
            
            this.Add( new entity(){
                    ID=4,
                    Name = "Federico",
                    Surname ="Arosio"});
            
            Console.WriteLine("done");
        }    
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Lazy...");
            Lazy<entites> ldsE = new Lazy<entites>();
                       

            Console.WriteLine("Normal...");
            entites dsE = new entites();
          
            Console.WriteLine("Normal:"+ dsE.Count.ToString());

            Console.WriteLine("Lazy:" + ldsE.Value.Count.ToString());

            Console.ReadLine();
        }
    }
}


Questo esempietto anche se molto molto scarno presenta il vantaggio di un costruttore Lazy.

già nel nome ben si comprende che cosa farà ma in breve possiamo ipotizzare che quanto costruito come lazy non sarà disponibile fin da subito.
Tanto che la "done" sulla console appare appena prima di della WriteLine Lazy.

Supponiamo giusto per dare visibiltà della situazione che il caricamento differito dei dati avrà luogo solo quando questi saranno necessari e di contro questo "sposta il tempo d'accesso" solo e soltanto a quando questi servono davvero.


var ldsEE = new Lazy<entites>().Value.Where (c=> c.Name.StartsWith("F"));


Ci è lecito pensare e implementare anche in questo modo .. ma perdendone qualche beneficio..

Sicuramente credo che sia sempre cosa buona e giusta sperimentare. !

giovedì 30 maggio 2013

How TO: C# internal WEB CAM r2

La prima versione mi è piaciuta ma onestamente non mi ha soddisfatto, perchè il sorgente era strettamente legato al form, oggi con un filo di calma in più ( giocandomi la pausa ) sono riuscito a migliorare il tutto e il gioco sta nel come gestire le cose.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace CameraCapture.Objects
{
    public class HandledObject
    {
        public int width;
        public int height;
        public Size clientSize;
        public IntPtr handle; 
    }
}


Questo primo oggetto mi serve come tramite per la gestiione dell'Handle... quasi mi sembra di tornare ai fasti delle API a 32bit.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CameraCapture.Objects
{
    public enum PlayState
    {
        Stopped,
        Paused,
        Running,
        Init
    }
}


Questo è rimasto invariato.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DirectShowLib;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;

namespace CameraCapture.Objects
{
    public class DirectShowWrapper
    {

        public int D = Convert.ToInt32("0X8000", 16);

        public int WM_GRAPHNOTIFY = 0;

        public IVideoWindow VideoWindow = null;
        public IMediaControl MediaControl = null;
        public IMediaEventEx MediaEventEx = null;
        public IGraphBuilder GraphBuilder = null;
        public ICaptureGraphBuilder2 CaptureGraphBuilder = null;

        public DsROTEntry rot = null;
        private PlayState CurrentState = PlayState.Stopped;

        public DirectShowWrapper()
        {
            WM_GRAPHNOTIFY = D + 1;
        }

        public void GetInterfaces(HandledObject f)
        {
            int hr = 0;
            this.GraphBuilder = (IGraphBuilder)(new FilterGraph());
            this.CaptureGraphBuilder = (ICaptureGraphBuilder2)(new CaptureGraphBuilder2());
            this.MediaControl = (IMediaControl)(this.GraphBuilder);
            this.VideoWindow = (IVideoWindow)(this.GraphBuilder);
            this.MediaEventEx = (IMediaEventEx)(this.GraphBuilder);
            hr = this.MediaEventEx.SetNotifyWindow(f.handle, 
                                    WM_GRAPHNOTIFY, 
                                    IntPtr.Zero);
        }

        public void CaptureVideo(HandledObject f)
        {
            int hr = 0;
            IBaseFilter sourceFilter = null;
            try
            {
                GetInterfaces(f);

                hr = this.CaptureGraphBuilder.SetFiltergraph(this.GraphBuilder);


                sourceFilter = FindCaptureDevice();

                hr = this.GraphBuilder.AddFilter(sourceFilter, "Video Capture");


                hr = this.CaptureGraphBuilder.RenderStream(PinCategory.Preview,
                        MediaType.Video,
                        sourceFilter,
                        null, null);

                Marshal.ReleaseComObject(sourceFilter);

                SetupVideoWindow(f);

                rot = new DsROTEntry(this.GraphBuilder);

                hr = this.MediaControl.Run();

                this.CurrentState = PlayState.Running;

            }
            catch (Exception ex)
            {

            }
        }

        public void SetupVideoWindow(HandledObject f)
        {
            int hr = 0;

            hr = VideoWindow.put_Owner(f.handle);


            hr = VideoWindow.put_WindowStyle(WindowStyle.Child | WindowStyle.ClipChildren);

            ResizeVideoWindow(f);

            hr = VideoWindow.put_Visible(OABool.True);
        }

        public void closeinterfaces()
        {

            if (this.MediaControl != null)
            {
                this.MediaControl.StopWhenReady();
            }

            this.CurrentState = PlayState.Stopped;


            if (this.MediaEventEx != null)
            {
                this.MediaEventEx.SetNotifyWindow(IntPtr.Zero, 
                        WM_GRAPHNOTIFY, 
                        IntPtr.Zero);
            }


            if (this.VideoWindow != null)
            {
                this.VideoWindow.put_Visible(OABool.False);
                this.VideoWindow.put_Owner(IntPtr.Zero);
            }


            if (rot != null)
            {
                rot.Dispose();
                rot = null;
            }


            Marshal.ReleaseComObject(this.MediaControl);
            Marshal.ReleaseComObject(this.MediaEventEx);
            Marshal.ReleaseComObject(this.VideoWindow);
            Marshal.ReleaseComObject(this.GraphBuilder);
            Marshal.ReleaseComObject(this.CaptureGraphBuilder);

            this.MediaControl = null;
            this.MediaEventEx = null;
            this.VideoWindow = null;
            this.CaptureGraphBuilder = null;
            this.GraphBuilder = null;
        }



        public void HandleGraphEvent()
        {
            int hr = 0;
            EventCode evCode;
            int evParam1;
            int evParam2;

            if (MediaEventEx == null)
            {
                return;
            }

            while (MediaEventEx.GetEvent(out evCode, out evParam1, out evParam2, 0) == 0)
            {
                hr = MediaEventEx.FreeEventParams(evCode, evParam1, evParam2);
            }
        }

        public IBaseFilter FindCaptureDevice()
        {

            int hr = 0;
            IEnumMoniker IclassEnum;
            System.Runtime.InteropServices.UCOMIEnumMoniker classEnum;

            IMoniker[] moniker = new IMoniker[1];

            Object source = null;
            ICreateDevEnum devEnum = (ICreateDevEnum)(new CreateDevEnum());

            hr = devEnum.CreateClassEnumerator(FilterCategory.VideoInputDevice,
                out classEnum, 0);


            IclassEnum = (IEnumMoniker)(classEnum);

            Marshal.ReleaseComObject(devEnum);

            if (classEnum == null)
            {
                throw new ApplicationException("No device video on board.");
            }
            if (IclassEnum.Next(moniker.Length, moniker, IntPtr.Zero) == 0)
            {
                Guid iid = typeof(IBaseFilter).GUID;
                moniker[0].BindToObject(null, null, iid, out source);
            }
            else
            {
                throw new ApplicationException("Unable to access video capture device!");
            }
            Marshal.ReleaseComObject(moniker[0]);
            Marshal.ReleaseComObject(classEnum);
            return (IBaseFilter)(source);
        }


        public void ChangePreviewState(bool showVideo)
        {
            int hr = 0;

            if (this.MediaControl == null)
            {
                return;
            }
            if (showVideo == true)
            {
                if (this.CurrentState != PlayState.Running)
                {
                    hr = this.MediaControl.Run();
                    this.CurrentState = PlayState.Running;
                }
            }
            else
            {

                hr = this.MediaControl.StopWhenReady();
                this.CurrentState = PlayState.Stopped;
            }
        }

        public void ResizeVideoWindow(HandledObject ho)
        {
            if (VideoWindow != null)
            {
                VideoWindow.SetWindowPosition(0, 0, ho.width, ho.clientSize.Height);
            }
        }
    }
}



Questo è l'oggetto che sostituisce e wrappa tutte le attività lasciando libero e snello il form.
Il motivo è semplice, in questo modo posso aggiungere una picturebox o qualsiasi altro oggetto per cui sussistano dimensioni e handle, mapparlo nell'oggetto di scambio, e gestire così la camera.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using DirectShowLib;
using System.Windows.Forms;
using CameraCapture.Objects;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
namespace CameraCapture
{
    public partial class frmMain : Form
    {
        DirectShowWrapper dsw = new DirectShowWrapper();
        HandledObject ho = new HandledObject();

      
        public frmMain()
        {            
            InitializeComponent();

            ho.height = this.Height;
            ho.width = this.Width;
            ho.handle = this.Handle;
            ho.clientSize = this.ClientSize;

            dsw.CaptureVideo(ho);
        }


        protected void WndProc(Message m)
        {
            if (m.Msg == dsw.WM_GRAPHNOTIFY)
            {
                dsw.HandleGraphEvent();
            }

            if (dsw.VideoWindow != null)
            {
                dsw.VideoWindow.NotifyOwnerMessage(
                                m.HWnd,
                                m.Msg,
                                (int)m.WParam,
                                (int)m.LParam);
            }
            else
            {
                base.WndProc(ref m);
            }
        }


        private void frmMain_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
                dsw.ChangePreviewState(false);

            if (this.WindowState == FormWindowState.Normal)
                dsw.ChangePreviewState(true);

            ho.height = this.Height;
            ho.width = this.Width;
            ho.handle = this.Handle;
            ho.clientSize = this.ClientSize;

            dsw.ResizeVideoWindow(ho);
        }

        public void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            dsw.closeinterfaces();
        }

    
    }
}



Orbene questo è quanto rimane del form ...ora la situazione è sicuramente molto più gestibile e pulita.

mercoledì 29 maggio 2013

How TO: C# internal WEB CAM

Ho cercato in lungo e in largo qualcosa di UTILIZZABILE e facilmente documentabile per creare un applicazione C# che sia in grado di utilizzare la WEBcam interna..

Ammetto che non c'e' molto ( c'è tanto ma nulla come lo cercavo io ) se non qualche esempio con Wia, o librerie create da qualcuno di cui non sempre si sa la provenienza.


Questo listato .. frutto di una traduzione da VB.Net, mi ha dato un buon risultato, e ammetto che paga. E' veloce, decisamente snello non semplicissimo da leggere.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using DirectShowLib;
using System.Windows.Forms;
using CameraCapture.Objects;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
namespace CameraCapture
{
    public partial class frmMain : Form
    {

        public int D = Convert.ToInt32("0X8000", 16);

        public int WM_GRAPHNOTIFY = 0;

        public IVideoWindow VideoWindow = null;
        public IMediaControl MediaControl = null;
        public IMediaEventEx MediaEventEx = null;
        public IGraphBuilder GraphBuilder = null;
        public ICaptureGraphBuilder2 CaptureGraphBuilder = null;

        public DsROTEntry rot = null;

        private PlayState CurrentState = PlayState.Stopped;

       

        public frmMain()
        {
            WM_GRAPHNOTIFY = D + 1;
            InitializeComponent();
            CaptureVideo(this);
        }



        protected void WndProc(Message m)
        {
            if (m.Msg == WM_GRAPHNOTIFY)
            {
                HandleGraphEvent();
            }

            if (VideoWindow != null)
            {
                VideoWindow.NotifyOwnerMessage(m.HWnd, m.Msg, 
                        (int)m.WParam, 
                        (int)m.LParam);
            }
            else
            {
                base.WndProc(ref m);
            }
        }

        public void HandleGraphEvent()
        {
            int hr = 0;
            EventCode evCode;
            int evParam1;
            int evParam2;

            if (MediaEventEx ==null) 
            {
                return;
            }

            while (MediaEventEx.GetEvent(out evCode, out evParam1, out evParam2, 0) == 0)
            {
                hr = MediaEventEx.FreeEventParams(evCode, evParam1, evParam2);            
            }
        }

        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            closeinterfaces();
        }


        public void SetupVideoWindow()
        {
            int hr = 0;

            hr = VideoWindow.put_Owner(this.Handle);


            hr = VideoWindow.put_WindowStyle(WindowStyle.Child | WindowStyle.ClipChildren);

            ResizeVideoWindow();

            hr = VideoWindow.put_Visible(OABool.True);
        }


        private void frmMain_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized )
                ChangePreviewState(false);
            
            if (this.WindowState == FormWindowState.Normal )
                ChangePreviewState(true);

            ResizeVideoWindow();
        }

        public void ResizeVideoWindow()
        {
            if (VideoWindow != null) 
            {
                VideoWindow.SetWindowPosition(0, 0, this.Width, this.ClientSize.Height);
            }
        }



        public void GetInterfaces(Form f)
        {
            int hr = 0;
            this.GraphBuilder = (IGraphBuilder)(new FilterGraph());
            this.CaptureGraphBuilder = (ICaptureGraphBuilder2)(new CaptureGraphBuilder2());
            this.MediaControl = (IMediaControl)(this.GraphBuilder);
            this.VideoWindow = (IVideoWindow)(this.GraphBuilder);
            this.MediaEventEx = (IMediaEventEx)(this.GraphBuilder);
            hr = this.MediaEventEx.SetNotifyWindow(f.Handle, WM_GRAPHNOTIFY, IntPtr.Zero);
        }


        public IBaseFilter FindCaptureDevice()
        {

            int hr = 0;
            IEnumMoniker IclassEnum;
            System.Runtime.InteropServices.UCOMIEnumMoniker classEnum;

            IMoniker[] moniker = new IMoniker[1];

            Object source = null;
            ICreateDevEnum devEnum = (ICreateDevEnum)(new CreateDevEnum());

            hr = devEnum.CreateClassEnumerator(FilterCategory.VideoInputDevice, 
                out classEnum, 0);


            IclassEnum = (IEnumMoniker)(classEnum);

            Marshal.ReleaseComObject(devEnum);

            if (classEnum == null)
            {
                throw new ApplicationException("No device video on board.");
            }
            if (IclassEnum.Next(moniker.Length, moniker, IntPtr.Zero) == 0)
            {
                Guid iid = typeof(IBaseFilter).GUID;
                moniker[0].BindToObject(null, null, iid, out source);
            }
            else
            {
                throw new ApplicationException("Unable to access video capture device!");
            }
            Marshal.ReleaseComObject(moniker[0]);
            Marshal.ReleaseComObject(classEnum);
            return (IBaseFilter)(source);
        }

        public void CaptureVideo(frmMain f)
        {
            int hr = 0;
            IBaseFilter sourceFilter = null;
            try
            {
                GetInterfaces(f);

                hr = this.CaptureGraphBuilder.SetFiltergraph(this.GraphBuilder);


                sourceFilter = FindCaptureDevice();

                hr = this.GraphBuilder.AddFilter(sourceFilter, "Video Capture");


                hr = this.CaptureGraphBuilder.RenderStream(PinCategory.Preview, 
                        MediaType.Video, 
                        sourceFilter, 
                        null, null);

                Marshal.ReleaseComObject(sourceFilter);

                f.SetupVideoWindow();

                rot = new DsROTEntry(this.GraphBuilder);

                hr = this.MediaControl.Run();

                this.CurrentState = PlayState.Running;

            }
            catch (Exception ex)
            {

            }
        }

        public void closeinterfaces()
        {

            if (this.MediaControl != null)
            {
                this.MediaControl.StopWhenReady();
            }

            this.CurrentState = PlayState.Stopped;


            if (this.MediaEventEx != null)
            {
                this.MediaEventEx.SetNotifyWindow(IntPtr.Zero, WM_GRAPHNOTIFY, IntPtr.Zero);
            }


            if (this.VideoWindow != null)
            {
                this.VideoWindow.put_Visible(OABool.False);
                this.VideoWindow.put_Owner(IntPtr.Zero);
            }


            if (rot != null)
            {
                rot.Dispose();
                rot = null;
            }


            Marshal.ReleaseComObject(this.MediaControl);
            Marshal.ReleaseComObject(this.MediaEventEx);
            Marshal.ReleaseComObject(this.VideoWindow);
            Marshal.ReleaseComObject(this.GraphBuilder);
            Marshal.ReleaseComObject(this.CaptureGraphBuilder);

            this.MediaControl = null;
            this.MediaEventEx = null;
            this.VideoWindow = null;
            this.CaptureGraphBuilder = null;
            this.GraphBuilder = null;
        }

        public void ChangePreviewState(bool showVideo)
        {
            int hr = 0;

            if (this.MediaControl == null)
            {
                return;
            }
            if (showVideo == true)
            {
                if (this.CurrentState != PlayState.Running)
                {
                    hr = this.MediaControl.Run();
                    this.CurrentState = PlayState.Running;
                }
            }
            else
            {

                hr = this.MediaControl.StopWhenReady();
                this.CurrentState = PlayState.Stopped;
            }
        }
    }
}

E questo è l'enumerativo.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CameraCapture.Objects
{
    public enum PlayState
    {
        Stopped,
        Paused,
        Running,
        Init
    }
}





L'unica notazione è l'aggiunta della libreria di DirectShow.


Il resto è solo divertimento .. o sperimentazione:


... E buon prò vi faccia come al solito !!


Grand Sla



Fra i prodotti presenti su WEB per la gestione di SLA e KPI, mi sono imbattuto in questo prodotto, l’ho provato e ammetto di essere rimasto piacevolmente soddisfatto.

Per me il punto di forza è nell’essere interamente sviluppato per il WEB, in questo modo la raggiungibilità è massima ( ormai sono pochi i punti in cui sia assente una rete WI-FI o la connettività dati sia assente ).

Il prodotto si presta inoltre a svariati tipi di utilizzo, e credo che anche questo sia un buon punto di forza, ossia nel mio caso l’ho utilizzato per valutare i carichi del mio gruppo di lavoro. E onestamente mi è bastato creare un CSV con i miei dati, impostare le mie regole d’analisi, e osservare i grafici.
Per altro se si è client oriented, o se si è sul pezzo in qualità di account è possibile dare una definizione dell’organigramma.

Organizzazione>Dipartimenti>Contratti.

Tramite questo tipo di gestione è facile organizzare una distribuzione dei dati a chi di competenza, o in base alla durata di un contratto.
Da oggi lascerò il link sul mio blog, per me è per chi vorrà approfondire.



Qui trovate il sito: GrandSLA