Solved

Threading Issue

Posted on 2003-12-09
7
1,459 Views
Last Modified: 2007-12-19
Alright. I'm trying to thread something ..and it's worked pretty good so far. This procedure works just fine without threading, but when I run the threading, it tells me that the Main function is already in use elsewhere. Has anyone experienced this before? The code is a little long to put on here... But the problem seems to be with the main thread, not the actual thread.
0
Comment
Question by:derekthornton
7 Comments
 
LVL 2

Author Comment

by:derekthornton
ID: 9904066
As long as it is..Here is the code for the file that is giving me issues.
------------------------------------------------------------------------------
------------------------------------------------------------------------------
I owe too much to this forum. But I am learning fast.
------------------------------------------------------------------------------

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Threading;

namespace OCR
{
      /// <summary>
      /// Summary description for Viewer.
      /// </summary>
      public class Viewer : System.Windows.Forms.Form
      {
            #region Windows Designer Variables
            private DevComponents.DotNetBar.DotNetBarManager menu;
            private DevComponents.DotNetBar.DockSite barLeftDockSite;
            private DevComponents.DotNetBar.DockSite barRightDockSite;
            private DevComponents.DotNetBar.DockSite barTopDockSite;
            private DevComponents.DotNetBar.DockSite barBottomDockSite;
            private System.Windows.Forms.ImageList imageList1;
            private System.Windows.Forms.Splitter splitter1;
            private System.ComponentModel.IContainer components;
            private OCR.ImageControl ImageControl;
            #endregion

            #region Instance Variables

            private const short mIP = 0;
            private const short mRP = 1;

            private const short mOCRSTATE_IDLE = 0;
            private const short mOCRSTATE_ACTIVE = 1;
            private const short mOCRSTATE_ABORT = 2;
            private const short mOCRSTATE_END = 3;
            private System.Windows.Forms.RichTextBox rtfBox;
            private System.Windows.Forms.Splitter splitter2;
            private System.Windows.Forms.ListBox lstPicture;

            private ImageInfo Picture = new ImageInfo();

            private PANEINFO[] mPane = new PANEINFO[mRP];
            private ImageBatch ImageBatch = new ImageBatch();

            private Rectangle theRectangle = new Rectangle
                  (new Point(0, 0), new Size(0, 0));

            Thread OCRThread;

            #endregion

            #region Form Methods

