Parth Bhatt
Parth Bhatt

Reputation: 19479

How to optimize the performance of a thread running in background in ASP.NET?

In my website, I am using thread to perform a repetitive process in background. Now the process takes around 30 to 45 seconds to update the database.

Basically the function performs the following:

1) Download the CSV from a website.

2) Parse the CSV and update the database tables.

I want to optimize the performance and decrease the time it takes to 15 seconds.

How can I do this?

EDIT:

Here is the Code:

        // MAIN FUNCTION IN THE THREAD
        private static void downloadAndParse()
        {
            NewHive.MyServ newServe = new NewHive.MyServ();
            NewHive.CsvDownload newService = new NewHive.CsvDownload();
            //NewHive.MyServ newServe = new NewHive.MyServ();
            string downloadSuccess = newService.CsvDownloader();
            if (downloadSuccess == "Success")
            {
                string parseSuccess = newService.CsvParser();

            }
            newServe.updateOthersInPosition();
        }







     //CSV DOWNLOAD FUNCTION
    public string CsvDownloader()
    {
        Byte[] inBuf = null;
       // HttpWebRequest wr = Convert.ChangeType(WebRequestFactory.Create("http://finance.yahoo.com/d/quotes.csv?s=RHT+MSFT&f=sb2b3jk"),HttpWebRequest);
       // HttpWebResponse ws = Convert.ChangeType(wr.GetResponse(),HttpWebResponse);

        HttpWebRequest wr = (HttpWebRequest)WebRequest.Create("http://download.finance.yahoo.com/d/quotes.csv?s=INDU+INDU+^N225+^GSPC+^GDAXI+^FCHI+^HSI+^IXIC+^STOXX50E+^FTSE&f=l1bd14na");
        HttpWebResponse ws = (HttpWebResponse)wr.GetResponse();
        Stream str = ws.GetResponseStream();
        inBuf = new Byte[100000000];
        int bytesToRead = (int)inBuf.Length;

        int bytesRead=0;
        while(bytesToRead>0)
        {
            int n = str.Read(inBuf,bytesRead,bytesToRead);
            if(n==0)
            {
                break;
            }
            bytesRead += n;
            bytesToRead -= n;
        }
        FileStream fstr = new FileStream("D:\\Hosting\\7312812\\html\\News.csv", FileMode.Create, FileAccess.Write);


        //  FileStream fstr = new FileStream("News.csv", FileMode.Create, FileAccess.Write);
       // FileStream fstr = new FileStream("C:\\VSS Working Folder\\27 Jan 11 NewHive\\NewHive\\CSV\\new.csv", FileMode.Create, FileAccess.Write);
        fstr.Write(inBuf,0,bytesRead);
        str.Close();
        fstr.Close();

        return "Success";
    }







     //CSV PARSER FUNCTION
   public string CsvParser()
    {
       int _nNrRowsProccessed = 0;

       string connectionString = @"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + ConfigurationManager.AppSettings["CSVFolder"] + ";";

    OdbcConnection conn = new OdbcConnection(connectionString);

    try
    {
        conn.Open();

        string strFileName = ConfigurationManager.AppSettings["CSVFile"];
        string strSQL = "Select * from " + strFileName;

        OdbcCommand cmd = new OdbcCommand();
        cmd.Connection = conn;
        cmd.CommandText = strSQL;
        cmd.CommandType = CommandType.Text;

        OdbcDataReader reader = cmd.ExecuteReader();

        NewHive.MyServ newService = new NewHive.MyServ();
       // MasterCalendar_DB.OpenMySQLConnection();

        while (reader.Read())
        {
            decimal LastTradePrice;
            decimal Bid;
            string MarketOpen;
            string IndexCode;
            string Index;
            decimal Offer;
            decimal LTPDatabase=0.1M;

                IndexCode = reader[3].ToString();
                String addSQL = "Select LastTradePrice from `jsontest`.`tbl_MarketData` where IndexCode = '" + IndexCode + "'";
                MySqlConnection objMyCon = new MySqlConnection(strProvider);
                objMyCon.Open();
                MySqlCommand command = objMyCon.CreateCommand();

                command.CommandText = addSQL;
                MySqlDataReader result = command.ExecuteReader();
                //int j = command.ExecuteNonQuery();
                while (result.Read())
                {
                    LTPDatabase = Convert.ToDecimal(result[0]);
                  //  LTPDatabase = Math.Round(LTPTemp, 2);
                }
                objMyCon.Close();
                decimal LTPTemp =  Convert.ToDecimal(reader[0].ToString());
                LastTradePrice = Math.Round(LTPTemp, 2);
                if (reader[1].ToString() != "N/A")
                {
                    Bid = Convert.ToDecimal(reader[1].ToString());
                }
                else
                {
                    Bid = 10.0M;
                }
                if (LastTradePrice != LTPDatabase)
                {
                    MarketOpen = "Open";
                }
                else
                {
                    MarketOpen = "Close";
                }
                Index = reader[4].ToString();

                if (reader[5].ToString() != "N/A")
                {
                    Offer = Convert.ToDecimal(reader[5].ToString());
                }
                else
                {
                    Offer = 20.0M;
                }
            //}
           // string[] arLine = strLine.Split(';');

           // string strAgencyPropertyID = arLine[0];
           // DateTime dt = DateTime.Parse(arLine[1]);
          //  Int64 nDate = (Int64)Util.ConvertToUnixTimestamp(dt);
           // String strAvailability = (arLine[2]);

            _nNrRowsProccessed++;

            newService.CSVInsert(IndexCode,Index,MarketOpen,Bid,Offer,LastTradePrice);
          //  MasterCalendar_DB.Insert(strAgencyPropertyID, nDate, strAvailability);
        }           

    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        conn.Close();
       // MasterCalendar_DB.CloseMySQLConnection();
    }
    return "Success";
    }
}

Upvotes: 1

Views: 399

Answers (1)

John Saunders
John Saunders

Reputation: 161831

You have many things wrong with your code:

  • You have all sorts of objects which implement IDisposable, and you're not placing them into using blocks. For instance,

    OdbcDataReader reader = cmd.ExecuteReader();

should be

using (OdbcDataReader reader = cmd.ExecuteReader())
{
    // ...
}
  • Do not catch exceptions that you cannot handle.

Upvotes: 3

Related Questions