Geeks With Blogs

News
Geekus Con Livus Malcolm Anderson's home for Geeks With Lives

On page 47 of Wrox's "C# Threading Handbook" in the code listing for the GenerateText method, you will see that each string builder append line starts with sb.Length.  Just remove the sb.Length and the code will run fine. 

 

If anyone knows some one at Apress, who handles the errata for their old wrox book, I would like to be able to put this errata in a more centralized location.

 

Thanks

 

Here's the code listing.

 

using System;

using System.Threading;

using System.Text;

namespace TimerExample

{

class TimerExample

{

private string message;

private static Timer tmr;

private static bool complete;

static void Main(string[] args)

{

TimerExample obj = new TimerExample();

Thread t = new Thread(new ThreadStart(obj.GenerateText));

t.Start();

TimerCallback tmrCallBack = new TimerCallback(obj.GetText);

tmr = new Timer(tmrCallBack, null, TimeSpan.Zero, TimeSpan.FromSeconds(2));

do

{

if (complete)

break;

} while (true);

Console.WriteLine("Exiting Main");

Console.ReadLine();

}

public void GenerateText()

{

StringBuilder sb = new StringBuilder();

for (int i = 1; i < 200; i++)

{

Thread.Sleep(100);

sb.Append("This is Line ");

sb.Append(i.ToString());

sb.Append(System.Environment.NewLine);

}

message = sb.ToString();

}

public void GetText(object state)

{

if (message == null)

return;

Console.WriteLine("Message is :");

Console.WriteLine(message);

tmr.Dispose();

complete = true;

}

}

}

Posted on Monday, May 29, 2006 10:49 AM Stupid Code Tricks | Back to top


Comments on this post: Wrox "C# Threading Handbook" Errata - Part 2

# re: Wrox "C# Threading Handbook" Errata - Part 2
Requesting Gravatar...
Better later, than never !!
dear Malcom, I am reading and testing this book.
But I don not understand how works this snippet, as explained in the book.
Why the timer polls once ?

[Tobin Titus and F.C. Ferracchiati have never aswered to all readers to provide any source code. Sometime I have a suspect that they did not try the code !!]


Left by marco on Apr 03, 2008 3:58 PM

# re: Wrox "C# Threading Handbook" Errata - Part 2
Requesting Gravatar...
I moved recently, and haven't unpacked all of my books yet, so off the top of my head, I don't remember what the point of this particular code is, only that it didn't work as it was written, and that I figured out how to make it work and then posted the code.

The only advice I can give you is to set some breakpoints and walk through the code, there's not a whole lot going on in any of the snippets, so you should do ok.

Left by malcolm anderson on Apr 13, 2008 4:54 PM

Your comment:
 (will show your gravatar)


Copyright © Malcolm Anderson | Powered by: GeeksWithBlogs.net