Results 1 to 3 of 3

Thread: background worker doesn't seem to help

  1. #1

    Thread Starter
    Hyperactive Member r0k3t's Avatar
    Join Date
    Dec 2005
    Location
    Cleveland
    Posts
    361

    background worker doesn't seem to help

    Hi there,

    I have an application that processes a long html file and breaks it into parts. in the process of this it has to create an entry in a database for every part and do some other stuff, blah blah... the point is that it takes about a minute and a half all said and done - the program looks like it isn't responding and the call I make just before I start the processing to disable the button doesn't always seem to work.

    The point of all this is that I looked into a background worker to solve the problem. I think I implemented it right but it doesn't seem to be doing the job - it seems to act just like it did before - i.e button doesn't disable, the label with the current line number doesn't update... I am wondering if I did something wrong. this is how I implemented it.

    I create the background worker and instead of calling the function from the button I call backgroundWorker.RunWorkerAsync() from the button. my do work function and the others (there is no cancel) look like this.

    Code:
     private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
            {
                
                htmlFileCreater();
            }
    
            private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                lblFileToLoad.Text = "File upload complete";
            }
    
            private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                lblProgress.Text = e.ProgressPercentage.ToString();
                //progressBar.Value = e.ProgressPercentage;
            }
    What am I missing? The interface still remains unresponsive and doesn't updatet...

    OK - Thanks!

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: background worker doesn't seem to help

    Could you show the htmlFileCreater subroutine?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Hyperactive Member r0k3t's Avatar
    Join Date
    Dec 2005
    Location
    Cleveland
    Posts
    361

    Re: background worker doesn't seem to help

    Hi there,

    As you can see this is a fairly long method - it also calls several methods - basically I am taking a long piece of research, and creating sections, and then for each section I am creating several sub-sections or records as we call them around the shop.

    You are probally thinking to yourself - wouldn't this be better if it where in XML - heck yeah it would but this is what I have to work with

    Thanks!

    c# Code:
    1. //Called by btnSelectFileToLoad_Click
    2.         //splits up the filename from C:\folder\folder\2134.htm into several parts
    3.         //filename = 2134.htm
    4.         //filename without the ext. = 2134
    5.         //creates a directory on the server and copies all the images into it
    6.         //Opens an output file for writing of the entire study in html format
    7.         //opens and arraylist which html is loaded into
    8.         //transfers arraylist to string array
    9.         //searchs through string array to find <h1> tag when found calls writeH1file with args
    10.         //          the string array
    11.         //          the start position
    12.         //          the ending position
    13.         //          the current section
    14.         //          the current line as a string
    15.         //          the name of the file minus the ext
    16.         //when finished closes the fileStream for the full study
    17.         private void htmlFileCreater()
    18.         {
    19.             FileStream strm = new FileStream(fileName, FileMode.Open, FileAccess.Read);
    20.             //Define the regular expressions
    21.             Regex h1_open = new Regex("<h1>");
    22.             Regex h2_open = new Regex("<h2>");
    23.             Regex table_open = new Regex("<table");
    24.             Regex h3_open = new Regex("<h3>");
    25.             Regex close_body = new Regex("</body>");
    26.             //Define the matches
    27.             Match h1Open = h1_open.Match("NOMATCH");
    28.             Match h2Open = h2_open.Match("NOMATCH");
    29.             Match h3Open = h3_open.Match("NOMATCH");
    30.             Match tableOpen = table_open.Match("NOMATCH");
    31.             Match closeBody = close_body.Match("NOMATCH");
    32.             //Take the file stream and create a stream reader
    33.             StreamReader rdr = new StreamReader(strm);
    34.             //Set up a couple of string variables to hold some information we want
    35.             string currentLine;
    36.             string fileNameMinusDirInfo;
    37.             string fileNameMinusExtension;
    38.             //Split up the filename we passed in which is actually the filename as returned from the open file
    39.             //dialog (which also contians the path) - we only wnat the last part, the real filename
    40.             //and not the part containing any directory information
    41.             string[] currentFileInfo = fileName.Split(new char[] { '\\' });
    42.             //Stick that information into a string so it is easier to know what it is
    43.             // fileNameMinusDirInfo - lots esier than (currentFileInfo[currentFileInfo.Length - 1]) :)
    44.             fileNameMinusDirInfo = currentFileInfo[currentFileInfo.Length - 1];
    45.             //now from that file name get the part that doesn't have the extenxtion
    46.             string[] filenameParts = fileNameMinusDirInfo.Split(new char[] { '.' });
    47.             fileNameMinusExtension = filenameParts[0];
    48.             //open the output file using the fileNameMinusDirInfo variable we got
    49.             FileStream strmOut = new FileStream("W:\\" + fileNameMinusExtension.Substring(0,2) + "xx\\" + fileNameMinusDirInfo, FileMode.Create);
    50.             //Now open the output stream for writing
    51.             StreamWriter fullStudyFileWriter = new StreamWriter(strmOut);
    52.             //we are going to load the file into and arraylist and then after that transfer it
    53.             //to a standard string[] array
    54.             ArrayList htmlFileArrayList = new ArrayList();
    55.             //we don't declare the array till later cause we need to get the size AFTER we load the
    56.             //the arrayList.
    57.        
    58.             int currentPosition = 0;
    59.             int h1startPosition = 0;
    60.             int h1endPosition = 0;
    61.             int sectionCounter = 0;
    62.            
    63.             //before anything else try and create the directory and copy over the gif's for the charts
    64.             //- if that fails bail out...
    65.             try{
    66.                 Directory.CreateDirectory("W:\\" + fileNameMinusExtension.Substring(0, 2) + "xx\\" + fileNameMinusExtension);
    67.                
    68.                 string dirString = "G:\\tfgfile\\INDSTUD\\" + fileNameMinusExtension.Substring(0, 2) + "xx\\" + fileNameMinusExtension;
    69.                 //MessageBox.Show(dirString);
    70.                 Directory.SetCurrentDirectory(dirString);
    71.                 //MessageBox.Show(Directory.GetCurrentDirectory());
    72.                 DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory());
    73.  
    74.                 foreach (FileInfo file in dir.GetFiles())
    75.                 {
    76.                     //MessageBox.Show(file.Name);
    77.                     if (file.Extension.EndsWith("gif") || file.Extension.EndsWith("jpg"))
    78.                     {
    79.                         string inFileName = dirString + "\\" + file.Name;
    80.                         string outFileName = "W:\\" + fileNameMinusExtension.Substring(0, 2) + "xx\\" + fileNameMinusExtension + "\\" + file.Name;
    81.                         int i;
    82.                         FileStream fin;
    83.                         FileStream fout;
    84.                         fin = new FileStream(inFileName, FileMode.Open);
    85.                         fout = new FileStream(outFileName, FileMode.Create);
    86.                         try
    87.                         {
    88.                             do
    89.                             {
    90.                                 i = fin.ReadByte();
    91.                                 if (i != -1) fout.WriteByte((byte)i);
    92.                             } while (i != -1);
    93.                         }
    94.                         catch (IOException exc)
    95.                         {
    96.                             Console.WriteLine(exc.Message + "File Error");
    97.                         }
    98.  
    99.                         fin.Close();
    100.                         fout.Close();
    101.                     }
    102.                 }
    103.                 //return;
    104.             }
    105.             catch(Exception ex)
    106.             {
    107.                 MessageBox.Show("I couldn't create the directory or copy all of the images: " + fileNameMinusExtension + "\nCheck your 'W' drive and try agian.\nExiting\n" + ex.Message);
    108.                 return;
    109.  
    110.             }
    111.  
    112.             //stuff the file into an array so we can process it.
    113.             while (!rdr.EndOfStream)
    114.             {
    115.                 currentLine = rdr.ReadLine();
    116.                 fullStudyFileWriter.WriteLine(currentLine);
    117.                 htmlFileArrayList.Add(currentLine);
    118.                
    119.             }
    120.             string[] data = new string[htmlFileArrayList.Count];
    121.             htmlFileArrayList.CopyTo(data);
    122.  
    123.             for (int i = 0; i < data.Length; i++)
    124.             {
    125.                 currentLine = data[i];
    126.                 h1Open = h1_open.Match(currentLine);
    127.                 closeBody = close_body.Match(currentLine);
    128.                 if (h1Open.Success || closeBody.Success)
    129.                 {
    130.                    
    131.                     //MessageBox.Show(currentLine);
    132.                     h1startPosition = h1endPosition;
    133.                     h1endPosition = i;
    134.                     if (h1startPosition != 0)
    135.                     {
    136.                         //get the title of the section and determin the number.
    137.                         //in the future the sectionNumberDecider will decide ALL the section numbers
    138.                         //but for most of them they are still undefined so we just move sequentially
    139.                         //unless it need to be a 19 or 20
    140.                         if (sectionNumberDecider(data[h1startPosition]) == 19)
    141.                         {
    142.                             sectionCounter = 19;
    143.                         }
    144.                         if (sectionNumberDecider(data[h1startPosition]) == 20)
    145.                         {
    146.                             sectionCounter = 20;
    147.                         }
    148.                         //MessageBox.Show("Section: " + sectionCounter + " Start: " + h1startPosition + " End: " + h1endPosition + " " + currentLine);
    149.                         writeH1file(data, h1startPosition, h1endPosition, sectionCounter, data[h1startPosition], fileNameMinusExtension);
    150.                         sectionCounter++;
    151.                     }
    152.                 }
    153.                 //The worker is commented out for the moment as it wasn't working!
    154.                 //worker.ReportProgress(currentPosition);
    155.                 currentPosition++;
    156.                
    157.             }
    158.             strm.Close();
    159.             strmOut.Close();
    160.            
    161.         } //end of htmlFileCreator

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width