public static bool CreateRequestFileBasedOnStatus()
        {
            bool bRtnVal = false;

            try
            {
                if (File.Exists(strStatusFile))
                {
                    StockMarketRunnerStatus obj = SerializationHelper.LoadXML <StockMarketRunnerStatus>(strStatusFile);
                    DateTime dtLastWritten      = File.GetLastWriteTime(rawRequestFilePath);
                    if (!dtLastWritten.Equals(obj.LastModifiedRawFile))
                    {
                        bRtnVal = true;
                    }
                }
                else
                {
                    bRtnVal = true;
                }
            }
            catch (Exception ex)
            {
                ApplicationLog.Instance.WriteException(ex);
            }
            return(bRtnVal);
        }
        public static bool StockRunnerCheck()
        {
            bool bRtnVal = true;

            try
            {
                StockMarketRunnerStatus obj = new StockMarketRunnerStatus();
                if (File.Exists(strStatusFile))
                {
                    obj = SerializationHelper.LoadXML <StockMarketRunnerStatus>(strStatusFile);
                    if (
                        ((DateTime.Today.DayOfWeek.Equals(DayOfWeek.Monday) && DateTime.Now.TimeOfDay.Hours < 16) || //Don't run on Monday before 4pm if ran on Friday after 4 pm..
                         (DateTime.Today.DayOfWeek.Equals(DayOfWeek.Saturday) || DateTime.Today.DayOfWeek.Equals(DayOfWeek.Sunday)) &&
                         (obj.LastRanDay.Equals(DayOfWeek.Friday) && obj.LastRan.TimeOfDay.Hours > 16)) || // Don't run on Saturday or Sunday if ran on Friday after 4 pm.
                        ((obj.LastRan.TimeOfDay.Hours > 16 && DateTime.Now.TimeOfDay.Hours < 16) && //Don't run if last run is yestarday after 4 pm and current time is less than 4pm.
                         (obj.LastRanDay.Equals(DateTime.Now.AddDays(-1).DayOfWeek))) ||
                        (obj.LastRan.AddHours(12) > DateTime.Now)    //Don't run if ran less than 12 hrs.
                        )
                    {
                        bRtnVal = false;
                    }
                }
                else
                {
                    ApplicationLog.Instance.WriteWarning(String.Format("Status file not found at '{0}'", strStatusFile));
                }
            }
            catch (Exception ex)
            {
                ApplicationLog.Instance.WriteException(ex);
            }
            return(bRtnVal);
        }
 public static void WriteStatus()
 {
     try
     {
         StockMarketRunnerStatus obj = new StockMarketRunnerStatus();
         obj.LastRan    = DateTime.Now;
         obj.LastRanDay = DateTime.Now.DayOfWeek;
         obj.LastModifiedRequestFile = File.GetLastWriteTime(requestFilePath);
         obj.LastModifiedRawFile     = File.GetLastWriteTime(rawRequestFilePath);
         SerializationHelper.WriteXML <StockMarketRunnerStatus>(obj, strStatusFile);
         ApplicationLog.Instance.WriteInfo(String.Format("Staus file modified at '{0}'", DateTime.Now));
     }
     catch (Exception ex)
     {
         ApplicationLog.Instance.WriteException(ex);
     }
 }
        protected override void OnStart(string[] args)
        {
            try
            {
                ApplicationLog.Instance.WriteInfo(String.Format("{0} started at '{1}'", GetType().Name, DateTime.Now));
                if (StockMarketRunnerStatus.StockRunnerCheck())
                {
                    StockMarketRunner runApp = new StockMarketRunner();
                    bool bRunStatus          = runApp.RunApplication();
                    if (bRunStatus)
                    {
                        ApplicationLog.Instance.WriteInfo(String.Format("{0} successfully completed at '{1}'", GetType().Name, DateTime.Now));
                    }
                    else
                    {
                        ApplicationLog.Instance.WriteWarning(String.Format("{0} unsuccessfully completed at '{1}'", GetType().Name, DateTime.Now));
                    }
                    StockMarketRunnerStatus.WriteStatus();
                }
                else
                {
                    ApplicationLog.Instance.WriteInfo(String.Format("{0} not ran at '{1}'", GetType().Name, DateTime.Now));
                }

                ProjectInstaller projectInstaller = new ProjectInstaller();

                ServiceController serviceController = new ServiceController(projectInstaller.ServiceName);
                if (serviceController.Status.Equals(ServiceControllerStatus.Running) ||
                    serviceController.Status.Equals(ServiceControllerStatus.StartPending))
                {
                    ApplicationLog.Instance.WriteInfo(String.Format("Stopping the service automatically at '{0}'", DateTime.Now));
                    this.Stop();
                }
            }
            catch (Exception ex)
            {
                ApplicationLog.Instance.WriteException(ex);
            }
        }
        public bool RunApplication()
        {
            bool bRtnVal = false;

            try
            {
                if (!File.Exists(requestFilePath) || StockMarketRunnerStatus.CreateRequestFileBasedOnStatus())
                {
                    CreateRequestFile();
                }

                StockRequestInfos stockInfosFromFile = SerializationHelper.LoadXML <StockRequestInfos>(requestFilePath);
                if (stockInfosFromFile != null)
                {
                    List <StockRequestInfo> listOfStockInfos = stockInfosFromFile.ListOfStockRequests;
                    List <MarketValueInfo>  lstMarketValues  = new List <MarketValueInfo>();
                    foreach (StockRequestInfo stockRequestInfo in listOfStockInfos)
                    {
                        MarketValueInfo marketValue = GetTickerInfo(stockRequestInfo);
                        if (marketValue != null)
                        {
                            lstMarketValues.Add(marketValue);
                        }
                    }
                    if (lstMarketValues != null && lstMarketValues.Count > 0)
                    {
                        CreateExcel(new MarketValueInfo());
                        UpdateExcel(lstMarketValues);
                        List <EmailUtil> lst = new List <EmailUtil>()
                        {
                            new EmailUtil()
                            {
                                ToEmail = "*****@*****.**", ToName = "Sunita Gaikwad"
                            },
                            new EmailUtil()
                            {
                                ToEmail = "*****@*****.**", ToName = "Pratik outlook"
                            },
                            //new EmailUtil() { ToEmail = "*****@*****.**", ToName = "Mayur Gaikwad" }
                        };
                        foreach (EmailUtil u in lst)
                        {
                            u.Subject = String.Format("Portfolio Excel for - '{0}'", DateTime.Today.Date.ToString("MM-dd-yyyy"));
                            u.Body    = "Please find attached portfolio excel sheet for today.";
                            bool bSendEMail = EmailUtil.Instance.SendEmail(u, strAttchmentFileName: String.Format("{0}{1}.xlsx", PortfoliExcelPath, excelFileName));
                            if (bSendEMail)
                            {
                                ApplicationLog.Instance.WriteInfo(String.Format("Email sent to '{0}' at '{1}'", u.ToEmail, DateTime.Now));
                            }
                            else
                            {
                                ApplicationLog.Instance.WriteWarning(String.Format("Email NOT sent to '{0}'", u.ToEmail));
                            }
                        }
                    }
                    bRtnVal = true;
                }
                else
                {
                    ApplicationLog.Instance.WriteError("Request file not in correct format.");
                }
            }
            catch (Exception ex)
            {
                bRtnVal = false;
                ApplicationLog.Instance.WriteException(ex);
            }
            return(bRtnVal);
        }