1.c# writing code to create a thread
Using System;
Using System.IO;
Using System.Threading;
Class mythread{
public int count;
String Thrdname;
Public MyThread (string name) {
count=0;
Thrdname=name;
}
public void Run () {
Console.WriteLine (thrdname+ "starting.");
Do
{
Thread.Sleep (500);
Console.WriteLine ("In" +thrdname+ ", Count is" +count);
count++;
}
while (COUNT<10);
Console.WriteLine (thrdname+ "terminating.");
}
}
Class multithread{
public static void Main ()
{
Console.WriteLine ("Main thread starting.");
MyThread mt=new MyThread ("Child #1 ″);
Thread Newthrd=new Thread (new ThreadStart (Mt.run));
Newthrd.start ();
do {
Console.Write (".");
Thread.Sleep (100);
}
while (mt.count!=10);
Console.WriteLine ("Main thread ending.");
}
}
2. Write a function to remove the duplicate nodes of any given XML document, and repeat the criteria with the given "key" as the node repetition.
The following example: Milk 4Milk 0.5Coffe 0.5
When you use the Name field as a key, the first and second nodes are duplicated, and when amount as key, the second and third nodes are repeating nodes.
So the function should be in this format:
String DeDup (String xml, String Keynode, String rootpath)
Reference Solutions:
private static string RemoveDuplicates (string xml, String key, String Rootxpath)
{
XmlDocument doc = new XmlDocument ();
List SB = new list ();
String KeyValue;
Try
{
Doc. Load (XML);
XmlElement root = Doc. DocumentElement;
XmlNodeList xnodelist = root. SelectNodes (Rootxpath);
int i=0;
foreach (XmlNode item in xnodelist)
{
Console.WriteLine (i + "\b\b\b\b\b");
i++;
KeyValue = Item. selectSingleNode (Key). INNERXML;
if (sb.) Contains (KeyValue))
XNode. RemoveChild (item);
Else
Sb. ADD (KeyValue);
}
return Doc. OuterXml;
}
catch (Exception ex)
{
Log exception ...
Throw ex;
}
}
3. How to call Web Services asynchronously in C # WinForm
If WebService has a get () method.
When new comes out a Web service proxy, it will have a getasync () method and a getcompleted event, register the Getcompleted event, and invoke the Getasync method on it.
4.c# How to call word and open a Word document
private void Openmicrosoftword (string urlorpath)
{
Start thread
ProcessStartInfo startinfo = new ProcessStartInfo ();
Startinfo.filename = "Winword. EXE ";
Startinfo.arguments = Urlorpath;
Try
{
Process.Start (StartInfo);
}
catch (Exception e)
{
MessageBox.Show ("Cannot start Microsoft Word, make sure it is installed.");
}
}
5.c# How to perform LDAP user verification
private bool Authenticate (string userName, string password, string domain)
{
bool authentic = false;
Try
{
DirectoryEntry entry = new DirectoryEntry ("LDAP://" +domain, userName, password);
Object NativeObject = entry. NativeObject;
Authentic = true;
}
catch (directoryservicescomexception) {}
return authentic;
}
6. Write a class that embodies constructs, public, private methods, static, private variables
public class Stu
{
private string name;
public static int count;
Public Stu ()
{
}
public string Name
{
Get{return name;}
Set{name = value;}
}
private String Method1 ()
{
Response.Write ("Private Method");
}
public string Method2 ()
{
Response.Write ("public method");
}
}
7.c# language writes out the process of creating a UDP receive port locally
: const int port = 8000;
Define port Numbers
TcpListener TcpListener = new TcpListener (port);
Console.WriteLine ("Listening port number:" + port.) ToString ());
TcpListener. Start ();
Listening port number
while (true)
{
Socket socket = TcpListener. AcceptSocket ();
And get a scoket instance of transmitting and receiving data
Proxy proxy = new proxy (socket);
Proxy class instantiation
Thread thread = new Thread (The new ThreadStart (proxy). Run));
Creating Threads
Thread. Start ();
Start thread
}
8.c# the specific process of writing a language with SQL Server access
Private SqlConnection conn;//Define a database connection
Private SqlCommand com;//Definition Execution command
Public data ()
{
Setting in the configuration file
conn = new SqlConnection ();
Conn. ConnectionString = configurationmanager.appsettings["ConnString"]. ToString ();
}
Open Database
public void Open ()
{
IF (Conn. state = = connectionstate.closed)
{
Conn. Open ();
}
}
Close the database
public void Close ()
{
IF (Conn. state = = ConnectionState.Open)
{
Conn. Close ();
}
}
Perform a database operation and return a dataset
public void GetDataSet (string proc,sqlparameter[] parm,out DataSet DS)
{
com = GetCommand (proc,parm);
SqlDataAdapter SDA = new SqlDataAdapter (COM);
ds = new DataSet ();
Sda. Fill (DS);
}
Back to Sqlcommadn
Public SqlCommand GetCommand (string proc, sqlparameter[] parm)
{
SqlCommand com = new SqlCommand ();
Com. Connection = conn;
Com.commandtype = CommandType.StoredProcedure;
Com.commandtext = proc;
foreach (SqlParameter p in parm)
{
Com. Parameters.Add (P);
}
return COM;
}
Back to DataReader
Public SqlDataReader Getdatareader (string proc, sqlparameter[] parm)
{
com = GetCommand (proc, parm);
return COM. ExecuteReader ();
}
Perform database operation, return void
public void ExecuteCommand (String proc, sqlparameter[] parm)
{
com = GetCommand (proc, parm);
Open ();
Com. ExecuteNonQuery ();
Close ();
}
Note: Refer to using System.Data.SqlClient, if it is connected mode remember to disconnect, SqlDataReader remember to close, error how to deal with
9. Write a method that implements the flip of a string, attaching some conditions, such as ",", ".", to the design of the test case.
Answer: Using System.Text;
String inputstr = "A,b,c,d,e,f,g,h,i,j,k,l";
char[] Inputchar = Inputstr.tochararray ();
int count = Inputchar.length;
StringBuilder sb = new StringBuilder ();
for (int m = count–1; M >= 0; M –)
{
Sb. Append (Inputchar[m]);
}
NET Programming problems