C# Note
C# Note
C# Note
xsd orders.xml
To generate a class definition of a strongly typed DataSet from the schema definition:
Now suppose there is the following time-consuming method that needs to be called
asynchronously:
The bottom textbox and button has nothing to do with the delegate process and is only
used to check that the UI is not locked before the method returns.
1. Define a delegate type with the same signature as the method to call;
private delegate int CalcDelegate(int input1, int input2, ref string strResult);
2. Define a callback method which will be called when the dispatched thread
finishes the time-consuming method call and returns. The content of the
callback method will be discussed later.
...
CalcDelegate mDeleg;
4. When the wrapped method returns, the thread will call the callback method
with an IAsyncResult parameter. In the callback method, we are supposed to
acquire a handle to the delegate contained in the IAsyncResult, and call its
EndInvoke to get back the result.
// Following line is used when this callback method can not acquire a handle of the
delegate, mDeleg, for
int i = (int)ar.AsyncState;
The IAsyncResult object that is passed to the callback method does not contain the result
of the time-consuming method. Instead it contains the identity of the calling method and
a handle to the delegate. This asynchronous process is like a dry cleaning store with a
procedure a bit different from common ones. You call in first, leave a job to be done, and
your address – the callback method. When the job is done, the store contacts you through
the address you left, and provides a job number. You go back to the store with the job
number to pick up the job done. The IAsyncResult object contains this job number. You
have to call the time-consuming method again with this job number to get back the real
result.
ar.AsyncWaitHandle.WaitOne();
strResult = null;
tbResult.Text = Convert.ToString(iResult);
tbMsg.Text = strResult;
3. The same delegate handle is used to call both BeginInvoke and EndInvoke.
1.1. Event
The event architecture offers a mechanism for an event generator to notify an event
consumer that something happened. The event generator does not need to know anything
about the consumer. Instead it publishes a method definition in the form of a delegate,
plus an event handle. When the event happens, the generator will call the unknown event
handling method of the consumer through the event handle. A third party which knows
both the generator and the consumer is responsible to connect the consumer’s event
handling method with the published event handle of the generator.
Example:
// The event and the event args are defined by the event generator.
mstrName = strName;
}
if (MyEventExposed != null)
MyEventExposed(this, args);
else
Console.WriteLine("Event not connected to event consumer!");
generator.GenerateEvent();
Console.ReadLine();
}
1.2. Convert Between Objects & byte Array with
BinaryFormatter
In the following example, we create a object of a simple class Car, convert it into a byte
array, and convert it back.
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace For_Testing_Simple_Code
[Serializable]
class Car
mstrMake = strMake;
mstrModel = strModel;
miYear = iYear;
mstrReg = strReg;
}
return mstrMake + ", " + mstrModel + ", " + miYear + ", " + mstrReg;
class Class1
[STAThread]
formatter.Serialize(memStream1, car1);
memStream1.Close();
Console.WriteLine(car2.ToString());
Console.ReadLine();
using System.IO;
using System.Security.Cryptography;
namespace For_Simple_Test
class Class1
[STAThread]
byte [] desKey = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
byte [] desIV = {0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78};
fsEncrypted,
des.CreateEncryptor(desKey, desIV),
CryptoStreamMode.Write);
//Read from the input file, then encrypt and write to the output file.
encryptStream.Write(buffer, 0, iLenth);
encryptStream.Close();
fsOriginal.Close();
fsEncrypted.Close();
//**** Decryption Process ****
fsDecrypted,
des.CreateDecryptor(desKey, desIV),
CryptoStreamMode.Write);
decryptStream.Write(buffer, 0, iLenth);
decryptStream.Close();
fsEncrypted.Close();
fsDecrypted.Close();