            public Viewer()
            {
                  InitializeComponent();
                  Picture.Viewer = this.ImageControl;
            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if(components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            public static void Main()
            {
                  Application.Run(new Viewer());
            }

            #endregion

            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.components = new System.ComponentModel.Container();
                  System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Viewer));
                  this.menu = new DevComponents.DotNetBar.DotNetBarManager(this.components);
                  this.barBottomDockSite = new DevComponents.DotNetBar.DockSite();
                  this.imageList1 = new System.Windows.Forms.ImageList(this.components);
                  this.barLeftDockSite = new DevComponents.DotNetBar.DockSite();
                  this.barRightDockSite = new DevComponents.DotNetBar.DockSite();
                  this.barTopDockSite = new DevComponents.DotNetBar.DockSite();
                  this.ImageControl = new OCR.ImageControl();
                  this.splitter1 = new System.Windows.Forms.Splitter();
                  this.rtfBox = new System.Windows.Forms.RichTextBox();
                  this.splitter2 = new System.Windows.Forms.Splitter();
                  this.lstPicture = new System.Windows.Forms.ListBox();
                  this.SuspendLayout();
                  //
                  // menu
                  //
                  this.menu.AutoDispatchShortcuts.Add(DevComponents.DotNetBar.eShortcut.F1);
                  this.menu.AutoDispatchShortcuts.Add(DevComponents.DotNetBar.eShortcut.CtrlC);
                  this.menu.AutoDispatchShortcuts.Add(DevComponents.DotNetBar.eShortcut.CtrlA);
                  this.menu.AutoDispatchShortcuts.Add(DevComponents.DotNetBar.eShortcut.CtrlV);
                  this.menu.AutoDispatchShortcuts.Add(DevComponents.DotNetBar.eShortcut.CtrlX);
                  this.menu.AutoDispatchShortcuts.Add(DevComponents.DotNetBar.eShortcut.CtrlZ);
                  this.menu.AutoDispatchShortcuts.Add(DevComponents.DotNetBar.eShortcut.Del);
                  this.menu.AutoDispatchShortcuts.Add(DevComponents.DotNetBar.eShortcut.Ins);
                  this.menu.BottomDockSite = this.barBottomDockSite;
                  this.menu.DefinitionName = "Viewer.menu.xml";
                  this.menu.Images = this.imageList1;
                  this.menu.ImagesLarge = null;
                  this.menu.ImagesMedium = null;
                  this.menu.LeftDockSite = this.barLeftDockSite;
                  this.menu.ParentForm = this;
                  this.menu.RightDockSite = this.barRightDockSite;
                  this.menu.TopDockSite = this.barTopDockSite;
                  this.menu.ItemClick += new System.EventHandler(this.menu_ItemClick);
                  //
                  // barBottomDockSite
                  //
                  this.barBottomDockSite.AccessibleRole = System.Windows.Forms.AccessibleRole.Window;
                  this.barBottomDockSite.BackgroundImageAlpha = ((System.Byte)(255));
                  this.barBottomDockSite.Dock = System.Windows.Forms.DockStyle.Bottom;
                  this.barBottomDockSite.Location = new System.Drawing.Point(0, 405);
                  this.barBottomDockSite.Name = "barBottomDockSite";
                  this.barBottomDockSite.Size = new System.Drawing.Size(622, 21);
                  this.barBottomDockSite.TabIndex = 3;
                  this.barBottomDockSite.TabStop = false;
                  //
                  // imageList1
                  //
                  this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
                  this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
                  this.imageList1.TransparentColor = System.Drawing.Color.Magenta;
                  //
                  // barLeftDockSite
                  //
                  this.barLeftDockSite.AccessibleRole = System.Windows.Forms.AccessibleRole.Window;
                  this.barLeftDockSite.BackgroundImageAlpha = ((System.Byte)(255));
                  this.barLeftDockSite.Dock = System.Windows.Forms.DockStyle.Left;
                  this.barLeftDockSite.Location = new System.Drawing.Point(0, 49);
                  this.barLeftDockSite.Name = "barLeftDockSite";
                  this.barLeftDockSite.Size = new System.Drawing.Size(0, 356);
                  this.barLeftDockSite.TabIndex = 0;
                  this.barLeftDockSite.TabStop = false;
                  //
                  // barRightDockSite
                  //
                  this.barRightDockSite.AccessibleRole = System.Windows.Forms.AccessibleRole.Window;
                  this.barRightDockSite.BackgroundImageAlpha = ((System.Byte)(255));
                  this.barRightDockSite.Dock = System.Windows.Forms.DockStyle.Right;
                  this.barRightDockSite.Location = new System.Drawing.Point(622, 49);
                  this.barRightDockSite.Name = "barRightDockSite";
                  this.barRightDockSite.Size = new System.Drawing.Size(0, 356);
                  this.barRightDockSite.TabIndex = 1;
                  this.barRightDockSite.TabStop = false;
                  //
                  // barTopDockSite
                  //
                  this.barTopDockSite.AccessibleRole = System.Windows.Forms.AccessibleRole.Window;
                  this.barTopDockSite.BackgroundImageAlpha = ((System.Byte)(255));
                  this.barTopDockSite.Dock = System.Windows.Forms.DockStyle.Top;
                  this.barTopDockSite.Location = new System.Drawing.Point(0, 0);
                  this.barTopDockSite.Name = "barTopDockSite";
                  this.barTopDockSite.Size = new System.Drawing.Size(622, 49);
                  this.barTopDockSite.TabIndex = 2;
                  this.barTopDockSite.TabStop = false;
                  //
                  // ImageControl
                  //
                  this.ImageControl.AutoScroll = true;
                  this.ImageControl.BackColor = System.Drawing.SystemColors.Desktop;
                  this.ImageControl.Dock = System.Windows.Forms.DockStyle.Left;
                  this.ImageControl.Image = null;
                  this.ImageControl.ImageSizeMode = OCR.SizeMode.Scrollable;
                  this.ImageControl.Location = new System.Drawing.Point(0, 49);
                  this.ImageControl.Name = "ImageControl";
                  this.ImageControl.Size = new System.Drawing.Size(460, 356);
                  this.ImageControl.TabIndex = 4;
                  //
                  // splitter1
                  //
                  this.splitter1.Location = new System.Drawing.Point(460, 49);
                  this.splitter1.Name = "splitter1";
                  this.splitter1.Size = new System.Drawing.Size(3, 356);
                  this.splitter1.TabIndex = 5;
                  this.splitter1.TabStop = false;
                  //
                  // rtfBox
                  //
                  this.rtfBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                  this.rtfBox.Dock = System.Windows.Forms.DockStyle.Top;
                  this.rtfBox.Location = new System.Drawing.Point(463, 49);
                  this.rtfBox.Name = "rtfBox";
                  this.rtfBox.Size = new System.Drawing.Size(159, 96);
                  this.rtfBox.TabIndex = 6;
                  this.rtfBox.Text = "";
                  //
                  // splitter2
                  //
                  this.splitter2.Dock = System.Windows.Forms.DockStyle.Top;
                  this.splitter2.Location = new System.Drawing.Point(463, 145);
                  this.splitter2.Name = "splitter2";
                  this.splitter2.Size = new System.Drawing.Size(159, 3);
                  this.splitter2.TabIndex = 7;
                  this.splitter2.TabStop = false;
                  //
                  // lstPicture
                  //
                  this.lstPicture.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                  this.lstPicture.Dock = System.Windows.Forms.DockStyle.Fill;
                  this.lstPicture.Location = new System.Drawing.Point(463, 148);
                  this.lstPicture.Name = "lstPicture";
                  this.lstPicture.Size = new System.Drawing.Size(159, 249);
                  this.lstPicture.TabIndex = 8;
                  //
                  // Viewer
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(622, 426);
                  this.Controls.Add(this.lstPicture);
                  this.Controls.Add(this.splitter2);
                  this.Controls.Add(this.rtfBox);
                  this.Controls.Add(this.splitter1);
                  this.Controls.Add(this.ImageControl);
                  this.Controls.Add(this.barLeftDockSite);
                  this.Controls.Add(this.barRightDockSite);
                  this.Controls.Add(this.barTopDockSite);
                  this.Controls.Add(this.barBottomDockSite);
                  this.Name = "Viewer";
                  this.Text = "Viewer";
                  this.Resize += new System.EventHandler(this.Viewer_Resize);
                  this.Load += new System.EventHandler(this.Viewer_Load);
                  this.Layout += new System.Windows.Forms.LayoutEventHandler(this.Viewer_Layout);
                  this.Paint += new System.Windows.Forms.PaintEventHandler(this.Viewer_Paint);
                  this.ResumeLayout(false);

            }
            #endregion

