using System;
using System.Threading;
class MainClass
{
public static void Main()
{
int firstInt = 25;
int secondInt = 80;
Console.WriteLine("firstInt initial value = {0}", firstInt);
Console.WriteLine("secondInt initial value = {0}", secondInt);
Interlocked.Increment(ref secondInt);
Console.WriteLine("secondInt after increment = {0}", secondInt);
}
}
Month: February 2011
Decrement
using System;
using System.Threading;
class MainClass
{
public static void Main()
{
int firstInt = 25;
int secondInt = 80;
Console.WriteLine("firstInt initial value = {0}", firstInt);
Console.WriteLine("secondInt initial value = {0}", secondInt);
Interlocked.Decrement(ref firstInt);
Console.WriteLine("firstInt after decrement = {0}", firstInt);
}
}
Read Write ini file
using System;
using System.Runtime.InteropServices;
using System.Text;
class MainClass {
[DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileString")]
private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
[DllImport("kernel32.dll", EntryPoint = "WritePrivateProfileString")]
private static extern bool WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);
static void Main(string[] args) {
string val;
val = GetIniValue("A", "Key1", "initest.ini");
Console.WriteLine(val);
WriteIniValue("B", "Key1", "New Value", "initest.ini");
val = GetIniValue("C", "Key1", "initest.ini");
Console.WriteLine(val);
WriteIniValue("D", "Key1", "Value1", "initest.ini");
}
public static string GetIniValue(string section, string key, string filename) {
int chars = 256;
StringBuilder buffer = new StringBuilder(chars);
string sDefault = "";
if (GetPrivateProfileString(section, key, sDefault,
buffer, chars, filename) != 0) {
return buffer.ToString();
} else {
return null;
}
}
public static bool WriteIniValue(string section, string key, string value, string filename) {
return WritePrivateProfileString(section, key, value, filename);
}
}
Decrypting data.
using System;
using System.IO;
using System.Security;
using System.Security.Cryptography;
public class StoreCryptoStream : ICryptoStream {
static byte[] tag1 = {(byte)'[',(byte)'S',(byte)'a',(byte)'u' ,(byte)'d' ,(byte)'e',(byte)'s' ,(byte)']'};
static byte[] tag2 = {(byte)'[',(byte)'S',(byte)'a',(byte)'u' ,(byte)'r' ,(byte)'c',(byte)'2' ,(byte)']'};
FileStream fs;
public StoreCryptoStream(FileStream fout) {
fs = fout;
}
public virtual void CloseStream() { fs.Close(); }
public virtual void CloseStream(Object obj) { fs.Close(); }
public virtual void SetSink(ICryptoStream pstm) { }
public virtual void SetSource(CryptographicObject co) { }
public virtual ICryptoStream GetSink() { return null; }
public virtual void Write(byte[] bin) {
int len = bin.GetLength(0);
Write(bin, 0, len);
}
public virtual void Write(byte[] bin, int start, int len) {
fs.Write(bin, start, len);
}
}
public class MainClass {
static byte[] symKey;
static byte[] symIV;
private static bool GenerateKey(string password) {
int len;
char[] cp = password.ToCharArray();
len = cp.GetLength(0);
byte[] bt = new byte[len];
for (int i = 0; i < len; i++) { bt[i] = (byte)cp[i]; } symKey = new byte[8]; symIV = new byte[8]; SHA1_CSP sha = new SHA1_CSP(); sha.Write(bt); sha.CloseStream(); for (int i = 0; i < 8; i++) { symKey[i] = sha.Hash[i]; } for (int i = 8; i < 16; i++) { symIV[i - 8] = sha.Hash[i]; } return true; } private static void DecryptData(string infile, string outfile) { FileStream fin = new FileStream(infile, FileMode.Open, FileAccess.Read); FileStream fout = new FileStream(outfile, FileMode.OpenOrCreate, FileAccess.Write); fout.SetLength(0); byte[] bin = new byte[4096]; long totlen = fin.Length; long rdlen = 8; int len; SymmetricAlgorithm des = new DES_CSP(); StoreCryptoStream scs = new StoreCryptoStream(fout); SymmetricStreamDecryptor ssd = des.CreateDecryptor(symKey, symIV); ssd.SetSink(scs); scs.SetSource(ssd); while (rdlen < totlen) { len = fin.Read(bin, 0, 4096); ssd.Write(bin, 0, len); rdlen = rdlen + len; } ssd.CloseStream(); fin.Close(); fout.Close(); } public static void Main(string[] args) { GenerateKey(args[0]); DecryptData(args[1], args[2]); } } [/csharp]
Encrypting data.
using System;
using System.IO;
using System.Security;
using System.Security.Cryptography;
public class StoreCryptoStream : ICryptoStream {
static byte[] tag1 = {(byte)'[',(byte)'S',(byte)'a',(byte)'u' ,
(byte)'d' ,(byte)'e',(byte)'s' ,(byte)']'};
static byte[] tag2 = {(byte)'[',(byte)'S',(byte)'a',(byte)'u' ,
(byte)'r' ,(byte)'c',(byte)'2' ,(byte)']'};
FileStream fs;
public StoreCryptoStream(FileStream fout) {
fs = fout;
}
public virtual void CloseStream() { fs.Close(); }
public virtual void CloseStream(Object obj) { fs.Close(); }
public virtual void SetSink(ICryptoStream pstm) { }
public virtual void SetSource(CryptographicObject co) { }
public virtual ICryptoStream GetSink() { return null; }
public virtual void Write(byte[] bin) {
int len = bin.GetLength(0);
Write(bin, 0, len);
}
public virtual void Write(byte[] bin, int start, int len) {
fs.Write(bin, start, len);
}
}
public class MainClass {
static byte[] symKey;
static byte[] symIV;
private static bool GenerateKey(string password) {
try {
int len;
char[] cp = password.ToCharArray();
len = cp.GetLength(0);
byte[] bt = new byte[len];
for (int i = 0; i < len; i++) { bt[i] = (byte)cp[i]; } symKey = new byte[8]; symIV = new byte[8]; SHA1_CSP sha = new SHA1_CSP(); sha.Write(bt); sha.CloseStream(); for (int i = 0; i < 8; i++) { symKey[i] = sha.Hash[i]; } for (int i = 8; i < 16; i++) { symIV[i - 8] = sha.Hash[i]; } return true; } catch (Exception e) { Console.WriteLine("An Exception Occurred in Generating eys:" + e.ToString()); return false; } } private static void EncryptData(string infile, string outfile) { try { FileStream fin = new FileStream(infile, FileMode.Open, FileAccess.Read); FileStream fout = new FileStream(outfile, FileMode.OpenOrCreate, FileAccess.Write); fout.SetLength(0); byte[] bin = new byte[4096]; long totlen = fin.Length; long rdlen = 0; int len; SymmetricAlgorithm des = new DES_CSP(); StoreCryptoStream scs = new StoreCryptoStream(fout); SymmetricStreamEncryptor sse = des.CreateEncryptor(symKey, symIV); SHA1_CSP sha = new SHA1_CSP(); sse.SetSink(sha); sha.SetSource(sse); sha.SetSink(scs); scs.SetSource(sha); while (rdlen < totlen) { len = fin.Read(bin, 0, 4096); sse.Write(bin, 0, len); rdlen = rdlen + len; } sse.CloseStream(); fin.Close(); fout.Close(); } catch (Exception e) { Console.WriteLine("An exception occurred while encrypting :" + e.ToString()); } } public static void Main(string[] args) { GenerateKey(args[0]); EncryptData(args[1], args[2]); } } [/csharp]
sample code for the EndInvoke method
using System;
using System.Threading;
public delegate int DelegateClass(out DateTime start,out DateTime stop);
public class Starter {
public static void Main() {
DelegateClass del = MethodA;
DateTime start;
DateTime stop;
IAsyncResult ar = del.BeginInvoke(out start, out stop,null, null);
ar.AsyncWaitHandle.WaitOne();
int elapse = del.EndInvoke(out start, out stop, ar);
Console.WriteLine("Start time: {0}", start.ToLongTimeString());
Console.WriteLine("Stop time: {0}", stop.ToLongTimeString());
Console.WriteLine("Elapse time: {0} seconds",elapse);
}
public static int MethodA(out DateTime start, out DateTime stop) {
start = DateTime.Now;
Thread.Sleep(5000);
stop = DateTime.Now;
return (stop - start).Seconds;
}
}
What happens if an unhandled exception is raised in a multicast delegate?
using System;
using System.Threading;
public delegate void DelegateClass();
public class Starter {
public static void Main() {
Console.WriteLine("Running on primary thread");
try {
DelegateClass del = MethodA;
IAsyncResult ar = del.BeginInvoke(null, null);
del.EndInvoke(ar);
} catch (Exception except) {
Console.WriteLine("Running on primary thread");
Console.WriteLine("Exception caught: " + except.Message);
}
}
public static void MethodA() {
if (Thread.CurrentThread.IsThreadPoolThread == true) {
Console.WriteLine("Running on a thread pool thread");
} else {
Console.WriteLine("Running on primary thread");
}
throw new Exception("failure");
}
}

