🙂 İNSANLARIN EN HAYIRLISI INSANLARA FAYDALI OLANDIR 🙂

Ramazan HABER / C# / monitor ve mutex

1-) C# -

monitor ve mutex

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace losda

{

    public partial class Form1 : Form

    {

        public object locker;

        public Form1()

        {

            InitializeComponent();

            locker = new object();

        }

        Thread t1;

        private void prog1()

        {

            lock (locker)

            {

                while (progressBar1.Value < 100)

                {

                    progressBar1.Value += 1;

                    textBox1.Text += "X";

                    Thread.Sleep(30);

 

 

                }

            }

            

        }

        private void prog2()

        {

            lock (locker)

            {

                while (progressBar2.Value < 100)

            {

                progressBar2.Value += 1;

                textBox1.Text += "y";

                Thread.Sleep(30);

 

 

                }

            }

        }

        private void prog3()

        {

 

            Monitor.Enter(locker);//lock ile aynı gorevi gorur

            try

            {

                while (progressBar3.Value < 100)

                {

                    progressBar3.Value += 1;

                    textBox1.Text += "z";

                    Thread.Sleep(30);

 

 

                }

            }

           

            finally

            {

               Monitor.Exit(locker);//yapmak zorunlu

            }

               

            

        }

        Mutex m;

        private void prog4()

        {

 

            m = new Mutex();

            m.WaitOne();//sırayı bekle kardeşim

            try

            {

                while (progressBar4.Value < 100)

                {

                    progressBar4.Value += 1;

                    textBox1.Text += "t";

                    Thread.Sleep(30);

 

 

                }

            }

 

            finally

            {

              m.ReleaseMutex();

            }

 

 

        }

        private void btn_hepsi_Click(object sender, EventArgs e)

        {

            System.Windows.Forms.Form.CheckForIllegalCrossThreadCalls = false;//threadlarda çakışma olursa ben hallederim

 

            t1 = new Thread(prog1);//metod da "()" yoktur

            Thread t2 = new Thread(prog2);//metod da "()" yoktur

            Thread t3 = new Thread(prog3);//metod da "()"

            Thread t4= new Thread(prog4);//metod da "()" yoktur

            t1.Start();

            t2.Start();

            t3.Start();

            t4.Start();

        }

    }

}

 2021 Ocak 18 Pazartesi
 420