            #region Menu Commands
            private void menu_ItemClick(object sender, System.EventArgs e)
            {
                  BaseItem item = sender as BaseItem;
                  switch(item.Name)
                  {
                        case "bOpen":
                              OpenPicture();
                              break;
                        case "bClose":
                              break;
                        case "bSave":
                              break;
                        case "bSaveAs":
                              break;
                        case "bBatch":
                              BatchDialog();
                              break;
                        case "bScrollable":
                              this.ImageControl.SetScrollable();
                              break;
                        case "bRatio":
                              this.ImageControl.SetRatioStretch();
                              break;
                        case "bOCRPage":
                              OCRPage();
                              break;
                        case "bOCRSelection":
                              OCRArea();
                              break;
                        case "bBatchSelection":
                              BatchSelection();
                              break;
                        case "bOCRPageThread":
                              OCRPageThreaded();
                              break;
                        case "bOCRSelectionThreaded":
                              OCRSelectionThreaded();
                              break;
                  }
            }
            #endregion

            #region OCR Class For Threading

            public class BatchProcess
            {
                  #region Instance Variables ( for Batch Class )

                  ImageBatch iBatch;
                  ImageControl iControl;
                  ImageInfo Picture;
                  DotNetBarManager menu;
                  ListBox lstPicture;
                  RichTextBox rtfBox;

