İçeriğe geç

Stopwatch Using (Kullanımı)

Bir önceki yazımda StopWatch nesnesinin nasıl kullanacağını işledim.

Bu yazıda kısaca kendi yazdığım using bloğu içerisinde stopwatch kullanımına değineceğim.

Öncelikle tam sınıfımız “StopwatchUsing.cs” ismi ile aşağıdaki gibi olacak.


public class StopwatchUsing : IDisposable
{
bool disposed = false;

Stopwatch sw = new Stopwatch();
string log_name = string.Empty;
public StopwatchUsing(string _log_name)
{
sw.Start();
this.log_name = _log_name;
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposed)
return;

if (disposing)
{
sw.Stop();

long sonuc = sw.ElapsedMilliseconds;
StreamWriter writer = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "islem-sure-log.txt", true);
try { writer.WriteLine(string.Format("{0}: {1} milisaniye", log_name, sw.ElapsedMilliseconds)); }
catch (Exception)
{ throw; }
finally { writer.Close(); }

this.log_name = string.Empty;
}

disposed = true;
}
}

Kullanımı ise şu şekilde:


using (StopwatchUsing _stop = new StopwatchUsing("xxx işleminin derlenme süresi"))
{

Süresi ölçülecek kodlarımız bu alana...

}

Gördüğünüz gibi artık her yerde Stopwatch nesnesi üretip, ayrı ayrı kodlamanız gerekmeyecek.

Bu using bloğu içerisine aldığınız her kodun bitiminde süre otomatik olarak bildiriliyor olacak.

 

Kolaylıklar… 🙂

0 0 votes
Article Rating
Tarih:Asp.Net - C#Asp.Net - MVCÇerez Bilgiler
Subscribe
Bildir
guest
0 Yorum
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x