Threading Problem
-
Dear sirs: I wrote program that performs one resource-intensive operation. This operation is to convert PDF into a text file. I decided to spin-off this single operation on a threading delegate as shown below: Class Pdf() { public delegate string PdfIsbn(); string ISearch.Isbn( string sFileName ) { m_sFileName = sFileName; PdfIsbn pdfIsbn = new PdfIsbn( GetIsbn ); IAsyncResult iasResult = pdfIsbn.BeginInvoke( new AsyncCallback( IsbnComplete ) , "Isbn is complete" ); /********************************** ******** Regular Processing ******* **********************************/ // It only works if this code is un-commented. while (!iasResult.IsCompleted) { Console.WriteLine("Please wait..."); //return m_sIsbn; } return m_sIsbn; } private void IsbnComplete( IAsyncResult iasResult ) { AsyncResult ar = ( AsyncResult )iasResult; PdfIsbn pdfIsbn = ( PdfIsbn )ar.AsyncDelegate; m_sIsbn = pdfIsbn.EndInvoke( iasResult ); Constant.g_iThreadCount -= 1; } string GetIsbn() { Constant.g_iThreadCount += 1; // Increment thread-counter. Console.WriteLine( "Pdf version of search implemented" ); Thread.Sleep(5000); dtSearch dtSrch = new dtSearch( m_sFileName ); // Pass-in filename only. regEx = new RegEx( "Isbn" ); // Search for a regular expression that fits the "ISBN"-type. SearchIsbn( regEx , dtSrch.ResultText ); return m_sIsbn = regEx.RegExResult; // Get the ISBN. } } The Main program is a windows form with a DataGridView. The main loop cycles through subfolders; each one containing a PDF. do { try { file.GetDirectory(); // gets a book inside a directory, performs search etc. // If a valid ISBN is found it will show up here. if (file.SearchItem != "") { newBooks.AddBook(new Book(true, file.FileName, file.SearchItem)); } } catch (Exception ex) { file.LogError(ex, ex.Message.ToSt
-
Dear sirs: I wrote program that performs one resource-intensive operation. This operation is to convert PDF into a text file. I decided to spin-off this single operation on a threading delegate as shown below: Class Pdf() { public delegate string PdfIsbn(); string ISearch.Isbn( string sFileName ) { m_sFileName = sFileName; PdfIsbn pdfIsbn = new PdfIsbn( GetIsbn ); IAsyncResult iasResult = pdfIsbn.BeginInvoke( new AsyncCallback( IsbnComplete ) , "Isbn is complete" ); /********************************** ******** Regular Processing ******* **********************************/ // It only works if this code is un-commented. while (!iasResult.IsCompleted) { Console.WriteLine("Please wait..."); //return m_sIsbn; } return m_sIsbn; } private void IsbnComplete( IAsyncResult iasResult ) { AsyncResult ar = ( AsyncResult )iasResult; PdfIsbn pdfIsbn = ( PdfIsbn )ar.AsyncDelegate; m_sIsbn = pdfIsbn.EndInvoke( iasResult ); Constant.g_iThreadCount -= 1; } string GetIsbn() { Constant.g_iThreadCount += 1; // Increment thread-counter. Console.WriteLine( "Pdf version of search implemented" ); Thread.Sleep(5000); dtSearch dtSrch = new dtSearch( m_sFileName ); // Pass-in filename only. regEx = new RegEx( "Isbn" ); // Search for a regular expression that fits the "ISBN"-type. SearchIsbn( regEx , dtSrch.ResultText ); return m_sIsbn = regEx.RegExResult; // Get the ISBN. } } The Main program is a windows form with a DataGridView. The main loop cycles through subfolders; each one containing a PDF. do { try { file.GetDirectory(); // gets a book inside a directory, performs search etc. // If a valid ISBN is found it will show up here. if (file.SearchItem != "") { newBooks.AddBook(new Book(true, file.FileName, file.SearchItem)); } } catch (Exception ex) { file.LogError(ex, ex.Message.ToSt
Maybe I am missing something here, but it seems like you should be updating your grid when the async event comes back: private void IsbnComplete( IAsyncResult iasResult ) { Then you need to do something like Application.DoEvents for your grid to repaint. Hope that helps. Ben
-
Maybe I am missing something here, but it seems like you should be updating your grid when the async event comes back: private void IsbnComplete( IAsyncResult iasResult ) { Then you need to do something like Application.DoEvents for your grid to repaint. Hope that helps. Ben
Thanks, I finally got it to work per your advice. -- modified at 15:19 Thursday 24th May, 2007