                  #endregion

                  #region BatchProcess Constructor
                  public BatchProcess(ImageBatch image,ImageControl control,
                        ImageInfo Pic,DotNetBarManager mnu,ListBox lst,RichTextBox rtf)
                  {
                        iBatch = image;
                        iControl = control;
                        Picture = Pic;
                        menu = mnu;
                        lstPicture = lst;
                        rtfBox = rtf;
                  }
                  #endregion

                  #region External Methods for Instantiating Batch Processing

                  public void ExecuteBatch()
                  {
                        BatchDialog();
                  }
                  public void ExecuteSelectionBatch()
                  {
                        BatchSelection();
                  }

                  #endregion

                  #region BatchDialog (Whole Page) Process
                  public void BatchDialog()
                  {
                        iBatch.Viewer = iControl;
                        BatchProcessor Batch = new BatchProcessor(iBatch);
                        Batch.ShowDialog();
                  
                        #region Loop For the Batch Process
                        if(Batch.DialogResult != DialogResult.Cancel)
                        {
                              Int32 BatchLength = iBatch.Picture.Length - 1;

                              for(int i = 0; i <= BatchLength;i++)
                              {
                                    API.TOCRInitialise(ref iBatch.Picture[i].JobNo);
                                    iBatch.Picture[i].JobInfo.JobType =
                                          OCRuser.TOCRJOBTYPE_DIBFILE;
                                    PerformOCR(iBatch.Picture[i]);
                              }
                  
                              #region Set all 'Picture' Values to Last Item of Batch
                              Picture = new ImageInfo();

                              Picture.Filename =
                                    iBatch.Picture[BatchLength].Filename;

                              Picture.Image =
                                    iBatch.Picture[BatchLength].Image;

                              Picture.FileFormat =
                                    iBatch.Picture[BatchLength].FileFormat;

                              Picture.Viewer =
                                    iBatch.Picture[BatchLength].Viewer;
                              #endregion
                        }
                        #endregion
                  }
                  #endregion


                  #region Batch Selection (For A Selected Area)
                  public void BatchSelection()
                  {
                        iBatch.Viewer = iControl;
                        BatchProcessor Batch = new BatchProcessor(iBatch);
                        Batch.ShowDialog();
                  
                        if(Batch.DialogResult != DialogResult.Cancel)
                        {
                              Int32 BatchLength = iBatch.Picture.Length - 1;

                              #region Loop for Batch Process
                              for(int i = 0; i <= BatchLength;i++)
                              {
                                    API.TOCRInitialise(ref iBatch.Picture[i].JobNo);

                                    iBatch.Picture[i].Viewer = iControl;

                                    Methods.HandleFile(iBatch.Picture[i]);      
                        
                                    iControl.OCRSelection(iBatch.Picture[i]);
      
                                    Methods.CreateSelectionMMFile(iBatch.Picture[i]);

                                    iBatch.Picture[i].JobInfo.JobType =
                                          OCRuser.TOCRJOBTYPE_DIBFILE;

                                    PerformOCR(iBatch.Picture[i]);
                              }
                              #endregion
                  
                              #region Set 'Picture' Object to Last Item of Batch
                              Picture = new ImageInfo();

                              Picture.Filename =
                                    iBatch.Picture[BatchLength].Filename;

                              Picture.Image =
                                    iBatch.Picture[BatchLength].Image;

                              Picture.FileFormat =
                                    iBatch.Picture[BatchLength].FileFormat;

                              Picture.Viewer =
                                    iBatch.Picture[BatchLength].Viewer;
                              #endregion
                        }
                  }
                  #endregion

