Reputation:
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>
Upvotes: 0
Views: 513
Reputation: 8292
I think what you intend to do is something like this:
public class LoggingDisposeable : IDisposable
{
MySQLProcessing MySQLP;
public LoggingDisposeable()
{
MYSQLP = new MySQLProcessing.MySQLProcessor();
}
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()";
MYSQLP.MySQLInsertUpdate(Loggingall, "Loggingall");
}
public void Dispose()
{
MYSQLP.Dispose();
}
}
Then use it like this:
using (var logClass = new LoggingDisposeable())
{
var options = new ParallelOptions();
options.MaxDegreeOfParallelism = 5;
Parallel.ForEach(urlTable.AsEnumerable(), options, drow =>
{
logClass.GenericLogging("ZipCode not available for this dealerL", "DealershipRequest", "checkExistingDealers", dealerID, "DealerShipZipCode", rDealerLink, "", "");
});
}
Upvotes: 2