Professional Documents
Culture Documents
NET MATERIAL
Threads
There are two main ways of multi-threading which .NET encourages: starting your own threads with
ThreadStart delegates, and using the ThreadPool class either directly (using
ThreadPool.QueueUserWorkItem) or indirectly using asynchronous methods (such as
Stream.BeginRead, or calling BeginInvoke on any delegate)
Example1:
C#
using System;
using System.Threading;
public class Test
{
static void Main()
{
Counter foo = new Counter();
ThreadStart job = new ThreadStart(foo.Count);
Thread thread = new Thread(job);
thread.Start();
for (int i=0; i < 5; i++)
{
Console.WriteLine ("Main thread: {0}", i);
Thread.Sleep(1000);
}
}
}
public class Counter
{ public void Count()
{ for (int i=0; i < 10; i++)
{
Console.WriteLine ("Other thread: {0}", i);
Thread.Sleep(500);
}
}
}
Example2:
C#
using System;
using System.Threading;
Example 3:
Imports System
Imports System.Drawing
Imports System.Threading
Imports System.Windows.Forms
Imports System.IO
public class MainClass
Shared Sub Main()
Dim Thrd As Thread
Dim TStart As New ThreadStart(AddressOf BusyThread)
Thrd = New Thread(TStart)
Thrd.Priority = ThreadPriority.Highest
Thrd.Start()
Console.WriteLine(Thrd.ThreadState.ToString("G"))
Console.WriteLine("Thrd.IsAlive " & Thrd.IsAlive)
If Thrd.IsAlive Then
Thrd.Abort()
Thrd.Join()
End If
End Sub
Shared Sub BusyThread()
While True
'Console.Write("thread ")
End While
End Sub
End Class
Ex 4:
Imports System
Imports System.Drawing
Imports System.Threading
Imports System.Windows.Forms
Imports System.IO
public class MainClass
Shared Sub Main()
Dim Thrd As Thread
Dim TStart As New ThreadStart(AddressOf BusyThread)
Thrd = New Thread(TStart)
Thrd.Priority = ThreadPriority.Highest
Thrd.Start()
Console.WriteLine(Thrd.ThreadState.ToString("G"))
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim num_desserts&
Dim satisfaction_quotient#
num_desserts = 100
satisfaction_quotient# = 1.23
Dim an_integer As Integer = 100
Dim a_long As Long = 10000000000
Dim a_double As Double = 1.0
Dim a_boolean As Boolean = True
Dim a_date As Date = #12/31/2007#
Dim i As Long = 123L
Dim ch As Char = "X"c
Dim flags As ULong
flags = 100 ' Decimal 100.
flags = &H64 ' Hexadecimal &H64 = 6 * 16 + 4 = 96 + 4 = 100.
flags = &O144 ' Octal &O144 = 1 * 8 * 8 + 4 * 8 + 4 = 64 + 32 + 4 = 100.
Console.WriteLine(flags) ' Decimal.
Console.WriteLine(Hex(flags)) ' Hexadecimal.
Console.WriteLine(Oct(flags)) ' Octal.
End Sub
End Class
Class Test
Public Shared Sub Main()
Try
' Create an instance of StreamReader to read from a file.
' The using statement also closes the StreamReader.
Using sr As New StreamReader("TestFile.txt")
Dim line As String
' Read and display lines from the file until the end of
' the file is reached.
Do
Write to file
Vb
Imports System
Imports System.IO
Imports System.Text
Imports System.Collections.Generic
C#
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
class Program
{ static void Main(string[] args)
{
string mydocpath =
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
StringBuilder sb = new StringBuilder();
Delegates
In the .NET Framework, callback functions are just as useful and as pervasive as they are in
unmanaged programming for Windows®. However, the .NET Framework has the added bonus of
providing a type-safe mechanism called delegates. I'll begin my discussion of delegates by
examining how to use one. The code in Figure 1 shows how to declare, create, and use delegates.
Notice the Set class at the top of Figure 1. Pretend that this class contains a set of items that will be
processed individually. When you create a Set object, you pass the number of items it should manage
to its constructor. The constructor then creates an array of Objects and initializes each to an integer
value.
The Set class also defines a public delegate. The delegate indicates the signature of a callback
method. In this example, a Feedback delegate identifies a method that takes three parameters (an
Object, an Int32, and another Int32) and returns void. In a way, a delegate is very much like a C/C++
typedef that represents the address of a function.
In addition, the Set class defines a public method called ProcessItems. This method takes one
parameter, feedback, which is a reference to a Feedback delegate object. ProcessItems iterates
through all the elements of the array, and for each element the callback method (specified by the
feedback variable) is called. This callback method is passed the value of the item being processed,
the item number, and the total number of items in the array. The callback method can process each
item any way it chooses.
Figure 1 DelegateDemo.cs
using System;
using System.WinForms; // In beta2: System.Windows.Forms
using System.IO;
class Set {
private Object[] items;
class App {
[STAThreadAttribute]
static void Main() {
StaticCallbacks();
InstanceCallbacks();
}
void FeedbackToFile(
Object value, Int32 item, Int32 numItems) {
LEFT JOIN
SELECT column_name(s) FROM table_name1 LEFT JOIN table_name2 ON
table_name1.column_name=table_name2.column_name
RIGHT
SELECT column_name(s) FROM table_name1 RIGHT JOIN table_name2 ON
table_name1.column_name=table_name2.column_name
Full
SELECT column_name(s) FROM table_name1 FULL JOIN table_name2 ON
table_name1.column_name=table_name2.column_name
Mobile tech
Mobile applications can now be developed to deliver any types of data, to any user, any place in the
world!
Different mobile devices support different programming languages. Some support WAP and WML,
some support HTML or a limited version of HTML, and some support both or a different language.
To support all types of mobile devices, developers must create one different application for each
language.
With .NET Mobile, Microsoft has introduced a new platform for developing mobile applications.
This tutorial is about how to develop applications with an extension to the .NET Framework, called
the Microsoft Mobile Internet Toolkit (MMIT) or simply .NET Mobile.
What You Should Already Know
Before you continue you should have a basic understanding of the following:
HTML / XHTML
XML namespaces
ASP .NET
If you want to study these subjects first, find the tutorials on our Home page.
.NET Mobile
.NET Mobile is an extension to Microsoft ASP.NET and the Microsoft's .NET Framework.
.NET Mobile is a set of server-side Forms Controls to build applications for wireless mobile devices.
These controls produce different output for different devices by generating WML, HTML, or
compact HTML.
How Does It Work?
The following table shows how the .NET Mobile works:
Mobile Devices
The Internet
Internet Information Services - IIS
The .NET Framework
ASP.NET
.NET Mobile
A web client requests a web page
The request travels the Internet
The request is received by IIS
The request is handled by the .NET framework
The requested page is compiled by ASP.NET
.NET Mobile handles any mobile device requirements
The page is returned to the client
Software Requirements
To develop mobile applications with .NET Mobile, you must have the following components:
1. Windows 2000 Professional/Server with IIS 5
2. All Windows 2000 service packs
3. The ASP.NET framework
4. Microsoft Mobile Internet Toolkit (MMIT)
5. Internet Explorer 5.5 or later
6. A WAP simulator
<mob:Form runat="server">
<mob:Label runat="server">Hello Spiro Solution</mob:Label>
</mob:Form>