                  #region OCR Method (Most Important!!)
                  private void PerformOCR(ImageInfo Picture)
                  {
                        LabelItem lblStatus = menu.GetItem("lblStatus") as LabelItem;
                        lblStatus.Text = "Performing Recognition";

                        Picture.JobStatus =
                              API.TOCRDoJob(Picture.JobNo,
                              ref Picture.JobInfo);
                        
                        if ( API.TOCRGetJobStatus(Picture.JobNo,ref Picture.JobStatus) == 0 )
                        {
                              do
                              {
                                    lblStatus.Text = "Fetching Job Status";

                                    API.TOCRGetJobStatus
                                          (Picture.JobNo,ref Picture.JobStatus);
                              }
                              while( Picture.JobStatus == 0);
                        }
                        else
                        {
                              lblStatus.Text = "Waiting For Job";

                              API.TOCRWaitForJob
                                    (Picture.JobNo,ref Picture.JobStatus);
                        }
                        if ( Picture.JobStatus == OCRuser.TOCRJOBSTATUS_DONE )
                        {
                              lblStatus.Text = "Job Done.Allocating Results";

                              Picture.ResultsHeaderSize =
                                    Marshal.SizeOf(typeof(TOCRResultsHeader));

                              lblStatus.Text = "Header Size: " +
                                    Picture.ResultsHeaderSize.ToString();

                              lstPicture.Items.Add("Header Size: " +
                                    Picture.ResultsHeaderSize.ToString());

                              Picture.ResultsItemSize =
                                    Marshal.SizeOf(typeof(TOCRResultsItem));

                              lblStatus.Text = "Item Size: " +
                                    Picture.ResultsItemSize.ToString();

                              lstPicture.Items.Add("Item Size: " +
                                    Picture.ResultsItemSize.ToString());

                              API.TOCRGetJobResults(Picture.JobNo,
                                    ref Picture.numberOfBytes, IntPtr.Zero);

                              Picture.ptAddress = IntPtr.Zero;
                              Picture.ptAddress =
                                    Marshal.AllocHGlobal(Picture.numberOfBytes);

                              this.lstPicture.Items.Add("ptAddress: " +
                                    Picture.ptAddress.ToString());

                              API.TOCRGetJobResults(Picture.JobNo,
                                    ref Picture.numberOfBytes, Picture.ptAddress);
                        
                              TOCRResultsHeader hdr = (TOCRResultsHeader)Marshal.PtrToStructure
                                    (Picture.ptAddress,  typeof(TOCRResultsHeader));
                        
                              Picture.numItems =
                                    (Picture.numberOfBytes - Picture.ResultsHeaderSize) /
                                    Picture.ResultsItemSize;

                              lblStatus.Text = "Number of Items: " + Picture.numItems.ToString();
                              lstPicture.Items.Add("Number of Items: " +
                                    Picture.numItems.ToString());

                              Picture.ResultsItemAddress = (IntPtr)((int)Picture.ptAddress +
                                    Picture.ResultsHeaderSize);
                        
                              TOCRResultsItem item = new TOCRResultsItem();

                              for(int i = 0;
                                    i < Picture.numItems;
                                    i++ )
                              {
                                    Picture.AddressOfItem =
                                          (IntPtr)((int)Picture.ResultsItemAddress +
                                          i * Picture.ResultsItemSize);

                                    item =
                                          (TOCRResultsItem)Marshal.PtrToStructure
                                          (Picture.AddressOfItem,
                                          typeof(TOCRResultsItem));

                                    if( (char)item.OCRCha == '\r' )
                                    {
                                          Picture.strResults += "\r";
                                    }
                                    else
                                    {
                                          Picture.strResults += (char)item.OCRCha;
                                    }
                              }
                        }
                        #region Finalize and Clear Everything
                        rtfBox.Text += Picture.strResults;
                        Picture.strResults = "";
                        rtfBox.Text += "\n<-- End of File -->\n";
                        Marshal.FreeHGlobal(Picture.ptAddress);
                        API.TOCRShutdown(Picture.JobNo);
                        #endregion
                  }
            }
            #endregion

                  #region Methods for Performing the OCR
            private void OCRPageThreaded()
            {
                  OCRThread = new Thread(new ThreadStart(new BatchProcess
                        (ImageBatch,this.ImageControl,this.Picture,
                        this.menu,this.lstPicture,this.rtfBox).ExecuteBatch));
                  OCRThread.Start();
            }
            private void OCRSelectionThreaded()
            {
                  OCRThread = new Thread(new ThreadStart(new BatchProcess
                        (ImageBatch,this.ImageControl,this.Picture,
                        this.menu,this.lstPicture,this.rtfBox).ExecuteSelectionBatch));
                  OCRThread.Start();
            }
            #endregion

