Programming C# 2nd Edition phần 9 potx

59 282 0
Programming C# 2nd Edition phần 9 potx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Programming C#, 2nd Edition 467 In this case the server is assumed to be running on your local machine, so the URI is http://localhost, followed by the port for the server (65100), followed in turn by the endpoint you declared in the server (theEndPoint). The remoting service should return an object representing the interface you've requested. You can then cast that object to the interface and begin using it. Because remoting cannot be guaranteed (the network might be down, the host machine may not be available, and so forth), you should wrap the usage in a try block: try { Programming_CSharp.ICalc calc = obj as Programming_CSharp.ICalc; double sum = calc.Add(3,4); You now have a proxy of the Calculator operating on the server, but usable on the client, across the process boundary and, if you like, across the machine boundary. Example 19-4 shows the entire client (to compile it, you must include a reference to ICalc.dll as you did with CalcServer.cs). Example 19-4. The remoting Calculator client namespace Programming_CSharp { using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; public class CalcClient { public static void Main( ) { int[] myIntArray = new int[3]; Console.WriteLine("Watson, come here I need you "); // create an Http channel and register it // uses port 0 to indicate won't be listening HttpChannel chan = new HttpChannel(0); ChannelServices.RegisterChannel(chan); // get my object from across the http channel MarshalByRefObject obj = (MarshalByRefObject) RemotingServices.Connect (typeof(Programming_CSharp.ICalc), "http://localhost:65100/theEndPoint"); try { // cast the object to our interface Programming_CSharp.ICalc calc = obj as Programming_CSharp.ICalc; [...]... Thread.CurrentThread.Name); } } 480 Programming C#, 2nd Edition Output (excerpt): Started thread Thread1 Thread Thread1 Decrementer: 1000 Thread Thread1 Decrementer: 99 9 Thread Thread1 Decrementer: 99 8 Started thread Thread2 Thread Thread1 Decrementer: 99 7 Thread Thread2 Incrementer: 0 Thread Thread1 Decrementer: 99 6 Thread Thread2 Incrementer: 1 Thread Thread1 Decrementer: 99 5 Thread Thread2 Incrementer:... change: Incrementer: Incrementer: Decrementer: Incrementer: Decrementer: Incrementer: Decrementer: Incrementer: Decrementer: Incrementer: Decrementer: Incrementer: Decrementer: 0 1 1000 2 99 9 3 99 8 4 99 7 5 99 6 6 99 5 20.1.4 Killing Threads Typically, threads die after running their course You can, however, ask a thread to kill itself by calling its Abort( ) method This causes a ThreadAbortException... 98 9 Thread Thread3 Incrementer: 2 Thread Thread1 Decrementer: 98 8 Thread Thread3 Incrementer: 3 Thread Thread1 Decrementer: 98 7 Thread Thread3 Incrementer: 4 Thread Thread1 Decrementer: 98 6 Thread Thread3 Incrementer: 5 // Thread Thread1 Decrementer: 1 Thread Thread3 Incrementer: 99 7 Thread Thread1 Decrementer: 0 Thread Thread3 Incrementer: 99 8 Thread Thread1 Exiting Thread Thread3 Incrementer: 99 9... Incrementer: 2 Thread Thread1 Decrementer: 99 4 Thread Thread2 Incrementer: 3 Started thread Thread3 Thread Thread1 Decrementer: 99 3 Thread Thread2 Incrementer: 4 Thread Thread2 Incrementer: 5 Thread Thread1 Decrementer: 99 2 Thread Thread2 Incrementer: 6 Thread Thread1 Decrementer: 99 1 Thread Thread3 Incrementer: 0 Thread Thread2 Incrementer: 7 Thread Thread1 Decrementer: 99 0 Thread Thread3 Incrementer: 1 Thread... Locks in NET), as well as more sophisticated tools such as a Monitor class Each is discussed later in this chapter 484 Programming C#, 2nd Edition Incrementing and decrementing a value is such a common programming pattern, and one which so often needs synchronization protection, that C# offers a special class, Interlocked, just for this purpose Interlocked has two methods, Increment and Decrement, which... 7 8 9 485 Programming C#, 2nd Edition Thread Thread Thread Thread Thread Thread Thread Thread Thread Thread Thread ThreadTwo ThreadOne ThreadTwo ThreadOne ThreadTwo ThreadOne ThreadTwo ThreadOne ThreadTwo ThreadOne ThreadTwo Incrementer: Incrementer: Incrementer: Incrementer: Incrementer: Incrementer: Incrementer: Incrementer: Incrementer: Incrementer: Incrementer: 10 11 12 13 14 15 16 17 18 19 20... decremented when you pop out This will allow you to display the subdirectories indented under the parent directories The complete listing is shown in Example 21-1 495 Programming C#, 2nd Edition Example 21-1 Recursing through subdirectories namespace Programming_ CSharp { using System; using System.IO; class Tester { public static void Main( ) { Tester t = new Tester( ); // choose the initial subdirectory... file, along with its length and the date and time it was last accessed Example 21-2 Exploring files and subdirectories namespace Programming_ CSharp { using System; using System.IO; class Tester { public static void Main( ) { Tester t = new Tester( ); 498 Programming C#, 2nd Edition // choose the initial subdirectory string theDirectory = Environment.GetEnvironmentVariable("SystemRoot"); // call the method... directories = dir.GetDirectories( ); foreach (DirectoryInfo newDir in directories) { dirCounter++; // increment the counter ExploreDirectory(newDir); } indentLevel ; // pop a directory level 499 Programming C#, 2nd Edition // static member variables to keep track of totals // and indentation level static int dirCounter = 1; static int indentLevel = -1; // so first push = 0 static int fileCounter = 0;... be handled in the order received.") 487 Programming C#, 2nd Edition When a thread is finished with the monitor, it can mark the end of its controlled area of code with a call to Exit( ): Monitor.Exit(this); Example 20-4 continues the simulation, providing synchronized access to a counter variable using a Monitor Example 20-4 Using a Monitor object namespace Programming_ CSharp { using System; using . Programming C#, 2nd Edition 467 In this case the server is assumed to be running on your local machine,. Example 19- 4 shows the entire client (to compile it, you must include a reference to ICalc.dll as you did with CalcServer.cs). Example 19- 4. The remoting Calculator client namespace Programming_ CSharp. (typeof (Programming_ CSharp.ICalc), "http://localhost:65100/theEndPoint"); try { // cast the object to our interface Programming_ CSharp.ICalc calc = obj as Programming_ CSharp.ICalc;

Ngày đăng: 12/08/2014, 23:22

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan