using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.IO;
using System.Xml;
using System.Configuration;
using System.Data;
using System.Net;
using System.IO.Compression;
using System.Reflection;
using System.Runtime.InteropServices;
namespace abc
{
class Program
{
static int numberOfThreads = 1;
static int lastProcessor = 0;
[DllImport("kernel32.dll")]
private static extern IntPtr GetCurrentThread();
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern UIntPtr SetThreadAffinityMask(IntPtr handle, UIntPtr mask);
static void Main(string[] args)
{
int concurrentThread = 1;
int.TryParse(ConfigurationManager.AppSettings["concurrentThread"], out concurrentThread);
if (concurrentThread < 1)
concurrentThread = 1;
if (concurrentThread > 99)
concurrentThread = 99;
Console.ReadLine();
Console.WriteLine("Press enter to exit...");
Console.WriteLine("start processing xml files...");
int.TryParse(ConfigurationManager.AppSettings["LastProcessor"], out lastProcessor);
numberOfThreads = concurrentThread;
for (int i = 0; i < concurrentThread; i++)
{
Thread workThread = new Thread(new ParameterizedThreadStart(ProcessXMLFiles));
workThread.Start(i);
}
Console.ReadLine();
Console.ReadLine();
}
static void ProcessXMLFiles(Object stateInfo)
{
int threadNum = (int)stateInfo;
ulong procMask = (ulong)System.Diagnostics.Process.GetCurrentProcess().ProcessorAffinity;
ulong mask = (ulong) lastProcessor >>threadNum;
UIntPtr lastAff = SetThreadAffinityMask(GetCurrentThread(), (UIntPtr)mask);
Console.WriteLine("set thread affinity = {0}", (ulong)lastAff);
string threadKey = "T" + threadNum.ToString().PadLeft(2, '0');
DateTime timeStart = DateTime.Now;
string xmlString = "<Events>" + "</Events>";
XmlDocument eventXmlDoc = new XmlDocument();
for(int j = 0; j < 900; j++)
{
Console.WriteLine("{0}: processing #{1}", threadKey, j.ToString());
for (int i = 0; i < 1000; i++)
{
eventXmlDoc.LoadXml(xmlString);
}
}
//========== for timing ======================
DateTime timeEnd = DateTime.Now;
Console.WriteLine("XML process time = {0} seconds, [{1}, {2}]", (int)timeEnd.Subtract(timeStart).TotalSeconds, timeStart, timeEnd);
//========== end =============================
//security2Writer.Close();
Console.WriteLine("end of processing xml files...");
}
}
}