            #endregion

            #region Batch Commands

            private void BatchDialog()
            {
                  ImageBatch.Viewer = this.ImageControl;
                  BatchProcessor Batch = new BatchProcessor(ImageBatch);
                  Batch.ShowDialog();
                  
                  if(Batch.DialogResult != DialogResult.Cancel)
                  {
                        Int32 BatchLength = ImageBatch.Picture.Length - 1;

                        for(int i = 0; i <= BatchLength;i++)
                        {
                              API.TOCRInitialise(ref ImageBatch.Picture[i].JobNo);
                              ImageBatch.Picture[i].JobInfo.JobType =
                                    OCRuser.TOCRJOBTYPE_DIBFILE;
                              PerformOCR(ImageBatch.Picture[i]);
                        }
                  
                        Picture = new ImageInfo();

                        Picture.Filename =
                              ImageBatch.Picture[BatchLength].Filename;

                        Picture.Image =
                              ImageBatch.Picture[BatchLength].Image;

                        Picture.FileFormat =
                              ImageBatch.Picture[BatchLength].FileFormat;

                        Picture.Viewer =
                              ImageBatch.Picture[BatchLength].Viewer;
                  }
            }

            private void BatchSelection()
            {
                  ImageBatch.Viewer = this.ImageControl;
                  BatchProcessor Batch = new BatchProcessor(ImageBatch);
                  Batch.ShowDialog();
                  
                  if(Batch.DialogResult != DialogResult.Cancel)
                  {
                        Int32 BatchLength = ImageBatch.Picture.Length - 1;

                        for(int i = 0; i <= BatchLength;i++)
                        {
                              API.TOCRInitialise(ref ImageBatch.Picture[i].JobNo);

                              ImageBatch.Picture[i].Viewer = this.ImageControl;

                              Methods.HandleFile(ImageBatch.Picture[i]);      
                        
                              this.ImageControl.OCRSelection(ImageBatch.Picture[i]);
      
                              Methods.CreateSelectionMMFile(ImageBatch.Picture[i]);

                              ImageBatch.Picture[i].JobInfo.JobType =
                                    OCRuser.TOCRJOBTYPE_DIBFILE;

                              PerformOCR(ImageBatch.Picture[i]);
                        }
                  
                        Picture = new ImageInfo();

                        Picture.Filename =
                              ImageBatch.Picture[BatchLength].Filename;

                        Picture.Image =
                              ImageBatch.Picture[BatchLength].Image;

                        Picture.FileFormat =
                              ImageBatch.Picture[BatchLength].FileFormat;

                        Picture.Viewer =
                              ImageBatch.Picture[BatchLength].Viewer;
                  }
            }
            #endregion

            #region Image Methods

