오늘의 C#공부 : 직렬화, 스레드/ 테스크

////직렬화

//using System;
//using System.IO;
//using System.Runtime.Serialization.Formatters.Binary;

//namespace Serialization
//{
//    [Serializable]
//    class NameCard
//    {
//        public string Name;
//        public string Phone;
//        public int Age;
//    }

//    class MainApp
//    {
//        static void Main(string[] args)
//        {
//            Stream ws = new FileStream(“a.dat”, FileMode.Create);
//            BinaryFormatter serializer = new BinaryFormatter();

//            NameCard nc = new NameCard();
//            nc.Name = “박상현”;
//            nc.Phone = “010-123-4567”;
//            nc.Age = 33;

//            serializer.Serialize(ws, nc);
//            ws.Close();

//            Stream rs = new FileStream(“a.dat”, FileMode.Open);
//            BinaryFormatter deserializer = new BinaryFormatter();

//            NameCard nc2;
//            nc2 = (NameCard)deserializer.Deserialize(rs);
//            rs.Close();

//            Console.WriteLine(“Name:  {0}”, nc2.Name);
//            Console.WriteLine(“Phone: {0}”, nc2.Phone);
//            Console.WriteLine(“Age:   {0}”, nc2.Age);

//        }
//    }
//}

////List를 직렬화하고역직렬화하기

//using System;
//using System.Collections.Generic;
//using System.IO;
//using System.Runtime.Serialization.Formatters.Binary;

//namespace SerializingCollection
//{
//    [Serializable]

//    class NameCard
//    {
//        public string Name;
//        public string Phone;
//        public int Age;

//        public NameCard(string Name, string Phone, int Age)
//        {
//            this.Name = Name;
//            this.Phone = Phone;
//            this.Age = Age;
//        }
//    }

//    class MainApp
//    {
//        static void Main(string[] args)
//        {
//            Stream ws = new FileStream(“a.dat”, FileMode.Create);
//            BinaryFormatter serializer = new BinaryFormatter();

//            List list = new List();
//            list.Add(new NameCard(“박상현”, “010-123-4567”, 33));
//            list.Add(new NameCard(“김연아”, “010-323-1111”, 22));
//            list.Add(new NameCard(“장미란”, “010-555-5555”, 26));

//            serializer.Serialize(ws, list);
//            ws.Close();

//            Stream rs = new FileStream(“a.dat”, FileMode.Open);
//            BinaryFormatter deserializer = new BinaryFormatter();

//            List list2;
//            list2 = (List)deserializer.Deserialize(rs);
//            rs.Close();

//            foreach (NameCard nc in list2)
//            {
//                Console.WriteLine(
//                    “Name : {0}, Phone : {1} , Age {2}”
//                    , nc.Name, nc.Phone, nc.Age);
//            }
//        }
//    }

//}

////스레드와 테스크

//using System;
//using System.Threading;

//namespace BasicThread
//{
//    class MainApp
//    {
//        static void DoSomething()
//        {
//            for (int i = 0; i < 5; i++)
//            {
//                Console.WriteLine(“DoSomeThing : {0}” , i);
//                    Thread.Sleep(10);
//            }
//        }

//        static void Main(string[] args)
//        {
//            Thread t1 = new Thread(new ThreadStart(DoSomething));

//            Console.WriteLine(“Starting Thread…”);
//            t1.Start();

//            for (int i = 0; i < 5; i++)
//            {
//                Console.WriteLine (“Main : {0}”, i);
//                Thread.Sleep(10);
//            }

//            Console.WriteLine(“Wating until thread stops…”);
//            t1.Join();

//            Console.WriteLine(“Fineshed”);
//        }
//    }
//}

////스레드 임의로 종료시키기

//using System;
//using System.Threading;

//namespace AbortingThread
//{
//    class SideTesk
//    {
//        int count;

//        public SideTesk(int count)
//        {
//            this.count = count;
//        }

//        public void KeepAlive()
//        {
//            try
//            {
//                while (count > 0)
//                {
//                    Console.WriteLine("{0} left", count–);
//                    Thread.Sleep(10);
//                }
//                Console.WriteLine(“Count : 0”);
//            }

//            catch (ThreadAbortException e)
//            {
//                Console.WriteLine(e);
//                Thread.ResetAbort();
//            }

//            finally
//            {
//                Console.WriteLine(“Clearing resource…”);
//            }
//        }

//        class MainApp
//        {
//            static void Main(string[] args)
//            {
//                SideTesk task = new SideTesk(100);
//                Thread t1 = new Thread(new ThreadStart(task.KeepAlive));
//                t1.IsBackground = false;

//                Console.WriteLine(“Starting thread…”);
//                t1.Start();

//                Thread.Sleep(100);//느리게 하고..

//                Console.WriteLine(“Aborting thread….”);
//                t1.Abort();

//                Console.WriteLine(“Wating until thread stops….”);
//                t1.Join();

//                Console.WriteLine(“Finished”);
//            }
//        }
   
//    }
//}

////Thread State

//using System;
//using System.Threading;

//namespace UsingThreadState
//{
//    class MainApp
//    {
//        private static void PrintThreadState(ThreadState state)
//        {
//            Console.WriteLine("{0,-16} : {1}", state, (int)state);
//        }

//        static void Main(string[] args)
//        {
//            PrintThreadState(ThreadState.Running);

//            PrintThreadState(ThreadState.StopRequested);

//            PrintThreadState(ThreadState.SuspendRequested);

//            PrintThreadState(ThreadState.Background);

//            PrintThreadState(ThreadState.Unstarted);

//            PrintThreadState(ThreadState.Stopped);

//            PrintThreadState(ThreadState.WaitSleepJoin);

//            PrintThreadState(ThreadState.Suspended);

//            PrintThreadState(ThreadState.AbortRequested);

//            PrintThreadState(ThreadState.Aborted);

//            PrintThreadState(ThreadState.Aborted | ThreadState.Stopped);
//        }
//    }   
//}

Hugo로 만듦
JimmyStack 테마 사용 중