Kişisel Web Sitesi                         
Gezinti Bağlantılarını Atla
Site içi arama...
Bul...
.NET 2005 ile Cross Threading 16.03.2009

.NET 2005 ile cross threading yapmak için öncelikle

public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}

kırmızı ile boyanmış satırı eklemek gerekiyor. bu satır, denetimi framework mimarisinden çıkarıp programcıya vermeyi sağlıyor.

örnek olması açısından form nesnesine 2 tane label nesnesi koyarak aşağıdaki kodu form1.cs içeriğine yapıştırıp test edebilirsiniz.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication11
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}

System.Threading.Thread th1;
System.Threading.Thread th2;

void saat_sn(Label l)
{
int a = 0;
while (a < 10)
{
l.Text ="Saniye: " + DateTime.Now.Second.ToString();
}
}

void saat_mlsn(Label l)
{
int a = 0;
while (a < 10)
{
l.Text = "Milisaniye: " + DateTime.Now.Millisecond.ToString();
}
}

private void Form1_Load(object sender, EventArgs e)
{
th1 = new System.Threading.Thread(delegate() { saat_sn(label1); });
th2 = new System.Threading.Thread(delegate() { saat_mlsn(label2); });

th1.Start();
th2.Start();
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
th1.Abort();
th2.Abort();
}
}
}

Label nesnelerinde saniye ve milisaniye değerlerinin sürekli okunarak nesnelere yazıldığını görebilirsiniz.


Yorumlar (0) Bülent KOÇER
Sizde Yorumunuzu Yapın
Gönderen