            private void OpenPicture()
            {
                  Picture.Filename =
                        this.ImageControl.OpenNewPicture();

                  Methods.HandleFile(Picture);

                  LabelItem lblFileType = menu.GetItem("lblFileType") as LabelItem;
                  lblFileType.Text = Picture.FileFormat;

                  lstPicture.Items.Add("Filename: " + Picture.Filename.Substring
                        (Picture.Filename.LastIndexOf(@"\")+1,
                        Picture.Filename.Length -
                        Picture.Filename.LastIndexOf(@"\")-1));

                  lstPicture.Items.Add("Format: " + Picture.FileFormat);
            }
            #endregion

            #region OCR Methods
            private void OCRArea()
            {
                  API.TOCRInitialise(ref Picture.JobNo);

                  this.ImageControl.OCRSelection(Picture);

                  lstPicture.Items.Add("Selection: " +
                        Picture.Selected.ToString());
                  Methods.CreateSelectionMMFile(Picture);

                  Picture.JobInfo.JobType = OCRuser.TOCRJOBTYPE_DIBFILE;
                  PerformOCR(Picture);
            }

            private void OCRPage()
            {
                  API.TOCRInitialise(ref Picture.JobNo);
                  Methods.CreateMMFile(Picture);

                  Picture.JobInfo.JobType = OCRuser.TOCRJOBTYPE_DIBFILE;
                  PerformOCR(Picture);
            }
            private void PerformOCR(ImageInfo Picture)
            {
                  LabelItem lblStatus = menu.GetItem("lblStatus") as LabelItem;
                  lblStatus.Text = "Performing Recognition";

                  Picture.JobStatus =
                        API.TOCRDoJob(Picture.JobNo,
                        ref Picture.JobInfo);
                        
                  if ( API.TOCRGetJobStatus(Picture.JobNo,ref Picture.JobStatus) == 0 )
                  {
                        do
                        {
                              lblStatus.Text = "Fetching Job Status";

                              API.TOCRGetJobStatus
                                    (Picture.JobNo,ref Picture.JobStatus);
                        }
                        while( Picture.JobStatus == 0);
                  }
                  else
                  {
                        lblStatus.Text = "Waiting For Job";

                        API.TOCRWaitForJob
                              (Picture.JobNo,ref Picture.JobStatus);
                  }
                  if ( Picture.JobStatus == OCRuser.TOCRJOBSTATUS_DONE )
                  {
                        lblStatus.Text = "Job Done.Allocating Results";

                        Picture.ResultsHeaderSize =
                              Marshal.SizeOf(typeof(TOCRResultsHeader));

                        lblStatus.Text = "Header Size: " +
                              Picture.ResultsHeaderSize.ToString();

                        lstPicture.Items.Add("Header Size: " +
                              Picture.ResultsHeaderSize.ToString());

                        Picture.ResultsItemSize =
                              Marshal.SizeOf(typeof(TOCRResultsItem));

                        lblStatus.Text = "Item Size: " +
                              Picture.ResultsItemSize.ToString();

                        lstPicture.Items.Add("Item Size: " +
                              Picture.ResultsItemSize.ToString());

                        API.TOCRGetJobResults(Picture.JobNo,
                              ref Picture.numberOfBytes, IntPtr.Zero);

                        Picture.ptAddress = IntPtr.Zero;
                        Picture.ptAddress =
                              Marshal.AllocHGlobal(Picture.numberOfBytes);

                        this.lstPicture.Items.Add("ptAddress: " +
                              Picture.ptAddress.ToString());

                        API.TOCRGetJobResults(Picture.JobNo,
                              ref Picture.numberOfBytes, Picture.ptAddress);
                        
                        TOCRResultsHeader hdr = (TOCRResultsHeader)Marshal.PtrToStructure
                              (Picture.ptAddress,  typeof(TOCRResultsHeader));
                        
                        Picture.numItems =
                              (Picture.numberOfBytes - Picture.ResultsHeaderSize) /
                              Picture.ResultsItemSize;

                        lblStatus.Text = "Number of Items: " + Picture.numItems.ToString();
                        lstPicture.Items.Add("Number of Items: " +
                              Picture.numItems.ToString());

                        Picture.ResultsItemAddress = (IntPtr)((int)Picture.ptAddress +
                              Picture.ResultsHeaderSize);
                        
                        TOCRResultsItem item = new TOCRResultsItem();

                        for(int i = 0;
                              i < Picture.numItems;
                              i++ )
                        {
                              Picture.AddressOfItem =
                                    (IntPtr)((int)Picture.ResultsItemAddress +
                                    i * Picture.ResultsItemSize);

                              item =
                                    (TOCRResultsItem)Marshal.PtrToStructure
                                    (Picture.AddressOfItem,
                                    typeof(TOCRResultsItem));

                              if( (char)item.OCRCha == '\r' )
                              {
                                    Picture.strResults += "\r";
                              }
                              else
                              {
                                    Picture.strResults += (char)item.OCRCha;
                              }
                        }
                  }
                  rtfBox.Text += Picture.strResults;
                  Picture.strResults = "";
                  rtfBox.Text += "\n<-- End of File -->\n";
                  Marshal.FreeHGlobal(Picture.ptAddress);
                  API.TOCRShutdown(Picture.JobNo);
            }

            #endregion

            #region Event Handlers

            private void Viewer_Load(object sender, System.EventArgs e)
            {
                  this.ImageControl.Width = this.Width - 250;
                  this.rtfBox.Height = this.Height - 300;

                  Int32 PNo;

                  if(API.TOCRSetErrorMode(OCRuser.TOCRDEFERRORMODE,
                        OCRuser.TOCRERRORMODE_MSGBOX) != OCRerrs.TOCR_OK)
                        this.Close();

                  this.Text = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;

                  for(PNo = mIP;PNo < mRP;PNo++)
                  {
                        mPane[PNo].hDCMem = GDI.CreateCompatibleDC((int)this.Handle);
                        mPane[PNo].BI.hBmp = 0;
                        mPane[PNo].AllowPaint = true;
                        mPane[PNo].Zoom = 100;
                        mPane[PNo].zLeft = 0;
                        mPane[PNo].zTop = 0;
                        mPane[PNo].zWidth = 0;
                        mPane[PNo].zHeight = 0;
                  }
            
                  Picture.JobInfo.StructId = 0;

                  this.Update();
            }

            private void Viewer_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
            {
                  this.ImageControl.Width = this.Width - 250;
                  this.rtfBox.Height = this.Height - 300;
            }

            private void Viewer_Resize(object sender, System.EventArgs e)
            {
                  this.ImageControl.Width = this.Width - 250;
                  this.rtfBox.Height = this.Height - 300;
            }

            private void Viewer_Layout(object sender, System.Windows.Forms.LayoutEventArgs e)
            {
                  this.ImageControl.Width = this.Width - 250;
                  this.rtfBox.Height = this.Height - 300;
            }

            public struct BMPINFO
            {
                  public int hBmp;
                  public int Width;
                  public int Height;
                  public int XPelsPerMeter;
                  public int YPelsPerMeter;
                  public int Length;
            }
            public struct PANEINFO
            {
                  public BMPINFO BI;
                  public Int32 hDCMem;
                  public Int32 Zoom;
                  public Int32 zLeft;
                  public Int32 zTop;
                  public Int32 zWidth;
                  public Int32 zHeight;
                  public bool AllowPaint;
            }
            #endregion

      }// Viewer Class
}
0
 
LVL 18

Expert Comment

by:testn
ID: 9904357
Can you post the actual error you got?
0
 
LVL 2

Author Comment

by:derekthornton
ID: 9904399
An unhandled exception of type 'System.InvalidOperationException' occurred in system.windows.forms.dll

Additional information: The object is currently in use elsewhere.

The cursor is placed at the last curly bracket of the
Public Static Void Main()

method.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 3

Expert Comment

by:magnus23
ID: 9905896
Hi derekthornton !
If i undestand this problem correctly, than you have a synchronization problem on your hands.
You accessing some code inside one thread from another thread wile process is still working. To solve this i suggest that you lock part of code that is crytical and must be safe, like this:


               public void ExecuteBatch()
               {
                  lock{
                         BatchDialog();
                        }
               }
               public void ExecuteSelectionBatch()
               {
                  lock{
                        BatchSelection();
                        }
               }
In this case, code is thread safe and it is impossible for two different threads to use this code in the same time.

Hope this will help.
Good Luck!
0
 
LVL 3

Accepted Solution

by:
magnus23 earned 400 total points
ID: 9905906
Small  correction :


               public void ExecuteBatch()
               {
                  lock(this)
                       {
                         BatchDialog();
                        }
               }
               public void ExecuteSelectionBatch()
               {
                  lock(this)
                       {
                        BatchSelection();
                        }
               }
0
 
LVL 2

Author Comment

by:derekthornton
ID: 9905986
I understand what you're saying, but the above didn't help. So I don't think it was the entire problem. Any other suggestions?
0
 
LVL 11

Assisted Solution

by:Agarici
Agarici earned 100 total points
ID: 9913481
i think (it is just an idea) the problem is with the Image control you use... this is an UI element and you cant just use it in any thread (all interaction with the UI should be done using Invoke)
try disabling the control update during the time your thread works...

A.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
About delegates in c sharp 3 47
Reset asp.net login password 4 42
Question! 4 36
How to parse the string and split the sub directory names in asp.net 7 40
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question