15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

emoting <strong>and</strong> events ❘ OC227<br />

The method that will be implemented in the client has some strict requirements. It can only have input<br />

parameters — return types, ref, <strong>and</strong> out parameters are not allowed — <strong>and</strong> the argument types must be<br />

either [Serializable] or remotable (derived from MarshalByRefObject). These requirements are fulfilled<br />

by the parameters that are defined with the EventH<strong>and</strong>ler delegate.<br />

Within the RemoteObject class, declare an event name Status of type EventH<strong>and</strong>ler.<br />

The client must add an event h<strong>and</strong>ler to the Status event to receive status information from the remote object:<br />

public class RemoteObject : MarshalByRefObject<br />

{<br />

public RemoteObject()<br />

{<br />

Console.WriteLine("RemoteObject constructor called");<br />

}<br />

public event EventH<strong>and</strong>ler Status;<br />

code snippet RemotingAndEvents/RemoteObject/RemoteObject.cs<br />

The LongWorking() method checks whether an event h<strong>and</strong>ler is registered before the event is fired by<br />

calling Status(this, e). To verify that the event is fired asynchronously, fire an event at the start of the<br />

method before doing the Thread.Sleep() <strong>and</strong> after the sleep:<br />

public void LongWorking(int ms)<br />

{<br />

Console.WriteLine("RemoteObject: LongWorking() Started");<br />

StatusEventArgs e = new StatusEventArgs(<br />

"Message for Client: LongWorking() Started");<br />

// fire event<br />

if (Status != null)<br />

{<br />

Console.WriteLine("RemoteObject: Firing Starting Event");<br />

Status(this, e);<br />

}<br />

System.Threading.Thread.Sleep(ms);<br />

e.Message = "Message for Client: LongWorking() Ending";<br />

// fire ending event<br />

if (Status != null)<br />

{<br />

Console.WriteLine("RemoteObject: Firing Ending Event");<br />

Status(this, e);<br />

}<br />

Console.WriteLine("RemoteObject: LongWorking() Ending");<br />

}<br />

event arguments<br />

As you’ve seen in the RemoteObject class, the class StatusEventArgs is used as an argument for the<br />

delegate. With the Serializable attribute an instance of this class can be transferred from the server to<br />

the client. Here is a simple property of type string to send a message to the client:<br />

[Serializable]<br />

public class StatusEventArgs : EventArgs<br />

{<br />

public StatusEventArgs(string message)<br />

{<br />

this.Message = message;<br />

}<br />

public string Message { get; set; }<br />

}<br />

code snippet RemotingAndEvents/RemoteObject/RemoteObject.cs<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!