i have one question. I ready many pages about best thereading like this http://www.albahari.com/threading/part4.aspx.
everything is written fine, but i still have problem with threads. I run 6 threads at the same time. I am parsing some data and this data must be stored to database. But i can not store the same data twice.
Now i get many replicated data in database. How can i prevent this. lock() is not good i think. I want to use Monitor, but i don't know if this is ok.
This is thread code:
CultureInfo contentCulture = (CultureInfo)propertyBag["LanguageCulture"].Value;
string cultureDisplayValue = "N/A";
if (!contentCulture.IsNull())
{
cultureDisplayValue = contentCulture.DisplayName;
}
AllocConsole();
Console.Out.WriteLine();
Console.Out.WriteLine("Url: {0}", propertyBag.Step.Uri);
Console.Out.WriteLine("Content type: {0}", propertyBag.ContentType);
Console.Out.WriteLine("Content length: {0}", propertyBag.Text.IsNull() ? 0 : propertyBag.Text.Length);
Console.Out.WriteLine("Depth: {0}", propertyBag.Step.Depth);
Console.Out.WriteLine("Culture: {0}", cultureDisplayValue);
Console.Out.WriteLine("ThreadId: {0}", Thread.CurrentThread.ManagedThreadId);
Console.Out.WriteLine("Thread Count: {0}", crawler.ThreadsInUse);
Console.Out.WriteLine();
ConsoleCount++;
if (ConsoleCount > 1000)
{
Console.Clear();
ConsoleCount = 0;
}
HtmlDocument htmlDoc = new HtmlDocument();
Encoding documentEncoding = htmlDoc.DetectEncoding(propertyBag.GetResponse());
propertyBag.GetResponse().Seek(0, SeekOrigin.Begin);
if (documentEncoding != null)
{
htmlDoc.Load(propertyBag.GetResponse(), documentEncoding, true);
}
else
{
htmlDoc.Load(propertyBag.GetResponse(), true);
}
string htmlContent = htmlDoc.DocumentNode.OuterHtml;
if (string.IsNullOrEmpty(htmlContent)) return;
IAdvertismentsDao advertismentsDao = DaoFactory.GetAdvertisementsDao();
List<TagValuePair> listTagValuePair = HtmlHelper.GetTagsAndValues(htmlContent);
string link = propertyBag.Step.Uri.ToString();
if (string.IsNullOrEmpty(link))
{
link = propertyBag.ResponseUri.ToString();
}
Advertisements ad =
new CrawlerManager(DaoFactory, ConnectionString).GetAdvertismentFromHtmlContent(
listTagValuePair, Agency, link);
if (ad != null)
{
if (!advertismentsDao.AdvertisementUrlExist(ad.Url))
{
if (
!advertismentsDao.AdvertisementExist(ad.Price, ad.HollidayDuration, ad.Name,
ad.Description, ad.City, ad.Area, ad.Country,
ad.Agency))
{
advertismentsDao.Save(ad);
advertismentsDao.CommitChanges();
}
}
else
{
if (advertismentsDao.ChekIfNeedUpdate(ad))
{
Advertisements advertisements = advertismentsDao.GetByUrl(ad.Url);
advertisements.Price = ad.Price;
advertisements.HollidayDuration = ad.HollidayDuration;
advertisements.Name = ad.Name;
advertisements.Description = ad.Description;
advertisements.DepartureDate = ad.DepartureDate;
advertismentsDao.SaveOrUpdate(advertisements);
advertismentsDao.CommitChanges();
}
}
InvokeEvent(ad, string.Empty);
}
else
InvokeEvent(null, link);