Friday, March 11, 2011

C # blocks module ABC (1)


1. The first C # program: Hello World classic routines

"Hello World" can be said to learn a programming language per the first routine of the. We can NotePad, Wordpad any editor, etc. Enter the following C # code, and save it as helloworld.cs, the final implementation of the command line csc helloworld.cs to run file:

/ / Using system

using System;

class Hello

(

static void Main () (/ / display output on console

Console.WriteLine ("Hello, C # World!");

)

)

2. Use OpenFileDialog class to browse or open files

With VC + + in the same way CfileDialog the Open, C # in the OpenFileDialog class can be used to open a file. This class is derived from the FileDialog. Use this class in the OpenFile method to open a file, then can be processed by (steam) 鏉?read this document.

Consider the following example code, it uses the OpenFileDialog class to browse a file:

OpenFileDialog fdlg = new OpenFileDialog ();

fdlg.Title = "C # Corner Open File Dialog";

fdlg.InitialDirectory = @ "c:";

fdlg.Filter = "All files (*.*)|*.*| All files (*.*)|*.*";

fdlg.FilterIndex = 2;

fdlg.RestoreDirectory = true;

if (fdlg.ShowDialog () == DialogResult.OK)

(

textBox1.Text = fdlg.FileName;

)

Title line to set the title of the Open dialog box, Filter an act to open the file type set up a filter, FileName row contains the selected file name.

The figure is the result of running:






[Next]



3. From C # to call COM components (post connection)

. NET Framework is a natural development of COM, the two share many of the core elements, including component reuse and language neutrality. For backward compatibility, COM Interop can use existing COM components without requiring modifications to the original components. When a. NET Framework developers want to COM code into a management application, the COM Interop feature can be used to introduce the relevant COM types. After the introduction, the COM types can be used. This is pre-connected. But sometimes you need to target the latter connection, which. NET, can be achieved using the name space mapping can be connected through late to call COM objects.

Here an application routine, it will call Excel, and later connect it through the use of video.

Late connection will use Reflectionb the Type class, the Type class has many ways to get COM objects, as we have already used GetTypeFromProgID ("Application"), this method from the system registry to get COM ID, then use the STATIC class members of the Activator.CreateInstance () to create the COM object to a new exemplify.

To call the COM object's methods, functions and properties, it must contain the correct settings using the Type object InvokeMethod () method. This method accepts variable number of parameters, one of the most important one is the method of the type of ex property (get or set). In the example we used the set for the Excel.Visible property, so that Excel application visible.

We will try. NET environment to call Excel application. This is a post-connected application, because if they are pre-connected, then you will need to use COM object of the RCW (RunTime Callable Wraper: the running time can call the package) to perform the following command-line program tblimp the task:

ex. c:> tblimp / out:

Download COMinDOTNET.zip, this is a console application. Here is the code calling excel:

file / / Variable

Type excel;

object [] parameter = new object [1];

object excelObject;

try

(

file / / Get the excel object

excel = Type.GetTypeFromProgID ("Excel.Application");

file / / Create instance of excel

excelObject = Activator.CreateInstance (excel);

file / / Set the parameter whic u want to set

parameter [0] = true;

file / / Set the Visible property

excel.InvokeMember ("Visible", BindingFlags.SetProperty, null, excelObject, parameter);

)

catch (Exception e)

(

Console.WriteLine ("Error Stack (0)", e.Message);

)

finally

(

file / / When this object is destroyed the Excel application will be closed

file / / So Sleep for sometime and see the excel application

Thread.Sleep (5000);

file / / Relaese the object

file / / GC.RunFinalizers ()

)




[Next]



4. To create multi-threaded applications

In. NET and C # in the preparation of a multi-threaded application will be very little more easily. Even for those who have never used C # applications written for beginners too many threads, just follow these simple steps you can achieve the purpose.

Name space definitions

In. NET, multi-threading capability is defined in System.Threading namespace. Therefore, before using any thread classes, must be defined System.Threading namespace. Defined as follows:

using System.Threading;

Start thread

System.threading name space Thread class represents a thread object, the object of this class can create new threads, delete, suspend and resume threads. The following code uses the Thread class to create a new thread, and then start this thread:

thread = new Thread (new ThreadStart (WriteData));

thread.Start ();

One WriteData this thread to perform a function, the code is as follows:

protected void WriteData ()

(

string str;

for (int i = 0; i <= 10000; i + +)

(

str = "Secondary Thread" + i.ToString ();

Console.WriteLine (listView1.ListItems.Count, str, 0, new string []{""});

Update ();

)

)

Kill thread

Thread class's Abort method is used to permanently kill a thread. Note, however, before calling the Abort method must determine whether the activation of the thread, that is, the value judgments thread.IsAlive:

if (thread.IsAlive)

(

thread.Abort ();

)

Suspension of threads

Thread.Sleep method is used to pause a thread for some time, the code is as follows:

thread.Sleep ();

Set thread priority

We can use the Thread class ThreadPriority property is set thread priority. The range of thread priority is Normal, AboveNormal, BelowNormal, Highest or Lowest. Consider the following setup code:

thread.Priority = ThreadPriority.Highest;

Delay Thread

Suspend Thread class methods can delay a thread. To the calling thread is delayed until the Resume method.

if (thread.ThreadState = ThreadState.Running)

(

thread.Suspend ();

)

Recovery is delayed thread

Call the Resume method can be delayed resumption of a thread. If the thread is not delayed, Resume method is invalid.

if (thread.ThreadState = ThreadState.Suspended)

(

thread.Resume ();

)










VBScript ReDim Statement



Describe the C # call an external process



First job out of the ivory tower beginning how the election



Expert Backup And Restore



FLV to MOV



Wizard Personal Interest



The birth of kissing fish



Prerelease download ATi Catalyst 5.5



Using De exterminate rabbits annoying Ads RMVB movies



Directory Web Development



TS Converter



MOV to FLV



Oracle memory structure process memory details