Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Threading Problem

Threading Problem

Scheduled Pinned Locked Moved .NET (Core and Framework)
regexlearninghelpannouncement
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hyland Computer Systems
    wrote on last edited by
    #1

    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

    K 1 Reply Last reply
    0
    • H Hyland Computer Systems

      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

      K Offline
      K Offline
      kubben
      wrote on last edited by
      #2

      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

      H 1 Reply Last reply
      0
      • K kubben

        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

        H Offline
        H Offline
        Hyland Computer Systems
        wrote on last edited by
        #3

        Thanks, I finally got it to work per your advice. -- modified at 15:19 Thursday 24th May, 2007

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups