Calculate time of method execution and send to WCF service async
- by Tim
I need to implement time calculation for repository methods in my asp .net mvc project classes. The problem is that i need to send time calculation data to WCF Service which is time consuming. I think about threads which can help to cal WCF service asynchronously. But I have very little experience with it. Do I need to create new thread each time or I can create a global thread, if so then how? I have something like that:
StopWatch class
public class StopWatch
{
private DateTime _startTime;
private DateTime _endTime;
public void Start()
{
_startTime = DateTime.Now;
}
protected void StopTimerAndWriteStatistics()
{
_endTime = DateTime.Now;
TimeSpan timeResult = _endTime - _startTime;
//WCF proxy object
var reporting = AppServerUtility.GetProxy<IReporting>();
//Send data to server
reporting.WriteStatistics(_startTime, _endTime, timeResult, "some information");
}
public void Stop()
{
//Here is the thread I have question with
var thread = new Thread(StopTimerAndWriteStatistics);
thread.Start();
}
}
Using of StopWatch class in Repository
public class SomeRepository
{
public List<ObjectInfo> List()
{
StopWatch sw = new StopWatch();
sw.Start();
//performing long time operation
sw.Stop();
}
}
What am I doing wrong with threads?