Developer .NET UPDATE, June 18, 2002

Developer .NET UPDATE—brought to you by Windows & .NET Magazine Network.
http://www.winnetmag.net


THIS ISSUE SPONSORED BY

Microsoft ASP.NET Connections
http://www.asp-connections.com

XML Web Services Connections
http://www.xmlconnections.com/xml/default.asp
(below DEVELOPER .NET PERSPECTIVES)


SPONSOR: MICROSOFT ASP.NET CONNECTIONS

MICROSOFT ASP.NET CONNECTIONS TO CO-LOCATE WITH SQL SERVER MAGAZINE LIVE!
Microsoft ASP.NET Connections, along with VS.NET Connections will co-locate with SQL Server Magazine LIVE! in Orlando, FL, at the Hyatt Grand Cypress Resort on Oct. 27 - 30. Keep your career on track. Learn from top third-party gurus, Microsoft product developers, and magazine authors. This is a must attend event to keep your skills sharp and to dig deeper into the .NET Framework and what it means for you and your company. It's chock full of practical information you can use right away. Real content with real value for you. Register for one conference and attend the other two conferences for FREE. Go to
http://www.asp-connections.com


June 18, 2002—In this issue:

1. DEVELOPER .NET PERSPECTIVES

  • Sharing in .NET

2. ANNOUNCEMENTS

  • Windows Scripting Solutions for the Systems Administrator
  • Special 2-For-1 Subscription Offer!

3. RESOURCE

  • Featured Thread: Date Format Inconsistency

4. NEW AND IMPROVED

  • Easily Communicate with Secure Clients

5. CONTACT US

  • See this section for a list of ways to contact us.

