/// <summary>
 /// Open text files and begin to index
 /// </summary>
 public void MainIndexProcess()
 {
     try
     {
         //define unique fileid for each file
         int             id       = 0;
         int             threadid = int.Parse(Thread.CurrentThread.Name);
         int             NumberOfBytesForAChar   = Encoding.Unicode.GetBytes("a").Length;
         long            SizeOfRamIndexPerThread = IndexingConfig.SizeOfRamIndex / IndexingConfig.NumberOfIndexingThreads;
         DirectoryInfo   dinfo           = new DirectoryInfo(IndexingConfig.sourceDirectoryLocation);
         FileInfo[]      Files           = dinfo.GetFiles("*.txt");
         RAMIndexManager ramIndexManager = null;
         foreach (FileInfo file in Files)
         {
             ++id;
             if (ramIndexManager == null)
             {
                 ramIndexManager = new RAMIndexManager(IndexingConfig.indexlocation);
                 ramIndexManager.IndexMedia(file, id);
                 //If RAM index reaches limit
                 if (ramIndexManager.GetRAMSize() >= SizeOfRamIndexPerThread || Files.Length >= id)
                 {
                     HandOverRamIndex(ref ramIndexManager);
                 }
             }
         }
         //for small file numbers this can be harcoded here to call the new thread to write index to disk but better implementation would be to call only when size exceeds.
         RamIndexHandler.InvokeRamindexHandlerThread();
     }
     catch (Exception e)
     {
         Console.WriteLine("Thread " + Thread.CurrentThread.Name + " Aborted.");
     }
 }
        /// <summary>
        /// Start the indexing process in a separate thread.
        /// </summary>
        public static void StartThreads()
        {
            IndexingClass ixClass = new IndexingClass();

            //For now use only a single thread. TODO: handle multiple simultaneous threads.
            NumberOfIndexingThreads = 1;
            IndexingThreads         = new Thread[NumberOfIndexingThreads];
            for (int i = 0; i < NumberOfIndexingThreads; i++)
            {
                IndexingThreads[i] = new Thread(new ThreadStart(ixClass.MainIndexProcess));
                IndexingThreads[i].IsBackground = true;
                IndexingThreads[i].Name         = i.ToString();
                IndexingThreads[i].Start();
            }
            SizeOfRamIndex          = SizeOfRamIndex * 1024 * 1024;
            SizeOfRamIndexPerThread = SizeOfRamIndex / NumberOfIndexingThreads;
            //User another thread which is triggered by previous thread. This can be helpful while indexing large number of files. One thread writes to mem and other thread mem to drive.
            RamIndexHandler.Clear();
            ramIndexHandler      = new Thread(new ThreadStart(handler.ManageRamIndexes));
            ramIndexHandler.Name = "RamIndexHandler";
            ramIndexHandler.Start();
        }
 /// <summary>
 /// Populate the index info table so that other thread can pick up.
 /// </summary>
 /// <param name="ramIndexManager">Instance of class ramIndexManager</param>
 private void HandOverRamIndex(ref RAMIndexManager ramIndexManager)
 {
     ramIndexManager.CloseWriter();
     RamIndexHandler.AddRamIndex(ramIndexManager);
     ramIndexManager = null;
 }