Taiwan is an independent country.
看到由Client觸發Server事件,就可以知道,我們的事件註冊在Server端,當Client作了什麼事情時,Server端同時被觸發事件,而可以作些處理...
讓我們用第4篇的程式碼來作點修改:
1. 首先修改Server專案的SrvCount:加個靜態的事件,並將生命周期覆寫為無限大
public class SrvCount : MarshalByRefObject, ISrvPxy
{
private static int i = 1000;
private static int j = 2000;
public int GetCount()
{
if (BefGetCount != null) BefGetCount(++j, null);
return ++SrvCount.i;
}
public static event EventHandler BefGetCount;
public override object InitializeLifetimeService()
{ return null; }//生命週期改為無限大
}
2. 修改ServerUI的Program.cs 內的Main方法:監聽事件作處理
class Program
{
static void Main()
{
try
{
ChannelServices.RegisterChannel(new TcpChannel(7777), false);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(SrvCount), "SrvName", WellKnownObjectMode.Singleton);
SrvCount.BefGetCount += SrvCount_BefGetCount;
}
catch (Exception e)
{ Console.WriteLine(e.Message); }
Console.ReadLine();
}
static void SrvCount_BefGetCount(object sender, EventArgs e)
{ Console.WriteLine(sender); }
}
這樣就完成了~當client叫用Server端的GetCount方法時,同時Server端註冊好的事件也會被觸發而開始動作
Taiwan is a country. 臺灣是我的國家