1. DEVELOPER .NET PERSPECTIVES
(contributed by Bill Sheldon, [email protected])

  • SHARING IN .NET

  • In the June 11 edition of Developer .NET Perspectives, I wrote about creating a second thread within your process. This week, I expand on that topic by showing you how to communicate with that thread.

    Logically, a main thread can communicate with a worker thread three times: at start, at finish, and while running. The first opportunity, at start, actually occurs before the thread starts. Remember that the ThreadStart class contains a method's address. Under Microsoft .NET, this method is attached to the instance of the worker class. When you define your worker class, you can assign a property (e.g., Public Message of type String) to that class.

    To show you how simple working with threads in .NET can be, let's walk through the steps of using Visual Studio .NET to create a basic demonstration. We'll work with Visual Basic (VB) to create a simple MyThread application that will create a second thread and let you stop that thread after it has started.

    Start by creating a simple VB Windows form project, and place a button on the resulting form. Add a new class called Worker to your project. In this empty class, add the public property Message and a method of 'Run'. The 'Run' method simply calls a MessageBox with the class's Message property. On the form, create the OnClick event handler for your button. In the OnClick method's code block, begin by creating a new instance of the project's Worker class. Also in the OnClick method, add a value (e.g., 'Hello World') to the Worker class's Message property. Next, add the code to create an instance of the ThreadStart class. The constructor relies on the result of applying the 'AddressOf' operator on the Worker.Run method that will run when the worker thread is started. After you add this code, you can create an instance of a Thread object that's based on this ThreadStart instance. Finally, add the line that actually calls the instance method Start on the Thread object. Now, test the project and ensure that pressing the button creates the thread and displays the message box with the message passed to the thread.

    You've now created a form that can communicate to a worker thread to start. If the message was the name of a file to process, this form would pass that information into the worker thread. This ability to pass information into a thread provides a way to address the second communication opportunity: at finish. Add a simple label to the Windows form and leave the default name of Label1. Then, add a Public property called mylabel to the Worker class and define it as type System.Windows.Forms.Label. Add a line to the end of the 'Run' method that sets the mylabel Text property to "Done". The final step is to modify the OnClick event handler related to the Windows form. Before you start the thread, set the Worker thread's mylabel property to the Label1 object that you added to the form.

    When you run the MyThread application, everything should work smoothly, but how do you know that events are really threaded? In the OnClick event handler, just after the instance of the thread is started, add a line to set the Text property of Label1 to Running. The MessageBox in the worker thread blocks that thread's final command until the MessageBox is cleared. Thus, when you test this version, the UI will display the label text "Running" until you close the message box and the worker thread can finish.

    The third communication opportunity occurs while the thread is running. You can enable this type of communication by simply setting a property in the thread, but what you really need is a shared object between the two threads that permits status and information to be interchanged. The structure should support the equivalent of 2-way communication so that the main thread can ask the worker thread to stop and the worker thread can display its status relative to completion.

    Add another class, Status, to the project, and this time use the keyword Shared to create a property. The declaration should look like

    Public Shared StopWork as Integer

    Then, add a new button to the Windows form, with a caption of Stop. In the button's OnClick event handler, simply reference the class by name and set this property to 1 (i.e., Status.StopWork = 1). Notice that you don't create an instance of the Status class. Shared properties don't require an instance because only one value is possible, regardless of the number of instances of a class. In fact, this example uses zero instances, but one copy of these values occurs. For more information about the Shared keyword, go to this following URL.

    Before testing the MyThread application, go to the Worker class's Run method and add code for a simple For...Next loop around your existing MessageBox call. Within this loop, add a conditional If statement that says

    If Shared.StopWork = 1 Then Exit For

    The worker thread will keep reopening its MessageBox until the StopWork value is set to 1. Start the project, and you can test that the MessageBox repeatedly appears until the user clicks the Stop button on the main form and exits the loop.

    The Shared keyword lets Visual Studio .NET developers create both shared properties and shared methods. Remember that because shared properties and especially shared methods aren't associated with an instance of a class, they can't access instance data for that class--only shared data. Finally, shared methods aren't completely thread-safe and only begin to address the power and risks of multithreaded applications.

    Creating thread-safe code is a future threading topic; however, shared properties and shared methods support access to the same data across different threads. Adding a second shared property called PercentComplete that the worker thread could update with its current status and that the main thread could use to display that status to the user is a simple process. Now you understand not only how simple working with threads in .NET can be but also how easily you can transfer a simple design into working code. If you would like a copy of the code I wrote following this design, feel free to email me.


    SPONSOR: XML WEB SERVICES CONNECTIONS

    XML WEB SERVICES CONNECTIONS TO CO-LOCATE WITH WINDOWS & .NET MAGAZINE LIVE!
    XML Web Services Connections and Windows & .NET Magazine LIVE! will co-locate from Oct. 30 - Nov. 2 immediately following Microsoft ASP.NET Connections, VS.NET Connections, and SQL Server Magazine LIVE! Deep discounts will be available for those attending the entire week of conferences. Save even more by registering now to lock in your Early Bird discount. For more information, go to
    http://www.xmlconnections.com


    2. ANNOUNCEMENTS
    (brought to you by Windows & .NET Magazine and its partners)

  • WINDOWS SCRIPTING SOLUTIONS FOR THE SYSTEMS ADMINISTRATOR

  • So, you're not a programmer, but that doesn't mean you can't learn to create and deploy timesaving, problem-solving scripts. Discover Windows Scripting Solutions online, the Web site that can help you tackle common problems and automate everyday tasks with simple tools, tricks, and scripts. While you're there, check out this article about WMI scripting for beginners!
    http://www.winscriptingsolutions.com

  • SPECIAL 2-FOR-1 SUBSCRIPTION OFFER!

  • Windows & .NET Magazine can help you find the right answer to an urgent problem, discover better ways to manage your enterprise, or prepare for an important migration. How can we improve on a resource this good? Subscribe now at our regular rate, and bring on a friend or colleague for free! This is a limited time offer, so act now!
    http://www.winnetmag.com/sub.cfm?code=21ap2f21

    3. RESOURCE

  • FEATURED THREAD: DATE FORMAT INCONSISTENCY

  • Ernesto is performing a migration from one Microsoft SQL Server 2000 system to another SQL Server 2000 system. On one of his servers, the output date format is ddmmyyyy, and on the other, the format is mmddyyyy. How can he configure consistent date formats? To join the discussion, go to the following URL:
    http://www.sqlmag.com/forums/messageview.cfm?catid=22&threadid=6720

    4. NEW AND IMPROVED
    (contributed by Carolyn Mader, [email protected])

  • EASILY COMMUNICATE WITH SECURE CLIENTS

  • /n software announced IP*Works! SSL for the Microsoft .NET Framework, a Secure Sockets Layer (SSL)-enabled version of IP*Works! written in Visual C# .NET. The software provides programmable components that facilitate sending email, transferring files, browsing the Web, and consuming XML Web services. You can develop tools that have secure Internet connections and digital certificate management. For pricing, contact n/software at 919-544-7770.
    http://www.nsoftware.com

    5. CONTACT US
    Here's how to reach us with your comments and questions:

    This weekly email newsletter is brought to you by Windows & .NET Magazine, the leading publication for Windows professionals who want to learn more and perform better. Subscribe today.
    http://www.winnetmag.com/sub.cfm?code=wswi201x1z

    Receive the latest information about the Windows and .NET topics of your choice. Subscribe to our other FREE email newsletters.
    http://www.winnetmag.net/email

    Hide comments

    Comments

    • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

    Plain text

    • No HTML tags allowed.
    • Web page addresses and e-mail addresses turn into links automatically.
    • Lines and paragraphs break automatically.
    Publish