This is probably a newb question so i'll come right out and say that. This is my first time creating an IDisposable class, I want to make sure that I created my class correctly, called it correctly, and Dispose of it correctly. Thanks!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Crawler
{
class LoggingClass
{
public class LoggingDisposeable : IDisposable
{
public void GenericLogging(string systemMsg, string SystemClass, string SystemSection, string ID, string FixID, string baseURL, string mysqlQueryName, string mysqlQuery)
{
string Loggingall = " insert into tblLogs " +
"set SystemMsg='" + systemMsg.Replace("'", "''") + "'" +
",SystemClass = '" + SystemClass.Replace("'", "''") + "'" +
",SystemSection = '" + SystemSection.Replace("'", "''") +
",ID = '" + CarID.Replace("'", "''") + "'" +
",FixID = '" + FixID.Replace("'", "''") + "'" +
",baseurl = '" + baseURL.Replace("'", "''") + "'" +
",mysqlqueryName = '" + mysqlQuery.Replace("'", "''") +
",mysqlquery = '" + mysqlQuery.Replace("'", "''") + "'" +
",Site = 'AutoTrader'" +
",TimeStamp = Now()";
using (var MYSQLP = new MySQLProcessing.MySQLProcessor())
{
MYSQLP.MySQLInsertUpdate(Loggingall, "Loggingall");
}
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!this.Disposed)
{
if (disposing)
{
// Perform managed cleanup here.
}
// Perform unmanaged cleanup here.
this.Disposed = true;
}
}
protected bool Disposed { get; private set; }
}
}
}
And this is how I call it.
var options = new ParallelOptions();
options.MaxDegreeOfParallelism = 5;
Parallel.ForEach(urlTable.AsEnumerable(),options, drow =>
{
using (var logClass = new LoggingClass.LoggingDisposeable())
{
logClass.GenericLogging("ZipCode not available for this dealerL", "DealershipRequest", "checkExistingDealers", dealerID, "DealerShipZipCode",rDealerLink, "", "");
}
}</pre>
using () { }blocks internally callDispose()when code exists the block, so your resource is already being disposed in this method as you've posted it and you don't need to implementIDisposableseparately. - Jim Schubert