jueves, 14 de mayo de 2020

Using delegates. Basic Example


MULTICAST


ENCAPSULATION






class Program
    {
        static void Main(string[] args)
        {
            BookDb books = new BookDb();
            AddBooks(books);            
            Console.WriteLine("Soft cover book titles:");
            books.SearchSoftCoverBooks(PrintTitle);

            Total sum = new Total();
            books.SearchSoftCoverBooks(sum.CalculateTotalPrice);
            Console.WriteLine("Cumulative total price: {0}", sum.total.ToString());

            Console.ReadKey();
        }

        static void PrintTitle(Book book)
        {
            Console.WriteLine(" > {0}", book.title);
        }
        static void AddBooks(BookDb db)
        {
            db.AddBook("C programming", "Brian W.", 19.95m, true);
            db.AddBook("Unix 2.0", "William G.", 25.50m, true);
            db.AddBook("MS-DOS", "Smith E.", 12.75m, false);
            db.AddBook("Cleam code", "Nicholson A.", 22.50m, true);
        }
    }

    public class BookDb
    {
        ArrayList db = new ArrayList();

        public void AddBook(string title, string owner, decimal price, bool softCover)
        {
            db.Add(new Book(title, owner, price, softCover));
        }

        public void SearchSoftCoverBooks(SearchBooks searchBooks)
        {
            foreach(Book b in db)
            {
                if(b.softCover)
                {
                    searchBooks(b);
                }
            }
        }
    }

    public class Total
    {
        public decimal total;
        public void CalculateTotalPrice(Book book)
        {
            total += book.price;
        }
    }

    public delegate void SearchBooks(Book book);

    public class Book
    {
        public string title;
        public string owner;
        public decimal price;
        public bool softCover;

        public Book(string title, string owner, decimal price, bool softCover)
        {
            this.title = title;
            this.owner = owner;
            this.price = price;
            this.softCover = softCover;
        }
    }

Using Interfaces, Basic Examples

EJEMPLO 1


EJEMPLO 2





Installing your own Windows Service



PROGRAM.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Topshelf;  //ADD THIS PACKAGE

namespace MyService
{
    class Program
    {
        static void Main(string[] args)
        {
            var host = HostFactory.Run(front =>
            {
                front.RunAsLocalSystem();
                front.SetServiceName("FileUpload");
                front.SetDisplayName("File Update Service");
                front.SetDescription("Servicio local de File Update Service");

                front.Service<FileUpdate>(back => 
                {
                    back.ConstructUsing(service => new FileUpdate());
                    back.WhenStarted(service => service.Start());
                    back.WhenStopped(service => service.Stop());
                });
            });

            int outvalue = (int)Convert.ChangeType(host, host.GetTypeCode());

            Environment.ExitCode = outvalue;
        }
    }
}

/*INSTALL SERVICE
*
*   COPY CODE TO SOME FOLDER. EXAMPLE: C:\TMP\CODE\
*   RUN WINDOWS SHELL CONSOLE AS ADMINISTRATOR
*   C:> CD C:\TMP\CODE\
*   C:\TMP\CODE\> MYSERVICE.EXE INSTALL START
* UNINSTALL SERVICE
*   RUN WINDOWS SHELL CONSOLE AS ADMINISTRATOR
*   C:> CD C:\TMP\CODE\
*   C:\TMP\CODE\> MYSERVICE.EXE UNINSTALL 
*  
*/

====================================================
CLASS FILEUPLOAD

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace MyService
{
    class FileUpdate
    {
        private readonly Timer time;

        public FileUpdate()
        {
            time = new Timer(1000)
            {
                AutoReset = true
            };
            time.Elapsed += Time_Elapsed;
        }

        private void Time_Elapsed(object sender, ElapsedEventArgs e)
        {
            string line = string.Format("{0}\n", DateTime.Now.ToString());
            File.AppendAllText(@"C:\tmp\timer.log", line);
        }

        public void Start()
        {
            time.Start();
        }

        public void Stop()
        {
            time.Stop();
        }
    }
}