Wei-Meng Lee - C# 2008 Programmer's Reference

Здесь есть возможность читать онлайн «Wei-Meng Lee - C# 2008 Programmer's Reference» весь текст электронной книги совершенно бесплатно (целиком полную версию без сокращений). В некоторых случаях можно слушать аудио, скачать через торрент в формате fb2 и присутствует краткое содержание. Город: Indianapolis, Год выпуска: 2009, ISBN: 2009, Издательство: Wiley Publishing, Inc., Жанр: Программирование, на английском языке. Описание произведения, (предисловие) а так же отзывы посетителей доступны на портале библиотеки ЛибКат.

C# 2008 Programmer's Reference: краткое содержание, описание и аннотация

Предлагаем к чтению аннотацию, описание, краткое содержание или предисловие (зависит от того, что написал сам автор книги «C# 2008 Programmer's Reference»). Если вы не нашли необходимую информацию о книге — напишите в комментариях, мы постараемся отыскать её.

C# 2008 Programmers Reference provides a concise and thorough reference on all aspects of the language. Each chapter contains detailed code samples that provide a quick and easy way to understand the key concepts covered.

C# 2008 Programmer's Reference — читать онлайн бесплатно полную книгу (весь текст) целиком

Ниже представлен текст книги, разбитый по страницам. Система сохранения места последней прочитанной страницы, позволяет с удобством читать онлайн бесплатно книгу «C# 2008 Programmer's Reference», без необходимости каждый раз заново искать на чём Вы остановились. Поставьте закладку, и сможете в любой момент перейти на страницу, на которой закончили чтение.

Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

Doing something...

Doing something...

Continuing with the execution...

Doing something...

Doing something...

...

A thread executes until:

□ It reaches the end of its life (method exits), or

□ You prematurely kill (abort) it.

Aborting a Thread

You can use the Abort()method of the Threadclass to abort a thread after it has started executing. Here's an example:

class Program {

static void Main(string[] args) {

Thread t = new Thread(new ThreadStart(DoSomething));

t.Start();

Console.WriteLine("Continuing with the execution...");

while (!t.IsAlive);

Thread.Sleep(1);

t.Abort();

Console.ReadLine();

}

static void DoSomething() {

try {

while (true) {

Console.WriteLine("Doing something...");

}

} catch (ThreadAbortException ex) {

Console.WriteLine(ex.Message);

}

}

}

When the thread is started, you continue with the next statement and print out the message " Continuing with the execution...". You then use the IsAliveproperty of the Thread class to find out the execution status of the thread and block the execution of the Main()function (with the while statement) until the thread has a chance to start. The Sleep()method of the Threadclass blocks the current thread ( Main()) for a specified number of milliseconds. Using this statement, you are essentially giving the DoSomething()function a chance to execute. Finally, you kill the thread by using the Abort()method of the Threadclass.

The ThreadAbortExceptionexception is fired on any thread that you kill. Ideally, you should clean up the resources in this exception handler (via the finallystatement):

static void DoSomething() {

try {

while (true) {

Console.WriteLine("Doing something...");

}

} catch (ThreadAbortException ex) {

Console.WriteLine(ex.Message);

} finally {

//---clean up your resources here---

}

}

The output of the preceding program may look like this:

Continuing with the execution...

Doing something...

Doing something...

Doing something...

Doing something...

Doing something...

Doing something...

Doing something...

Thread was being aborted.

Notice that I say the program may look like this. When you have multiple threads running in your application, you don't have control over which threads are executed first. The OS determines the actual execution sequence and that is dependent on several factors such as CPU utilization, memory usage, and so on. It is possible, then, that the output may look like this:

Doing something...

Continuing with the execution...

Doing something...

Doing something...

Doing something...

Doing something...

Doing something...

Doing something...

Thread was being aborted.

While you can use the Abort()method to kill a thread, it is always better to exit it gracefully whenever possible.

Here's a rewrite of the previous program:

class Program {

private static volatile bool _stopThread = false;

static void Main(string[] args) {

Thread t = new Thread(new ThreadStart(DoSomething));

t.Start();

Console.WriteLine("Continuing with the execution...");

while (!t.IsAlive);

Thread.Sleep(1);

_stopThread = true;

Console.WriteLine("Thread ended.");

Console.ReadLine();

}

static void DoSomething() {

try {

while (!_stopThread) {

Console.WriteLine("Doing something...");

}

} catch (ThreadAbortException ex) {

Console.WriteLine(ex.Message);

} finally {

//---clean up your resources here---

}

}

}

First, you declare a static Boolean variable call _stopThread:

private static volatile bool _stopThread = false;

Notice that you prefix the declaration with the volatilekeyword, which is used as a hint to the compiler that this variable will be accessed by multiple threads. The variable will then not be subjected to compiler optimization and will always have the most up-to-date value.

To use the _stopThreadvariable to stop the thread, you modify the DoSomething()function, like this:

while (!_stopThread) {

Console.WriteLine("Doing something...");

}

Finally, to stop the thread in the Main()function, you just need to set the _stopThreadvariable to true:

_stopThread = true;

Console.WriteLine("Thread ended.");

The output of this program may look like this:

Continuing with the execution.

Doing something...

Doing something...

Doing something...

Doing something...

Doing something...

Doing something...

Thread ended.

Doing something...

The DoSomething()function may print another message after the " Thread ended." message. That's because the thread might not end immediately. To ensure that the " Thread ended." message is printed only after the DoSomething()function ends, you can use the Join()method of the Threadclass to join the two threads:

static void Main(string[] args) {

Thread t = new Thread(new ThreadStart(DoSomething));

t.Start();

Console.WriteLine("Continuing with the execution...");

while (!t.IsAlive);

Thread.Sleep(1);

_stopThread = true;

//---joins the current thread (Main()) to t---

t.Join();

Console.WriteLine("Thread ended.");

Console.ReadLine();

}

The Join()method essentially blocks the calling thread until the thread terminates. In this case, the Threadended message will be blocked until the thread ( t) terminates.

The output of the program now looks like this:

Continuing with the execution.

Doing something...

Doing something...

Doing something...

Doing something...

Doing something...

Doing something...

Thread ended.

Figure 10-4 shows graphically the two different threads of execution.

Читать дальше
Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

Похожие книги на «C# 2008 Programmer's Reference»

Представляем Вашему вниманию похожие книги на «C# 2008 Programmer's Reference» списком для выбора. Мы отобрали схожую по названию и смыслу литературу в надежде предоставить читателям больше вариантов отыскать новые, интересные, ещё непрочитанные произведения.


Отзывы о книге «C# 2008 Programmer's Reference»

Обсуждение, отзывы о книге «C# 2008 Programmer's Reference» и просто собственные мнения читателей. Оставьте ваши комментарии, напишите, что Вы думаете о произведении, его смысле или главных героях. Укажите что конкретно понравилось, а что нет, и почему Вы так считаете.