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)

Nopūsties:

No:
Lietotājvārds:
Parole:
Ievadi te 'qws' (liidzeklis pret spambotiem):
Temats:
Tematā HTML ir aizliegts
  
Ziņa:

Gandrīz jau aizmirsu pateikt – šis lietotājs ir ieslēdzis IP adrešu noglabāšanu. Operatore Nr. 65.
Neesi iežurnalējies. Iežurnalēties?