{"id":185,"date":"2009-08-05T14:24:00","date_gmt":"1970-01-01T00:00:00","guid":{"rendered":"http:\/\/www.yazilimmutfagi.com\/?p=185"},"modified":"2009-08-05T14:24:00","modified_gmt":"1970-01-01T00:00:00","slug":"csharp-ile-thread-kullanimi","status":"publish","type":"post","link":"https:\/\/www.yazilimmutfagi.com\/index.php\/2009\/08\/05\/csharp-ile-thread-kullanimi\/","title":{"rendered":"C# ile Thread Kullanimi"},"content":{"rendered":"<p>\n\t&nbsp;&nbsp;&nbsp; Thread bilgisayar biliminde, bir programin kendini es zamanli birden fazla is par&ccedil;asina ayirabilmesinin bir yoludur. Thread ve islem arasindaki fark bir isletim sisteminden digerine degismekle birlikte genel olarak thread olusturulusu ve kaynaklari paylasmasi a&ccedil;isindan process&#39;den ayrilir. &Ccedil;oklu thread&#39;ler paralel olarak pek &ccedil;ok bilgisayar sisteminde uygulanabilir. Tek islemci kullanildigi durumlarda &ccedil;ok thread&#39; li uygulama zaman dilimleme ile ger&ccedil;eklestirilir; tek islemci fakli thread&#39; lar arasinda &ccedil;ok hizli ge&ccedil;is yapar ve bu durumda islemler ger&ccedil;ekte olmasa bile es zamanli kosuluyormus izlenimi verir. &Ccedil;ok islemcili sistemlerde farkli thread&#39; ler farkli islemciler &uuml;zerinde es zamanli olarak &ccedil;alisabilir.<br \/>\n\t<a href='www.yazilimmutfagi.com'>.<\/a><br \/>\n\t<br \/>\n\tPek &ccedil;ok modern isletim sistemi bir is d&uuml;zenleyicisi yardimiyla hem zaman dilimleme hem de &ccedil;ok islemcili thread&#39; lemeyi desteklemektedir. Isletim sistem &ccedil;ekirdegi (kernel) sistem &ccedil;agrilari vasitasi ile programciya thread&#39; leri kontrol etme imk&acirc;ni saglamaktadir. Bunun yoklugunda programlar, zamanlayicilar, sinyaller veya diger y&ouml;ntemleri kullanarak kendi &ccedil;alismalarini sonlandirabilirler. Bunlara user-space thread&#39; ler denir.<br \/>\n\t<br \/>\n\t&nbsp;&nbsp;&nbsp;&nbsp; Bizde yazdigimiz programlarda ayni anda birden fazla islem yaptirmak istiyorsak Threadleri kullanabiliriz. Farkli threadler ayni fonksiyonu kullanabilecegi gibi her thread farkli bir is yapan ayri fonksiyonlarida kullanabilir. Threadleri kullanarak basit bir uygulama yazalim.<br \/>\n\t<a href='www.yazilimmutfagi.com'>.<\/a><\/p>\n<pre class=\"brush:csharp\">\nThread thread1 = new Thread(new ThreadStart(ProgressBar1Doldur));\nThread thread2 = new Thread(new ThreadStart(ProgressBar2Doldur));\nThread thread3 = new Thread(new ThreadStart(ProgressBar3Doldur));\n\nthread1.Start();\nthread2.Start();\nthread3.Start();<\/pre>\n<p>\n\tYukaridaki kodda g&ouml;rd&uuml;g&uuml;n&uuml;z gibi ilk olarak yeni bir thread olusturuyor ve bu threadin kullanacagi fonksiyonu da ThreadStart nesnesi i&ccedil;inde yaziyoruz. <em>thread1.Start() <\/em>dedigimiz zaman <span style=\"color:#0000ff;\">ThreadStart <\/span>i&ccedil;ine yazmis olugumuz kodlar ilgili thread tarafindan islenmeye baslar.<\/p>\n<pre class=\"brush:csharp\">\npublic void ProgressBar1Doldur()\n{\n    while (progressBar1.Value &lt; 100)\n    {\n        progressBar1.Value += 1;\n        Thread.Sleep(10);\n    }\n}\n\npublic void ProgressBar2Doldur()\n{\n    while (progressBar2.Value &lt; 100)\n    {\n        progressBar2.Value += 1;\n        Thread.Sleep(60);\n    }\n}\n\npublic void ProgressBar3Doldur()\n{\n    while (progressBar3.Value &lt; 100)\n    {\n        progressBar3.Value += 1;\n        Thread.Sleep(50);\n    }\n}<\/pre>\n<p>\n\t<a href='www.yazilimmutfagi.com'>.<\/a><br \/>\n\tHer thread olusturulurken belirlenmis olan fonksiyon i&ccedil;indeki kodlari &ccedil;alistirir ve fonksiyonda ki islemler bittigi zaman otomatik olarak sonlanir.<br \/>\n\t<br \/>\n\tAsagidaki formu dizaynini yapip yukaridaki gibi bir kod yazar ve derlerseniz <em>&ldquo;&Ccedil;apraz is par&ccedil;acigi islemi ge&ccedil;erli degil: &#39;progressBar1&#39; <\/em>denetimine olusturuldugu is par&ccedil;acigi disinda baska bir is par&ccedil;acigindan erisildi.&rdquo; Diye bir hata alirsiniz. Bunun sebebi threadlerin senkronlanmadigindandir. Bir threadin kullandigi bir nesneye baska bir thread erismeye &ccedil;alistigi anda yukaridaki hatayi alirsiniz. Bu hatayi almamak i&ccedil;in threadleri senkron olarak &ccedil;alistirmaniz gerekiyor. Threadleri senkron olarak &ccedil;alistirma konusuna daha sonra girecegiz. Simdili yukaridaki hatayi engellemek i&ccedil;in threadleri &ccedil;alistirmadan ince asagidaki kodu ekleyin.<\/p>\n<pre class=\"brush:csharp\">\nSystem.Windows.Forms.Form.CheckForIllegalCrossThreadCalls = false;\n \nThread thread1 = new Thread(new ThreadStart(ProgressBar1Doldur));\nThread thread2 = new Thread(new ThreadStart(ProgressBar2Doldur));\nThread thread3 = new Thread(new ThreadStart(ProgressBar3Doldur));\n\nthread1.Start();\nthread2.Start();\nthread3.Start();<\/pre>\n<p>\n\t<img decoding=\"async\" alt=\"\" src=\"http:\/\/www.yazilimmutfagi.com\/yuklenendosyalar\/image\/threadresim1.jpg\" style=\"width: 548px; height: 263px;\" \/><\/p>\n<p>\n\t&Ouml;rnek uygulamamizi <a href=\"http:\/\/www.yazilimmutfagi.com\/yuklenendosyalar\/file\/ThreadKullanimiSolution.rar\">buradan <\/a>indirebilirsiniz.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Thread bilgisayar biliminde, bir programin kendini es zamanli birden fazla is par&#231;asina ayirabilmesinin bir yoludur. Thread ve islem arasindaki fark bir isletim sisteminden digerine degismekle birlikte genel olarak thread olusturulusu ve kaynaklari paylasmasi a&#231;isindan process&#39;den ayrilir. &#199;oklu thread&#39;ler paralel olarak pek &#231;ok bilgisayar sisteminde uygulanabilir.<\/p>\n","protected":false},"author":517,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[417],"tags":[3879,4872,5515,5873,4648,4133,5953,5375],"_links":{"self":[{"href":"https:\/\/www.yazilimmutfagi.com\/index.php\/wp-json\/wp\/v2\/posts\/185"}],"collection":[{"href":"https:\/\/www.yazilimmutfagi.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yazilimmutfagi.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yazilimmutfagi.com\/index.php\/wp-json\/wp\/v2\/users\/517"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yazilimmutfagi.com\/index.php\/wp-json\/wp\/v2\/comments?post=185"}],"version-history":[{"count":0,"href":"https:\/\/www.yazilimmutfagi.com\/index.php\/wp-json\/wp\/v2\/posts\/185\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yazilimmutfagi.com\/index.php\/wp-json\/wp\/v2\/media?parent=185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yazilimmutfagi.com\/index.php\/wp-json\/wp\/v2\/categories?post=185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yazilimmutfagi.com\/index.php\/wp-json\/wp\/v2\/tags?post=185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}