C # building Block module ABC (i) (transferred from Next generation technology network)

Source: Internet
Author: User
Tags command line contains sleep static class thread thread class
First C # program: Classic Routines Hello World

"Hello World" can be said to be the first routine to learn every programming language. We can enter the following C # code in any editor such as Notepad, WordPad, and Save as HelloWorld.cs, and finally execute the CSC helloworld.cs on the command line to run the file:


Using System
Using System;
Class Hello
{
static void Main () {//Display output on console
Console.WriteLine ("Hello,c# world!");
}
}


To browse or open a file with the OpenFileDialog class

As with the CFileDialog open method in VC + +, the OpenFileDialog class in C # can be used to open a file. This class is derived from the FileDialog. Open a file with the OpenFile method in this class, and then you can read the file through the stream (steam).

Consider the following routine code, which 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 One line sets the caption for the Open dialog box, and the file type opened by the filter behavior sets a filter, and the filename row contains the selected file name.


The following figure is the result of the operation:




Calling COM components from C #

. NET Framework is a natural development of COM, which shares many core elements, including reuse of components and neutrality of language. For backward compatibility, COM interop can use an existing COM component without requiring modifications to the original component. When a. NET framework developer wants to incorporate COM code into a management application, it is possible to introduce the associated COM type with COM interop functionality. Once introduced, this COM type can be used. This belongs to the Prophase connection. But sometimes you need a late connection to the object, which is. NET, a COM object can be invoked through a post connection using a namespace mapping.


This describes an application routine that calls Excel and makes it visible by using late joins.


Later connections will use the Reflectionb type class, which has many ways to get COM objects, just as we have used Gettypefromprogid ("Application"), which obtains COM IDs from the system registry. A new example of this COM object is then created using the members of the Static class, Activator.CreateInstance ().


To invoke the methods, functions, and properties of a COM object, you must use the InvokeMethod () method that contains the correctly set type object. This method accepts some parameter variables, the most important of which is the ex property (get or set) of the method type. In the example we used the Set property for Excel.visible to make the Excel application visible.


We will try to be in. NET environment to invoke the Excel application. This is a late-connected application, because if it is a front-end connection you will need to use the COM object's RCW (RunTime callable Wraper: The Run-time callable Package) to complete the following command-line program Tblimp tasks:


Ex. c:\> Tblimp/out:


Download Comindotnet.zip, this is a console application. Here's the code that calls Excel:


Variable
Type Excel;
Object[] parameter= new object[1];
Object Excelobject;
Try
{
Get the Excel object
Excel = Type.gettypefromprogid ("Excel.Application");
Create instance of Excel
Excelobject = Activator.CreateInstance (Excel);
Set the parameter whic u want to set
Parameter[0] = true;
Set the Visible property
Excel. InvokeMember ("Visible", BindingFlags.SetProperty, NULL, excelobject, parameter);
}
catch (Exception e)
{
Console.WriteLine ("Error Stack {0}", e.message);
}
Finally
{
When this object is destroyed the Excel application'll be closed
So sleep for sometime and the Excel application
Thread.Sleep (5000);
Relaese the Object
Gc. Runfinalizers ()
}


Creating Multithreaded Applications


Writing a multithreaded application in. NET and C # will be very easy. Even for beginners who have never written a multithreaded application in C #, just follow these simple steps to accomplish this.


Defining namespaces

In. NET, multithreaded functionality is defined in the System.Threading namespace. Therefore, you must define the System.Threading namespace before you use any of the thread classes. The method is defined as follows:


Using System.Threading;


Start thread

The thread class in the System.Threading namespace represents a threading object that can be used to create new threads, delete, pause, and resume threads. The following code uses the thread class to create a new thread and then starts the thread:


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

Thread. Start ();


Where WriteData is a function to be executed by this thread, 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


The abort method of the thread class is used to permanently kill a thread. Note, however, that it is important to determine whether the thread is still active before calling the Abort method, which is to judge thread. Value of IsAlive:


if (thread. IsAlive)

{

Thread. Abort ();

}


Pausing a thread


The Thread.Sleep method is used to suspend a thread for a period of time, as follows:


Thread. Sleep ();


Set the priority of a thread


We can use the ThreadPriority property of the thread class to set the thread's precedence. The range of thread priority values is normal, AboveNormal, BelowNormal, highest, or lowest. Take a look at the following Setup code:


Thread. Priority = Threadpriority.highest;


Delay thread


The suspend method of the thread class can delay a thread. The thread is deferred until the resume method is invoked.


if (thread. ThreadState = threadstate.running)

{

Thread. Suspend ();

}


Recovering a thread that is delayed


Call the Resume method to recover a deferred thread. If the thread is not delayed, the resume method is invalid.


if (thread. ThreadState = threadstate.suspended)

{

Thread. Resume ();

}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.