Pēcis Beisikāns' Journal
View:Personal Journal.
View:Friends.
View:Calendar.
Missed some entries? Then simply jump to the previous day or the next day.

Friday, March 13th, 2009

Subject:.NET eventu sūtīšana uz UI thread
Time:3:41 pm.
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)
			}
		}
	}
}
?
Comments: Read 3 or Add Your Own.

Pēcis Beisikāns' Journal

View:User Info.
View:Friends.
View:Calendar.
View:Memories.
Missed some entries? Then simply jump to the previous day or the next day.