Скачиваний:
64
Добавлен:
15.03.2015
Размер:
4.31 Mб
Скачать

384

Day 16

You learned how to make asynchronous calls to Web Services in the second part of the lesson. This advanced technique can be especially helpful for Web Services that don’t return results immediately. You saw how to create client programs that use asynchronous calls from a Web form and from a console application.

Today’s lesson is the last one that deals explicitly with Web Services. Future lessons, such as Day 18, “Configuring Internet Applications,” will show you how to configure Web Services, and Day 19, “Securing Internet Applications,” will include sample programs that allow you to create secure services.

Q&A

QI plan on using a commercial Web Service for one of my projects, and I don’t have any control over the way this service is implemented. Can I still use asynchronous calling techniques, like the ones described in this lesson?

AYes. Creating an asynchronous Web Service client doesn’t depend on the remote Web Service that you are using. Visual Studio .NET and the .NET framework allow you to create asynchronous clients for any kind of Web Service. The key methods in the Web Service proxy class, such as BeginSomeMethod and EndSomeMethod, are generated for you automatically.

QI plan on creating a standalone Windows Forms application that will call a Web Service asynchronously. Which technique in this lesson should I use?

AWindows Forms applications are similar to the console application from Listing 16.9, in that they don’t support client-side cookies. You can use the techniques from Listing 16.9 in Windows Forms applications to call a Web Service asynchronously. You would need to move the code in the console application’s Main method into the Form event handler that calls the Web Service.

Workshop

The Workshop provides quiz questions to help you solidify your understanding of the material covered today as well as exercises to give you experience using what you have learned. Try to understand the quiz and exercise before continuing to tomorrow’s lesson. Answers are provided in Appendix A, “Answers to Quizzes and Exercises.”

Quiz

1.What attribute must be used to enable session state for a public Web method?

2.Why does using session state in a Web Service cause complications for some client programs?

Putting It All Together with Web Services

385

3.Does a special attribute enable application state for Web Services?

4.What technique is used to deal with Web Services that take a long time to return a result from a method call?

5.Do you have to take any special actions to enable asynchronous calling for a Web Service?

6.What interface do objects returned from asynchronous method calls support?

7. Do Web forms support asynchronous method calls to Web Services?

16

Exercises

1.Modify SlowService to return error messages at random.

2.Modify one of the asynchronous clients to handle the randomly generated error messages from the new SlowService created in Exercise 1.

CHAPTER 23

Multi-Threading

IN THIS CHAPTER

• Creating New Threads 498

• Synchronization 499

498

Extreme C#

PART IV

Multi-threading is the capability to execute multiple threads simultaneously. As systems become more sophisticated, it’s often desirable to take advantage of machine capabilities such as multi-processor architectures. Even on single processing machines, multi-thread- ing provides performance enhancements in some applications.

Besides boosting performance, multi-threading is handy in other scenarios such as eventdriven operating environments. For instance, it’s often necessary to invoke a progress indicator in its own thread so it can receive updates to an ongoing operation in another thread. Multi-threaded programs allow a program to engage in simultaneous events that can provide a user with real-time feedback.

Creating New Threads

The act of creating and invoking a new thread in C# is relatively straightforward. The process involves creating a Thread object, instantiating a ThreadStart delegate with a delegate method handler, and passing the ThreadStart delegate to the new Thread object. All that’s remaining is to start the thread, and off it runs. Listing 23.1 shows how to create and execute a new thread.

LISTING 23.1 Creating a New Thread: SingleThread.cs

using System;

using System.Threading;

///<summary>

///Shows how to create a single thread of execution.

///</summary>

class SingleThread

{

static void Main(string[] args)

{

SingleThread st = new SingleThread();

Thread th = new Thread(new ThreadStart(st.SayHello));

th.Start();

}

public void SayHello()

{

Console.WriteLine(“Hello from a single thread.”);

}

}