Logo Background

Csharp ile Çok kanallı multi threading uygulamaları

  • Written by ElektronikElektronik 1 Yorum Var1 Yorum Comments
    Last Updated: Aralık 20, 2009

    Örnekleri

    Network uygulamaları ve birçok sorgu gerektiren uygulamlarda projelerimiz şişerek bize cevap vermeyecek ve yaptığımız sorgular bitene kadar bize cevap vermeyecektir. Örnek verecek olursak bilgisayarımızda birçok işlemi aynı anda yapabiliyoruz. Winamp programı açıkken, word, excel gibi programları kullanabiliyoruz aynı anda internette gezinip, dosya kopyalayabiliyoruz.. Bunları yapabilmemizin nedeni olayıdır. Çok kanallı uygulamalar geliştirdiğimizde, uygulamalarımız can sıkıcı bir halden çıkıp her zaman bizim kontrolümüzde olacaktır..

    formunda bir işlem yaptığınızı düşünelim ve gelen cevaba göre form üzerinde herhangi bir kontrolü değiştirmeye çalıştığınızda operation not valid: hatasına sebep olacaktır. İşte bu hataların önüne geçmek için yine thread olayını kullanmak zorundasınız..

    Çok kanallı çalışma ortamı oluşturabilmek için projenizde using System.Therading kütüphanesini kullanmanız gerekmektedir. Aksi takdirde başka bir oluşturamazsınız.

    Çok kanallı uygulama için hemen bir örnek yazarak farkı anlayalım..

    ?View Code CSHARP
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
     
    namespace multithreadornek
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
     
     
            private void islem()
            {
                int sayi = 0;
                while (true) // Sonsuz bir döngü oluşturuyoruz..
                {
                    sayi++; // sayiyi 1 artıyoruz..
                }
     
            }
     
            // 1. butonumuzun click olayı .. sonsuz döngüye girecek...
            private void button1_Click(object sender, EventArgs e)
            {
                islem(); 
            }
     
            private void button2_Click(object sender, EventArgs e)
            {
                MessageBox.Show("www.xdelete.com"); 
            }
     
        }
    }

    Yukarıdaki örneğimizde Buton 1 ‘e tıkladığımızda sonsuz döngüye girecek ve buton2 ye tıklasak bile uygulamamız buradaki kod bloğunu işletemeyecektir.
    Görüldüğü üzere bu bizim için bir problemdir. Ve bu problemin çözümü olarak hemen yeni bir kanal açacak ve onun üzerinden kodumuzu işleteceğiz..
    using ; kütüphanesini eklemeyi unutmayın ;)

    ?View Code CSHARP
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
     
    namespace multithreadornek
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
     
            Thread Kanal;
            private void islem()
            {
                int sayi = 0;
                while (true) // Sonsuz bir döngü oluşturuyoruz..
                {
                    sayi++; // sayiyi 1 artıyoruz..
                }
     
            }
     
            // 1. butonumuzun click olayı .. sonsuz döngüye girecek...
            private void button1_Click(object sender, EventArgs e)
            {
                // yeni bir kanal oluşturarak..
                ThreadStart basla = new ThreadStart(islem);
                Kanal = new Thread(basla);
                Kanal.Start();
            }
     
            private void button2_Click(object sender, EventArgs e)
            {
                MessageBox.Show("www.xdelete.com"); 
            }
     
        }
    }

  • Warning: Unexpected character in input: ''' (ASCII=39) state=1 in /home/xdelete/public_html/forum/cache/data_c1176e3b86b838cc919e441a620ca4b6-SMF-modSettings.php on line 1

    Parse error: syntax error, unexpected ':' in /home/xdelete/public_html/forum/cache/data_c1176e3b86b838cc919e441a620ca4b6-SMF-modSettings.php on line 1