1

I have a function which is taking a lot of time to execute in a web application.

I have tested this with a profiler and by my logging.

I have other functions running in the same pageload.

What is a best way to display the rest of the values from those functions and keep this function in a thread and display it in a label when it finishes?

This function is used to get events in application which takes time.

private void getEventErrors()
    {
        EventLog eventLog = new EventLog("Application", ".");

        getEvents(eventLog.Entries);
    }

private void getEvents(EventLogEntryCollection eventLogEntryCollection)
    {
        int errorEvents = 0;

        foreach (EventLogEntry logEntry in eventLogEntryCollection)
        {
            if (logEntry.Source.Equals("XYZ"))
            {
                DateTime variable = Convert.ToDateTime(logEntry.TimeWritten);
                long eventTimeTicks = (variable.Ticks);
                long eventTimeUTC = (eventTimeTicks - 621355968000000000) / 10000000;

                long presentDayTicks = DateTime.Now.Ticks;
                long daysBackSeconds = ((presentDayTicks - 864000000000) - 621355968000000000) / 10000000;


                if (eventTimeUTC > daysBackSeconds)
                {
                    if (logEntry.EntryType.ToString() == "Error")
                    {
                        errorEvents = errorEvents + 1;
                    }                        
                }
            }
        }
        btn_Link_Event_Errors_Val.Text = errorEvents.ToString(GUIUtility.TWO_DECIMAL_PT_FORMAT);            
        if (errorEvents == 0)
        {
            lbl_EventErrorColor.Attributes.Clear();
            lbl_EventErrorColor.Attributes.Add("class", "green");
        }
        else
        {
            lbl_EventErrorColor.Attributes.Clear();
            lbl_EventErrorColor.Attributes.Add("class", "red");
        }
    }

I have 3 functions in the pageload event, two to get the values from the DB and the other one is shown above.

Should both these functions be service calls?

What i wanted was, the page should load fast and if there is a function taking a lot of time it should run in the background and display when done and in the process if the user want to navigate to a new page it should kill it and move on.

user175084
  • 4,550
  • 28
  • 114
  • 169
  • 1
    How about moving it to a service where it belongs and making an AJAX call? – Bryan Crosby Mar 28 '12 at 20:47
  • Moving it to a service..? sorry but could you be more clear because i am new to this. I still have not started Implementing AJAX. An example will be quite helpful. Thnaks – user175084 Mar 28 '12 at 20:51

1 Answers1

2

If you have a function that is running in a separate thread in ASP.NET, you may want to consider moving it to a service. There are many reason for this

See this answer (one of many on SO) for why running long running tasks in ASP.NET is not always a good idea.

One option for the service is to use WCF. You can get started here. Your service could implement a method, say GetEvents() which you could use to pull your events. That way you won't tie up your page waiting for this process to complete (using AJAX of course). Also, this allows you to change your implementation of GetEvents() without touching your code on your website.

Community
  • 1
  • 1
Bryan Crosby
  • 6,486
  • 3
  • 36
  • 55
  • BUT calling the method of service will take equal time. – Pankaj Mar 28 '12 at 21:29
  • I have 3 functions in the pageload event, two to get the values from the DB and the other one is shown above. Should both these functions be service calls. What i wanted was, the page should load fast and if there is a function taking a lot of time it should run in the background and display when done. and in the process if the user want to navigate to a new page it should kill it and move on. – user175084 Mar 28 '12 at 22:31
  • You should consider moving the logging request to the service. You may also consider some type of paging or mechanism to retrieve a smaller amount of entries. Do you really need to view them all at the same time? – Bryan Crosby Mar 28 '12 at 23:36
  • yes it is the dashboard page so need to show the no of event errors in event logs and and no of users and machines form DB. – user175084 Mar 29 '12 at 14:22
  • @user175084: That's fine. My point was, you probably just want to load a certain page size and return that to the user. The problem with the Windows event log is, in general, rather slow to access. It is meant to be more of a post-mortem tool than being read live by your app. Are you sure you are using the *right tool for the job?* You might want to re-think your architecture. – Bryan Crosby Mar 29 '12 at 14:55