Akozdem!R Posted February 26, 2015 Share Posted February 26, 2015 Bir butonla bir işlem yaptırmak için döngüye sokuyoruz, arayüzdeki farklı bir butonu kullanarak döngüden nasıl çıkartabiliriz? Örnek, kendi yaptığım. Ancak döngüye müdehale olmuyor. Fikri olan varsa bekliyorum. Boolean iptal=false; private void button2_Click(object sender, EventArgs e) { iptal = true; } private void button1_Click(object sender, EventArgs e) { for (int i = 1, j = 0; i < 10; i++, j++) { if (iptal) break; ... } } Quote Link to comment Share on other sites More sharing options...
KintaRo Posted February 26, 2015 Share Posted February 26, 2015 normal döngü formu kitldiği için direk müdahale edilmez sanırım ama döngüyü backGroundWorker kontrolü ile yaparsanız, BGWorker Async olarak yani formdan bağımsız olarak çalışır. Arka planda çalışır diyebiliriz. hem formu kitlemez hem de müdahale edebilirsiniz. Mantık yine aynı public bir değişkene bağlı kontrol. using System; using System.ComponentModel; using System.Windows.Forms; namespace DonguDurdurma { public partial class Form1 : Form { bool kontrol = false; public Form1() { CheckForIllegalCrossThreadCalls = false; InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { bgWorker.RunWorkerAsync(); } private void button2_Click(object sender, EventArgs e) { kontrol = true; } private void bgWorker_DoWork(object sender, DoWorkEventArgs e) { kontrol = false; Random rnd = new Random(); for (int i = 0; i < 500000; i++) { int random = rnd.Next(12345, 987564); listBox1.Items.Add(String.Format("{0} - {1}", i, random)); label1.Text = "Son Veri: " + random; if (kontrol) { break; } } } } } Örnek Proje: https://yadi.sk/d/PCeDsibLeuKxv Quote Link to comment Share on other sites More sharing options...
Editor pairs Posted February 26, 2015 Editor Share Posted February 26, 2015 Thread de kullanabilirsiniz. Quote Link to comment Share on other sites More sharing options...
KintaRo Posted February 26, 2015 Share Posted February 26, 2015 Sayın Editör bir örnek verinde öğrenelim o zaman. Quote Link to comment Share on other sites More sharing options...
Editor pairs Posted February 26, 2015 Editor Share Posted February 26, 2015 Formu bu şekilde tasarladıktan sonra. Kod kısmı. Quote Link to comment Share on other sites More sharing options...
lamont Posted February 26, 2015 Share Posted February 26, 2015 Thread ile döngünü içinemi müdahale ediyoruz pairs ? Quote Link to comment Share on other sites More sharing options...
Editor pairs Posted February 26, 2015 Editor Share Posted February 26, 2015 (edited) Hayır thread bir programın kendini eş zamanlı (paralel olarak) birden fazla iş parçasına ayırabilmesine denir. Bilgisayarımızda Tek işlemci var ise birden fazla Thread kullanımında uygulamalar zaman dilimleme ile gerçekleştirilir. Yani tek işlemci 2 veya daha fazla thread arasında çok hızlı geçiş yapar.Bu durum sanki paralel olarak işlemlerin gerçekleştiği izlenimini verir.Bilgisayarımızda birden fazla işlemci var ise threadler farklı işlemciler üzerinde eş zamanlı olarak çalışabilir. Edited February 26, 2015 by pairs Quote Link to comment Share on other sites More sharing options...
lamont Posted February 26, 2015 Share Posted February 26, 2015 yani işlemleri parçalara bölerek çalışma Quote Link to comment Share on other sites More sharing options...
Editor pairs Posted February 26, 2015 Editor Share Posted February 26, 2015 yani işlemleri parçalara bölerek çalışma İŞlemcinin çekirdeklerine bölerek çalışma diyebiliriz. Paralel Programlama olarak araştırarak daha detaylı ve geniş dökümanlara ulaşabilirsin. Quote Link to comment Share on other sites More sharing options...
MostWanted Posted February 26, 2015 Share Posted February 26, 2015 Aslında bu gibi durumlarda backgroundworker daha iyi bir çözüm şöyleki backgroundworker kendisi zaten bi thread oluşturup onda çalışıyor ve oluşturmak, kontrol etmek normal thread'e göre daha kolay.Aslına bakarsanız backgroundworker tam olarak yukarda anlatılan durum için ortaya çıkmış bir konsept (MainUI thread'ini responsive(dondurmamak) yapmak). Android programlama ile uğraşanlar bunu iyi bilirler ve çokça kullanırlar. Quote Link to comment Share on other sites More sharing options...
burak_489 Posted February 26, 2015 Share Posted February 26, 2015 Break yerine Exit denedin mi ? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.