stackoverflow1453

Programming&&Games&&Life

Archive for June, 2016

Multiple Threading

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

namespace ConsoleApplication58
{
    class MultiThreading
    {
        static void Main(string[] args)
        {
            MultiThreading mul = new MultiThreading();

            Thread t1 = new Thread(new ThreadStart(mul.WriteX));
            Thread t2 = new Thread(new ThreadStart(mul.WriteO));

            t1.Start();
            t2.Start();

        }

        private void WriteX()
        {
            for (int i = 0; i < 300; i++)
            {
                Console.Write("x");
            }
        }

        private void WriteO()
        {
            for (int i = 0; i < 300; i++)
            {
                Console.Write("o");
            }
        }
    }
}

//Note: In this code, t1 and t2 objects manage 2 separate threads which work independently from each other.
//Means, till both the jobs finish, a little WriteX method and a little WriteO method works.

                                    //OUTPUT
/*
 * xxxxxoooooooooooooooooooooooooooooooxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 * xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxoooooooooooooooooo
 * ooooooooooooooooooooooooooooooooooooooooxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 * xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxoooooooooooooooooo
 * oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
 * oooooooxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 * xxxxxxxxxxxxxxoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
 * ooooooooooooooooooooooooooooooooooooooooDevam etmek için bir tusa basin . . .
 * */

Threading

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
/*
 * t1 object does the Work1 job in SingleThreading class.
 * That is when StartSingleOperation() method is called, without waiting for the other jobs to finish, work1 starts.
*/

namespace ConsoleApplication57
{
    class SingleThreading
    {
        static void Main(string[] args)
        {
            SingleThreading s = new SingleThreading();
            s.StartSingleOperation();
        }

        public void StartSingleOperation()
        {
            Thread t1 = new Thread(new ThreadStart(Work1));
            t1.Start();
        }

        private void Work1()
        {
            Console.WriteLine("This is worker thread. ThreadID: {0}", Thread.CurrentThread.ManagedThreadId);
        }
    }
}

How to import a setting to Visual Studio?

Download the setting file. (.vssettings extension)

For example: http://studiostyl.es/schemes/vim-darkblue

Then go Tools/Import Export Settings/Import Selected Environment Settings

and choose the file you downloaded above. Thats all…

Unsynchronized producer/consumer relationship

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

namespace Asenkron_Producer_Consumer
{
    class Tutucu
    {
        public int sayi = -1;
    }

    class Uretec
    {

        Random tr1 = new Random();
        Tutucu tu1 = new Tutucu();

        //constructor
        public Uretec(Tutucu x, Random r)
        {
            tr1 = r;
            tu1 = x;
        }

        //method
        public void Uret()
        {
            for (int i = 1; i <= 4; i++)
            {
                Thread.Sleep(tr1.Next(1, 6000));
                tu1.sayi = i;
                Console.WriteLine("Producer writes {0}.", i);
            }

            Console.WriteLine("Uretici üretimi bitirdi.");
        }
    }

    class Tuketec
    {

        Random tr2 = new Random();
        Tutucu tu2 = new Tutucu();

        //constructor
        public Tuketec(Tutucu x, Random r)
        {
            tr2 = r;
            tu2 = x;
        }

        //method
        public void Tuket()
        {
            for (int i = 1; i <= 4; i++)
            {
                Thread.Sleep(tr2.Next(1, 6000));
                Console.WriteLine("Consumer reads {0}.", tu2.sayi);
            }

            Console.WriteLine("Tüketici tüketimi bitirdi.");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Random random = new Random();

            Tutucu t = new Tutucu();

            Uretec u = new Uretec(t, random);
            Tuketec tuk = new Tuketec(t, random);

            //create thread
            Thread ureticiThread = new Thread(new ThreadStart(u.Uret));
            Thread tuketiciThread = new Thread(new ThreadStart(tuk.Tuket));

            //start thread
            ureticiThread.Start();
            tuketiciThread.Start();
        }
    }
}

How to give more than 1 parameter to Thread.Start()?

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

namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            Thread t = new Thread(new ParameterizedThreadStart(p.say));

            int[] seri = new int[2] { 100, 200 };
            t.Start(seri);

        }

        public void say(object x)
        {
            int[] b = (int[])x;
            for (int i = b[0]; i < b[1]; i++)
            {
                Console.WriteLine("{0}", i);
            }
        }
    }
}

How to make a thread with a parameter without making another class?

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

namespace Sayıcı
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            ParameterizedThreadStart a = new ParameterizedThreadStart(p.say);
            Thread thread1 = new Thread(a);
            //or you can use directly:
            //Thread thread1 = new Thread(new ParameterizedThreadStart(test1.say));           

            thread1.Start(15);
        }

        //1st method
        public void say(object k)
        {
            //you should cast object to int to use
            for (int i = 1; i <= (int)k; i++)
            {
                Console.WriteLine("{0}", i);
            }
        }
    }
}

How to give parameter to a thread by using ParameterizedThreadStart()?

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

namespace Sayıcı
{
    class Program
    {
        static void Main(string[] args)
        {
            test test1 = new test();
            ParameterizedThreadStart a = new ParameterizedThreadStart(test1.say);
            Thread thread1 = new Thread(a);
            //or you can use directly:
            //Thread thread1 = new Thread(new ParameterizedThreadStart(test1.say));           

            thread1.Start(15);
        }
    }

    class test
    {
        public int kadar = 0;

        //1st method
        public void say(object k)
        {
            kadar = (int)k;         //you should cast object to int to use
            for (int i = 1; i <= kadar; i++)
            {
                Console.WriteLine("{0}", i);
            }
        }
    }
}