CannibalSmith ([info]cannibalsmith) rakstīja [info]koderi kopienā,
@ 2009-03-13 15:41:00

Previous Entry  Add to memories!  Tell a Friend!  Next Entry
.NET eventu sūtīšana uz UI thread
Kā ir pareizāk? Kā ir labāk? Kā ir optimālāk?
public class Class
{
	delegate void InvokeEvent(EventArgs e);
	public event EventHandler Event;

	public Class()
	{
		ThreadPool.QueueUserWorkItem(new WaitCallback(Work));
	}

	void Work()
	{
		Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new InvokeEvent(OnEvent), new EventArgs());
	}

	protected void OnEvent(EventArgs e)
	{
		if (Event != null) Event(this, e);
	}
}
vai
public class Class
{
	public event EventHandler Event;

	public Class()
	{
		ThreadPool.QueueUserWorkItem(new WaitCallback(Work));
	}

	void Work(object state)
	{
		OnEvent(new EventArgs());
	}

	protected void OnEvent(EventArgs e)
	{
		if (Event != null)
		{
			if (Thread.CurrentThread == Application.Current.Dispatcher.Thread)
			{
				Event(this, e);
			}
			else
			{
				Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new EventHandler(Event), this, e)
			}
		}
	}
}
?


(Lasīt komentārus) - (Ierakstīt jaunu komentāru)


[info]cannibalsmith
2009-03-15 10:34 (saite)
IMHO vieglaak ir abstraheet visu multi-thread shitu prom no UI koda nevis piebaazt to ar InvokeRequired paarbaudeem.

(Atbildēt uz šo) (Iepriekšējais) (Diskusija)


[info]bubu
2009-03-15 13:43 (saite)
Tādā gadījumā tu savu klasi padari par sliktu performancei ne-multithreadētā ne-UI kodā.
Bet nu dari kā zini - tava klase :)

(Atbildēt uz šo) (Iepriekšējais)


(Lasīt komentārus) -

Neesi iežurnalējies. Iežurnalēties?