Search code examples
c#thread-safetysingle-threaded

loop abruptly stops when converting rtf to html via thread


I am asking this question after 4 days of debugging so please bear with me. :)

I have a list (commentlist) of 413 records

logger.Debug("For each comment convert from rtf to Html: " + commentsList.Count().ToString());

int c = 1;
            foreach (var r in commentsList)
            {

                try
                {                        
                    r.NotesLong = ConvertRtfToHtml(r.NotesLong);
                    logger.Debug("Done");
                }
                catch (Exception e)
                {
                    logger.Error(e.Message);
                }
                finally {
                    logger.Debug(c.ToString() + " SID: " + r.StudentID.ToString() + " RID: " +r.ResultID.ToString() + " RTID: " + r.ResultTypeID.ToString());
                    c++;
                }
                
                if (r.Color == null)
                    r.Color = "FFFFFF";

            }

            logger.Debug("Completed notes long conversion");

First time when i run it returns all of the records. Second time however, it is stuck at record 303 (no matter how many times i try it is stopped at the same record). There is no exception but the loop stops abruptly [The record 303 does not have a buggy data]..It could be a thread issue but then the records should be different each time?

ConvertRtfToHtml

 public string ConvertRtfToHtml(string rtfText)
    {

        try
        {
            var thread = new Thread(ConvertRtfInSTAThread);
            logger.Debug("ConvertRtfInSTAThread");
            var threadData = new ConvertRtfThreadData { RtfText = rtfText };
            logger.Debug("ConvertRtfThreadData");
            thread.SetApartmentState(ApartmentState.STA);
            logger.Debug("SetApartmentState");
            thread.Start(threadData);
            thread.Join();
            logger.Debug("return html text");
            return threadData.HtmlText;
            
        }
        catch (Exception e){
            logger.Error("ConvertRtfToHtml: " + e.Message);
            return "Error";
        }

    }

In the above code some issue arises in the following 2 lines

            thread.Start(threadData);
            thread.Join();

Logs are as follows

.........

2017-08-03 12:14:38,952 [8] DEBUG - Done

2017-08-03 12:14:38,952 [8] DEBUG - 302 SID: 164 RID: 44380 RTID: 48

2017-08-03 12:14:38,952 [8] DEBUG - ConvertRtfInSTAThread

2017-08-03 12:14:38,952 [8] DEBUG -ConvertRtfThreadData

2017-08-03 12:14:38,952 [8] DEBUG -SetApartmentState

2017-08-03 12:14:38,956 [8] DEBUG - return html text

2017-08-03 12:14:38,956 [8] DEBUG - Done

2017-08-03 12:14:38,956 [8] DEBUG - 303 SID: 166 RID: 44381 RTID: 14

2017-08-03 12:14:38,956 [8] DEBUG - ConvertRtfInSTAThread

2017-08-03 12:14:38,956 [8] DEBUG - ConvertRtfThreadData

2017-08-03 12:14:38,956 [8] DEBUG -SetApartmentState

update

The issue occurs at join. so i changed the function as follows

public string ConvertRtfToHtml(string rtfText)
    {

        try
        {
            var thread = new Thread(ConvertRtfInSTAThread);                
            var threadData = new ConvertRtfThreadData { RtfText = rtfText };
            thread.SetApartmentState(ApartmentState.STA);
            logger.Debug("SetApartmentState");
            thread.Start(threadData);
            try
            {
                thread.Join();
            }
            catch(ThreadStateException e){
                logger.Error("ThreadStateException " + e.Message);
            }
            catch (ThreadInterruptedException e) {
                logger.Error("ThreadInterruptedException " + e.Message);
            }

            logger.Debug("return html text");
            return threadData.HtmlText;
            
        }
        catch (Exception e){
            logger.Error("ConvertRtfToHtml: " + e.Message);
            return "Error";
        }

    }

Still no exception is logged, however the main Action method is started once again...How is that happening?


Solution

  • Okay!! I added a try/catch to ConvertRtfInSTAThread function and found that it starts throwing exceptions for all items after element 302 i.e. at 303 and onwards ...

    exceptions:

    303: The operation completed successfully

    304: Not enough storage is available to process this command

    305: The operation completed successfully

    306: The operation completed successfully

    .......

    314: The operation completed successfully

    Now there is no hanging of application but what kind of exception is this (operation completed successfully)? Can i remove the storage problem?