A prepared String is not being saved completely when not debugging - c#

I have a Program which should save permanently values to a .txt file. When I debug it by putting a breakpoint into the code it works fine. But when letting it work for a while it only saves the last value caught instead of the last 3.
The method ExecuteStrategy is being executed every minute.
Here is the code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Bot_V1._0
{
public class SaveAllMinuteBarsStrategy : IStrategy
{
public Guid StrategyGuid { get; set; }
//private static string[] Data = new string[8];
private string DataDax { get; set; }
private string DataDow { get; set; }
private string DataOil { get; set; }
private string DataGold { get; set; }
private string DataSpy { get; set; }
private string DataEsd { get; set; }
private string DataEgp { get; set; }
private string DataUpy { get; set; }
private string xPath = #"G:\TradingBot\Saved Candles folder";
private DateTime firstEnter;
const char LF = '\n';
public SaveAllMinuteBarsStrategy (Guid StrategyCGuid)
{
this.StrategyGuid = StrategyGuid;
}
public List<ShareAndTimeRange> InitStrategy (Guid stratGuid)
{
firstEnter = DateTime.Now;
var Dax = new ShareAndTimeRange("DAX", 1, stratGuid, null, false);
var Dow = new ShareAndTimeRange("DOW", 1, stratGuid, null, false);
var Oil = new ShareAndTimeRange("OIL", 1, stratGuid, null, false);
var Gold = new ShareAndTimeRange("GOLD", 1, stratGuid, null, false);
var Spy = new ShareAndTimeRange("SPY", 1, stratGuid, null, false);
var Esd = new ShareAndTimeRange("EUR/USD", 1, stratGuid, null, false);
var Egb = new ShareAndTimeRange("EUR/GBP", 1, stratGuid, null, false);
var Ujp = new ShareAndTimeRange("USD/JPY", 1, stratGuid, null, false);
var ElementList = new List<ShareAndTimeRange>();
ElementList.Add(Dax);
ElementList.Add(Dow);
ElementList.Add(Oil);
ElementList.Add(Gold);
ElementList.Add(Spy);
ElementList.Add(Esd);
ElementList.Add(Egb);
ElementList.Add(Ujp);
return ElementList;
}
public void ExecuteStrategy(ShareAndTimeRange Share)
{
SaveDataHourly(Share);
}
private void SaveDataHourly(ShareAndTimeRange Share)
{
if (Share.Candle == null)
return;
var PreparedString = Share.Candle.Time.ToString("yyyyMMdd HHmmss") + ";";
PreparedString = PreparedString + Share.Candle.Open + ";" + Share.Candle.High + ";";
PreparedString = PreparedString + Share.Candle.Low + ";" + Share.Candle.Close;
PreparedString = PreparedString + LF;
switch (Share.ShareName)
{
case ("DAX"):
{
DataDax = DataDax + PreparedString;
//Data[0] = Data[0] +
bool reset = HDMYSaving(Share, DataDax);
if (reset)
DataDax = "";
break;
}
case ("DOW"):
{
DataDow = DataDow + PreparedString;
bool reset = HDMYSaving(Share, DataDow);
if (reset)
DataDow = "";
break;
}
case ("OIL"):
{
DataOil = DataOil + PreparedString;
bool reset = HDMYSaving(Share, DataOil);
if (reset)
DataOil = "";
break;
}
case ("GOLD"):
{
DataGold = DataGold + PreparedString;
bool reset = HDMYSaving(Share, DataGold);
if (reset)
DataGold = "";
break;
}
case ("SPY"):
{
DataSpy = DataSpy + PreparedString;
bool reset = HDMYSaving(Share, DataSpy);
if (reset)
DataSpy = "";
break;
}
case ("EUR/USD"):
{
DataEsd = DataEsd + PreparedString;
bool reset = HDMYSaving(Share, DataEsd);
if (reset)
DataEsd = "";
break;
}
case ("EUR/GBP"):
{
DataEgp = DataEgp + PreparedString;
bool reset = HDMYSaving(Share, DataEgp);
if (reset)
DataEgp = "";
break;
}
case ("USD/JPY"):
{
DataUpy = DataUpy + PreparedString;
bool reset = HDMYSaving(Share, DataUpy);
if (reset)
DataUpy = "";
break;
}
}
}
private void SavePersistentData (string FolderName , string FileName, string Share )
{
if (!System.IO.File.Exists(FolderName))
{
Directory.CreateDirectory(FolderName);
}
string fullPath = System.IO.Path.Combine(FolderName, FileName);
if (!System.IO.File.Exists(fullPath))
{
FileStream fs = File.Create(fullPath);
fs.Dispose();
}
using (StreamWriter sw = new StreamWriter(fullPath, false))
{
sw.Write(Share);
sw.Dispose();
}
//System.IO.File.WriteAllText(fullPath, Share);
}
private void SavePersistent (string ReadFolder , string WriteFolder , string WriteFolderFile)
{
DirectoryInfo ParDir = new DirectoryInfo(ReadFolder);
string dat = "";
foreach (var item in ParDir.GetFiles())
{
var elem = System.IO.Path.Combine(ReadFolder, item.Name);
dat += System.IO.File.ReadAllText(elem);
}
SavePersistentData(WriteFolder, WriteFolderFile, dat);
foreach (var item in ParDir.GetFiles())
{
var del = System.IO.Path.Combine(ReadFolder, item.Name);
File.Delete(del);
}
}
private bool HDMYSaving(ShareAndTimeRange Share, string Values)
{
if (Share.Candle == null)
return false;
bool toReset = false;
string SharePath = System.IO.Path.Combine(xPath, Share.ShareName);//c:\TradingBot\Saved Candles folder\"Sharename"\
string HourPath = System.IO.Path.Combine(SharePath, "Hour Data");//c:\TradingBot\Saved Candles folder\"Sharename"\Hour Data
int res;
int resuzt = Math.DivRem(Share.Candle.Time.Minute,3,out res);
if ( res == 0) //0 Share.Candle.Time.Minute == 0
{
DateTime DateToWriteH = Share.Candle.Time;
string HourFilePath = DateToWriteH.ToString("yyyyMMddHHmm") + ".txt";
SavePersistentData(HourPath, HourFilePath, Values);
toReset = true;
}
DateTime DateToWriteD = Share.Candle.Time.AddDays(-1);
string DayPath = System.IO.Path.Combine(SharePath, "Day Data");//c:\TradingBot\Saved Candles folder\"Sharename"\Day Data
string DayFilePath = System.IO.Path.Combine(DayPath, DateToWriteD.ToString("yyyyMMdd") + ".txt");
if (Share.Candle.Time.Hour == 0 && Share.Candle.Time.Minute == 0 && !System.IO.File.Exists(DayFilePath))
{
SavePersistent(HourPath, DayPath, DateToWriteD.ToString("yyyyMMdd") + ".txt");
}
DateTime DateToWriteM = Share.Candle.Time.AddMonths(-1);
string MonthPath = System.IO.Path.Combine(SharePath, "Month Data");//c:\TradingBot\Saved Candles folder\"Sharename"\Month Data
string MonthFilePath = System.IO.Path.Combine(MonthPath, DateToWriteM.ToString("yyyyMM") + ".txt");
if (Share.Candle.Time.Day == 1 && Share.Candle.Time.Hour == 0 && Share.Candle.Time.Minute == 0 && !System.IO.File.Exists(MonthFilePath))
{
SavePersistent(DayPath, MonthPath, DateToWriteM.ToString("yyyyMM") + ".txt");
}
DateTime DateToWriteY = Share.Candle.Time.AddYears(-1);
string YearPath = System.IO.Path.Combine(SharePath, "Year Data");//c:\TradingBot\Saved Candles folder\"Sharename"\Year Data
string YearFilePath = System.IO.Path.Combine(YearPath, DateToWriteY.ToString("yyyy") + ".txt");
if (Share.Candle.Time.Month == 1 && Share.Candle.Time.Day == 1 && Share.Candle.Time.Hour == 0 && Share.Candle.Time.Minute == 0 && !System.IO.File.Exists(YearFilePath))
{
SavePersistent(MonthPath, YearPath, DateToWriteY.ToString("yyyy") + ".txt");
}
return toReset;
}
}
Is there a problem having a lot of Events which are fired every 2 seconds or every minute?
Thanks!!

i found the problem. It was on another Class. The Fault was firing an event every time the program colleted a new value, but the program schould wait for the other "WebsTakenInCare".
Here is the Code before:
if (DataList.Count() == WebsTakenInCare)
{
FinalizedData = ShortenUpData(DataList);
DataList.Clear();
}
BuildCandles(FinalizedData);
and after changing:
if (DataList.Count() == WebsTakenInCare)
{
FinalizedData = ShortenUpData(DataList);
DataList.Clear();
BuildCandles(FinalizedData);
}
else
return;
regards!

Related

Change for loop to forEach

I am working on a inventory updating program in vs2022 which I am using Gembox Spreadsheet for, It is working right now, but the boss man would like me to try to convert my for loop to a for each loop. The loop is what I am using to update my file.
Current For Loop
public void updateFile(string filename, int range, ItemLine selectedItem, decimal outValue)
{
var fullPath = $".\\Backend Files\\{filename}";
SpreadsheetInfo.SetLicense("FREE -LIMITED-KEY");
var workbook = ExcelFile.Load(fullPath, new CsvLoadOptions(CsvType.CommaDelimited));
var worksheet = workbook.Worksheets[0];
//foreach (var row in worksheet.Rows.Skip(1))
//{
// foreach (var cell in row.AllocatedCells)
// {
// string updateValue =
// if(cell.Value == int.TryParse((int)outValue, out int updateValue))
// {
// }
// }
//}
for (int i = 1; i <= range; i++)
{
var plan = worksheet.Rows[i].Cells[0].Value;
var desc = worksheet.Rows[i].Cells[1].Value;
var csvDescription = plan + " - " + desc;
var platedescription = plan + " - ";
if (selectedItem.ComboDescription == csvDescription)
{
worksheet.Rows[i].Cells[2].Value = selectedItem.Quantity;
}
if (selectedItem.plateDescription == platedescription)
{
worksheet.Rows[i].Cells[1].Value = selectedItem.Quantity;
}
}
workbook.Save(fullPath, new CsvSaveOptions(CsvType.CommaDelimited));
}
I beleive the forEach would look similar to this
private static List<ItemLine> ReadFile(string fileName, string defaultValueDescription)
{
string path = $".\\Backend Files\\{fileName}";
SpreadsheetInfo.SetLicense("FREE -LIMITED-KEY");
var workbook = ExcelFile.Load(path, new CsvLoadOptions(CsvType.CommaDelimited));
var worksheet = workbook.Worksheets[0];
var items = new List<ItemLine>();
items.Add(new ItemLine { Description = defaultValueDescription, Quantity = 0 });
foreach (var row in worksheet.Rows.Skip(1))
{
var cells = row.AllocatedCells;
var il = new ItemLine();
if (cells.Count == 2)
{
il.Description = cells[0].Value?.ToString() ?? "Description Not Found";
il.Quantity = cells[1].IntValue;
}
else if (cells.Count >= 3)
{
il.Plan = cells[0].Value?.ToString() ?? "Plan Not Found";
il.Description = cells[1].Value?.ToString() ?? "Description Not Found";
il.Quantity = cells[2].IntValue;
}
items.Add(il);
}
return items;
}

How to limit the amount of parameters for concatenatenation?

The idea of the program is to concatenate different parameters and put it all inside another parameter. How to let the end user decide if he wants to concatenate 2 or 3 parameters. Right now it is like if you don't put in 3 parameters it won't work. Nothing I came up with works.
namespace CombineParametersWinForm
{
public partial class Form1 : System.Windows.Forms.Form
{
//Class variable
Document revitDoc { get; set; }
public Form1(Document doc)
{
InitializeComponent();
this.revitDoc = doc;
//Create a list of the parameters you want your user to choose from
List<string> stringParameters = new List<string>
{
"Weight",
"Angle",
"Manufacturer"
};
//Add list to comboboxes on form
foreach (string parameterName in stringParameters)
{
comboBox1.Items.Insert(0, parameterName);
comboBox2.Items.Insert(0, parameterName);
comboBox3.Items.Insert(0, parameterName);
}
}
private void button1_Click(object sender, EventArgs e)
{
FilteredElementCollector collector = new FilteredElementCollector(revitDoc);
ElementCategoryFilter filter = new ElementCategoryFilter(BuiltInCategory.OST_PipeFitting);
//Applying Filter
IList<Element> ducts = collector.WherePasses(filter).WhereElementIsNotElementType().ToElements();
foreach (Element duct in ducts)
{
//Get Parameter values
string parameterValue1 = duct.LookupParameter(comboBox1.Text).AsValueString();
string parameterValue2 = duct.LookupParameter(comboBox2.Text).AsValueString();
string parameterValue3 = duct.LookupParameter(comboBox3.Text).AsValueString();
string newValue = parameterValue1 + "-" + parameterValue2 + "-" + parameterValue3;
//do not need .ToString() when setting parameter
using (Transaction t = new Transaction(revitDoc, "Set Parameter name"))
{
t.Start();
duct.LookupParameter("New").Set(newValue);
t.Commit();
}
Ideas. First is pretty long one.
string parameterValue1 = duct.LookupParameter(comboBox1.Text).AsValueString();
string parameterValue2 = duct.LookupParameter(comboBox2.Text).AsValueString();
string parameterValue3 = duct.LookupParameter(comboBox3.Text).AsValueString();
if (parameterValue1 != "" || parameterValue1 != null)
{
parameterValue1 = parameterValue11;
}
if (parameterValue2 != "" || parameterValue2 != null)
{
parameterValue1 = parameterValue22;
}
if (parameterValue3 != "" || parameterValue3 != null)
{
parameterValue3 = parameterValue33;
}
if (parameterValue1 == "" || parameterValue1 == null)
{
parameterValue11 = "";
}
if (parameterValue2 == "" || parameterValue2 == null)
{
parameterValue22 = "";
}
if (parameterValue3 == "" || parameterValue3 == null)
{
parameterValue33 = "";
}
string newValue = parameterValue1 + "-" + parameterValue2 + "-" + parameterValue3;
using (Transaction t = new Transaction(revitDoc, "Set Parameter name"))
{
t.Start();
duct.LookupParameter("New").Set(newValue);
t.Commit();
}
}
}
}
}
Second is a short one but still doesn't work
string parameterValue1 = duct.LookupParameter(comboBox1.Text).AsValueString();
string parameterValue2 = duct.LookupParameter(comboBox2.Text).AsValueString();
string parameterValue3 = duct.LookupParameter(comboBox3.Text).AsValueString();
List<string> listParam = new List<string> { parameterValue1, parameterValue2, parameterValue3 };
foreach (string s in listParam)
{
if (s != "" /*&& s != null*/)
{
List<string> listParamNotNull = new List<string> { s };
string newValue = String.Join(" ,", listParamNotNull);
using (Transaction t = new Transaction(revitDoc, "Set Parameter name"))
{
t.Start();
duct.LookupParameter("New").Set(newValue);
t.Commit();
}
You are creating three separate Lists and wanting to do something against all of them combined, but actually doing against each of them individually, overwriting as you go.
Please take a closer look at your nested logic.
Something like this is likely more appropriate:
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
string parameterValue1 = "Value1";
string parameterValue2 = ""; // purposefully providing an empty value
string parameterValue3 = "Value3";
var listParamIn = new List<string> { parameterValue1, parameterValue2, parameterValue3 };
var listParamOut = new List<string>();
foreach (string s in listParamIn){
if (string.IsNullOrEmpty(s))
continue;
listParamOut.Add(s);
}
string newValue = String.Join(", ", listParamOut);
Console.WriteLine(newValue);
/* continue with your transaction
using (Transaction t = new Transaction(revitDoc, "Set Parameter name")){
t.Start();
duct.LookupParameter("New").Set(newValue);
t.Commit();
}
*/
}
}
Output:
Value1, Value3
See:
https://dotnetfiddle.net/mXdL5F

Fragment cannot find variable from other Fragment

I have a problem. I have 2 Android.Support.V4.App.Fragments
In the first Framgent I use this code:
AgentSpinnerAdapter = new ArrayAdapter<string>(Context, Android.Resource.Layout.SimpleSpinnerDropDownItem);
AgentSpinner.Adapter = AgentSpinnerAdapter;
foreach (string[] str in NamesArray)
{
string AgentId = str[0];
string Owner = str[1];
string Exchange = str[2];
string Remark = str[3];
AgentSpinnerAdapter.Add("Agent " + AgentId + " - " + Owner + " - " + Remark);
}
In the second Fragment I call this line:
dbValue = Fragment1.AgentSpinnerAdapter.GetItem(0);
But it says that AgentSpinnerAdapter is a nullreference, which is weird, because it get's filled. I have set the AgentSpinnerAdapter to Public static. Also in my MainActivity I first create Fragment1 and then Fragment2 like this:
Fragment1 = Fragment1.NewInstance();
Fragment2 = Fragment2.NewInstance();
What am I doing wrong?
UPDATE
Here is the full Fragment1.cs method
public void LoadAgentSpinner()
{
string json = "";
try
{
string html = string.Empty;
string url = "https://www.efy.nl/app/getagents.php";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
IgnoreBadCertificates();
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
json = reader.ReadToEnd();
}
}
catch (Exception ex1)
{
try
{
WebClient client = new WebClient();
NameValueCollection fields = new NameValueCollection();
fields.Add("error", ex1.GetBaseException().ToString());
string url = "https://www.mywebsite.com";
IgnoreBadCertificates();
byte[] respBytes = client.UploadValues(url, fields);
string resp = client.Encoding.GetString(respBytes);
SelectedQuantity.Text = "";
SelectedLimit.Text = "";
}
catch (Exception ex2)
{
string exFullName = (ex2.GetType().FullName);
string ExceptionString = (ex2.GetBaseException().ToString());
}
}
//Parse json content
var jObject = JObject.Parse(json);
//Create Array from everything inside Node:"Coins"
var agentPropery = jObject["Agents"] as JArray;
//Create List to save Coin Data
agentList = new List<agent>();
//Find every value in Array: coinPropery
foreach (var property in agentPropery)
{
//Convert every value in Array to string
var propertyList = JsonConvert.DeserializeObject<List<agent>>(property.ToString());
//Add all strings to List
agentList.AddRange(propertyList);
}
//Get all the values from Name, and convert it to an Array
string[][] NamesArray = agentList.OrderBy(i => i.AgentId)
.Select(i => new string[] { i.AgentId.ToString(), i.Owner, i.Exchange, i.Remark })
.Distinct()
.ToArray();
AgentSpinnerAdapter = new ArrayAdapter<string>(Context, Android.Resource.Layout.SimpleSpinnerDropDownItem);
AgentSpinner.Adapter = AgentSpinnerAdapter;
foreach (string[] str in NamesArray)
{
string AgentId = str[0];
string Owner = str[1];
string Exchange = str[2];
string Remark = str[3];
AgentSpinnerAdapter.Add("Agent " + AgentId + " - " + Owner + " - " + Remark); // format your string here
}
if(MainActivity.db.CheckExistTableSettings("Default Agent") == true)
{
string Value = MainActivity.db.SelectValueFromTableSettings("Default Agent");
int spinnerPosition = AgentSpinnerAdapter.GetPosition(Value);
AgentSpinner.SetSelection(spinnerPosition);
}
else
{
AgentSpinner.SetSelection(0);
}
}
In a few of my applications it's necessary to access the other fragments from my main Activity, so we do the following:
public class MainActivity : AppCompatActivity, BottomNavigationView.IOnNavigationItemSelectedListener
{
public static Dictionary<string, Fragment> FragmentList { get; set; }
private Fragment currentFragment = null;
private BottomNavigationView navigation;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.layout_mainactivity);
// create our fragments and initialise them early.
if (FragmentList == null)
{
FragmentList = new Dictionary<string, Fragment>
{
{ "main", MainFragment.NewInstance() },
{ "bugreport", BugReportFragment.NewInstance() },
{ "settings", SettingsFragment.NewInstance() }
};
}
navigation = FindViewById<BottomNavigationView>(Resource.Id.bottom_nav);
navigation.SetOnNavigationItemSelectedListener(this);
navigation.SelectedItemId = Resource.Id.navigation_main;
}
public bool OnNavigationItemSelected(IMenuItem item)
{
if (!popAction)
{
navigationResourceStack.Push(item.ItemId);
}
switch (item.ItemId)
{
case Resource.Id.navigation_main:
currentFragment = FragmentList["main"];
break;
case Resource.Id.navigation_settings:
currentFragment = FragmentList["settings"];
break;
case Resource.Id.navigation_bugreport:
currentFragment = FragmentList["bugreport"];
break;
}
if (currentFragment == null)
{
return false;
}
else
{
FragmentManager.BeginTransaction().Replace(Resource.Id.frame_content, currentFragment).Commit();
return true;
}
}
}
What this means is you could do something like MainActivity.FragmentList["main"] and then call any public method on the actual initialized class because a pointer to it is stored within the dictionary.

Is that necessary to dispose objects inside static functions?

Ok i am having a major problem atm.
My software is using extremely high amount of ram. I am using a lot of HtmlAgilityPack.HtmlDocument objects with big size pages sources.
However all of the objects are used inside static functions and HtmlAgilityPack.HtmlDocument isn't IDisposable
So do i need to set every variable explicitly to null ?
Even if they are inside static functions ?
For example do i need to set these variables to null at the end of the function below
The variables i am asking : lstDrwList ? Or since it is inside it will get disposed automatically ?
Should i call explicitly garbage collector ?
C# .net 4.5 WPF application
private static void func_CheckWaitingToProcessPages(Object state)
{
ParallelOptions myOptions = new ParallelOptions();
myOptions.MaxDegreeOfParallelism = PublicSettings.ir_How_Many_Tasks_For_Per_Pages_Process;
List<DataRow> lstDrwList = new List<DataRow>();
using (DataTable dtMyTable = DbConnection.db_Select_DataTable(srSelectTopProcessPagesQuery))
{
foreach (DataRow drw in dtMyTable.Rows)
{
lstDrwList.Add(drw);
}
}
Parallel.ForEach(lstDrwList, myOptions, drw =>
{
process_Given_Page(drw);
});
}
The problem is found issue is how to fix
Here the problem this happens in 10 seconds i used visual studio profiler
Here the full class that causes this huge memory leak issue
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace doktora_tez_projesi_crawler_program
{
public static class PagesProcessor
{
private static Timer _timer;
private static int howManySeconds = 10;
public static void func_StartCrawlingWaitingUrls()
{
PublicStaticFunctions.AddMsgToEvents("Checking waiting to process crawled urls process started every " + howManySeconds + " seconds!");
_timer = new Timer(func_CheckWaitingToProcessPages, null, PublicSettings.irTimers_Delayed_Start_MiliSeconds, howManySeconds * 1000);
}
private static string srSelectTopProcessPagesQuery = " select top 100 cl_IdUrl,cl_RooSiteId,cl_CrawlSource,cl_CrawlOrgUrl from tblCrawlUrls " +
" where cl_PageProcessed=0 and cl_TotalCrawlTimes > 0 " +
" order by cl_LastProcessDate asc";
private static void func_CheckWaitingToProcessPages(Object state)
{
ParallelOptions myOptions = new ParallelOptions();
myOptions.MaxDegreeOfParallelism = PublicSettings.ir_How_Many_Tasks_For_Per_Pages_Process;
List<DataRow> lstDrwList = new List<DataRow>();
using (DataTable dtMyTable = DbConnection.db_Select_DataTable(srSelectTopProcessPagesQuery))
{
foreach (DataRow drw in dtMyTable.Rows)
{
lstDrwList.Add(drw);
}
}
Parallel.ForEach(lstDrwList, myOptions, drw =>
{
process_Given_Page(drw);
});
}
private class csProductFeatures
{
public string srProductRootSiteId = "null", srProductTitle = "null", srProductCode = "null", srProductImageLink = "null";
public string srProductDetailedExplanation = "null", srProductFeatures = "null", srCrawledOrgUrl = "null", srProductIdCode = "null";
public bool blPossibleProductPage = false, blFreeCargo = false, blProductPage = true;
public List<string> lstProductCategories = new List<string>();
public int irProductPrice = 0;
public List<csProductComments> lstProductComments = new List<csProductComments>();
public List<KeyValuePair<string, string>> lstProductFeatures = new List<KeyValuePair<string, string>>();
}
private class csProductComments
{
public string srCommentTitle = "null", srCommentPros = "null", srCommentCons = "null";
public int irCommentScore = 0; //0 = negative 5=full star
}
private static void process_Given_Page(DataRow drw)
{
csProductFeatures temp_ProductFeatures = new csProductFeatures();
temp_ProductFeatures.srProductRootSiteId = drw["cl_RooSiteId"].ToString();
temp_ProductFeatures.srCrawledOrgUrl = drw["cl_CrawlOrgUrl"].ToString();
HtmlDocument hdMyDoc = new HtmlDocument();//nulled
hdMyDoc.LoadHtml(drw["cl_CrawlSource"].ToString());
bool blBreakLoop = false;
foreach (var vrVariable in PublicVariables.dicRootSites[temp_ProductFeatures.srProductRootSiteId].lstRootSiteIdentifiers)
{
if (vrVariable.srHtmlObjectType != "link")
{
HtmlNodeCollection hdNodes;
if (vrVariable.blSelectMultipleNodes == false)
hdNodes = hdMyDoc.DocumentNode.SelectNodes(string.Format("//{0}[#{1}='{2}']", vrVariable.srHtmlObjectType,
vrVariable.srHtmlObjectTypeIdentifier, vrVariable.srHtmlObjectTypeName));
else
hdNodes = hdMyDoc.DocumentNode.SelectNodes(string.Format("//{0}[#{1}='{2}']//{3}", vrVariable.srHtmlObjectType,
vrVariable.srHtmlObjectTypeIdentifier, vrVariable.srHtmlObjectTypeName, vrVariable.srHtmlSubIdentifierType));
if (hdNodes == null && vrVariable.srIndetifierType == "ProductTitle")
{
blBreakLoop = true;
temp_ProductFeatures.blProductPage = false;
continue;
}
if (blBreakLoop == true)
break;
if (hdNodes == null)
continue;
string sr_Node_Required_Val = "null";
if (hdNodes[0].InnerText != null)
sr_Node_Required_Val = hdNodes[0].InnerText;
string srLinkVal = "null";
if (vrVariable.srHtmlObjectType == "a" && hdNodes[0].Attributes != null)
{
if (hdNodes[0].Attributes["href"] != null)
{
srLinkVal = PublicStaticFunctions.Return_Absolute_Url(hdNodes[0].Attributes["href"].Value, temp_ProductFeatures.srCrawledOrgUrl);
}
}
if (vrVariable.blGetValue == true)
{
if (hdNodes[0].Attributes != null)
if (hdNodes[0].Attributes["value"] != null)
sr_Node_Required_Val = hdNodes[0].Attributes["value"].Value;
}
sr_Node_Required_Val = sr_Node_Required_Val.Trim();
switch (vrVariable.srIndetifierType)
{
case "ProductPage":
temp_ProductFeatures.blPossibleProductPage = true;
break;
case "ProductTitle":
temp_ProductFeatures.srProductTitle = sr_Node_Required_Val;
break;
case "ProductCode":
temp_ProductFeatures.srProductCode = sr_Node_Required_Val;
break;
case "ProductCargo":
temp_ProductFeatures.blFreeCargo = true;
break;
case "ProductCategories":
temp_ProductFeatures.lstProductCategories = func_Return_Product_Categories(hdNodes);
break;
case "ProductPrice":
temp_ProductFeatures.irProductPrice = func_Return_Product_Price(sr_Node_Required_Val, temp_ProductFeatures.srProductRootSiteId);
break;
case "ProductImage":
temp_ProductFeatures.srProductImageLink = srLinkVal;
break;
case "ProductIdCode":
temp_ProductFeatures.srProductIdCode = sr_Node_Required_Val;
break;
}
}
if (vrVariable.srHtmlObjectType == "link")
{
string srLinkToFetch = vrVariable.srHtmlObjectTypeIdentifier;
if (vrVariable.blUsesProductIdCode == true)
{
srLinkToFetch = string.Format(srLinkToFetch, temp_ProductFeatures.srProductIdCode);
}
string srFetchResult = CrawlGivenUrl.func_fetch_Page(srLinkToFetch);
string srResultToAssign = "null";
if (srFetchResult == PublicSettings.srCrawlFailedMessage)
{
srResultToAssign = srFetchResult;
}
else
{
HtmlDocument temp_HdDocument = new HtmlDocument();//nulled
temp_HdDocument.LoadHtml(srFetchResult);
if (temp_HdDocument.DocumentNode != null)
if (temp_HdDocument.DocumentNode.InnerText != null)
srResultToAssign = temp_HdDocument.DocumentNode.InnerText;
temp_HdDocument = null;
}
switch (vrVariable.srIndetifierType)
{
case "ProductExplanation":
temp_ProductFeatures.srProductDetailedExplanation = srResultToAssign;
break;
case "ProductFeatures":
temp_ProductFeatures.lstProductFeatures = func_Return_Product_Features(temp_ProductFeatures.srProductRootSiteId, srFetchResult, temp_ProductFeatures.srCrawledOrgUrl);
break;
}
}
}
if (temp_ProductFeatures.blProductPage == true)
{
string asdas = "";
}
hdMyDoc = null;
}
private static List<string> func_Return_Product_Categories(HtmlNodeCollection hdNodeCollection)
{
List<string> lstCategories = new List<string> { };
foreach (HtmlNode hdNode in hdNodeCollection)
{
if (hdNode.InnerText != null)
{
lstCategories.Add(hdNode.InnerText);
}
}
return lstCategories;
}
private static int func_Return_Product_Price(string srPriceText, string srRootSiteId)
{
int irPrice = 0;
srPriceText = srPriceText.Replace(PublicVariables.dicRootSites[srRootSiteId].srPriceDelimeter, "");
if (srPriceText.Contains(PublicVariables.dicRootSites[srRootSiteId].srPriceIgnoreDelimeter) == true)
{
srPriceText = srPriceText.Substring(0, srPriceText.IndexOf(PublicVariables.dicRootSites[srRootSiteId].srPriceIgnoreDelimeter));
}
Int32.TryParse(srPriceText, out irPrice);
return irPrice;
}
private static List<KeyValuePair<string, string>> func_Return_Product_Features(string srRootSiteId, string srPageSource, string srCrawlUrl)
{
List<KeyValuePair<string, string>> lstFoundFeatures = new List<KeyValuePair<string, string>>();
if (srPageSource == PublicSettings.srCrawlFailedMessage)
return lstFoundFeatures;
HtmlDocument temp_HdDocument = new HtmlDocument();//nulled
temp_HdDocument.LoadHtml(srPageSource);
List<string> lstFeatureTitles = new List<string>();
List<string> lstFeatureDescriptions = new List<string>();
foreach (var vrVariable in PublicVariables.dicRootSites[srRootSiteId].lstRootSitesFeaturesIdentifiers)
{
if (vrVariable.blPerFeatureIdentifier == true)
{
HtmlNodeCollection hdNodes = temp_HdDocument.DocumentNode.SelectNodes(string.Format("//{0}[#{1}='{2}']", vrVariable.srHtmlObjectType,
vrVariable.srHtmlObjectIdentifier, vrVariable.srHtmlObjectIdentifierName));
if (hdNodes != null)
foreach (var vrNewVariable in PublicVariables.dicRootSites[srRootSiteId].lstRootSitesFeaturesIdentifiers)
{
if (vrNewVariable.blPerFeatureIdentifier == false)
{
foreach (HtmlNode hdTempNode in hdNodes)
{
var vrTempNewNode = hdTempNode.SelectSingleNode(string.Format("//{0}[#{1}='{2}']", vrVariable.srHtmlObjectType,
vrVariable.srHtmlObjectIdentifier, vrVariable.srHtmlObjectIdentifierName));
if (vrTempNewNode != null)
if (vrTempNewNode.InnerText != null)
{
string srNodeFeature = vrTempNewNode.InnerText.Trim();
switch (vrVariable.srWhichFeatureIdentifier)
{
case "FeatureTitle":
lstFeatureTitles.Add(srNodeFeature);
break;
case "FeatureDescription":
lstFeatureDescriptions.Add(srNodeFeature);
break;
}
}
}
}
}
break;
}
}
temp_HdDocument = null;
if (lstFeatureDescriptions.Count != lstFeatureTitles.Count)
{
ErrorLogger.LogError("found features count not equal to features description count crawled url: " + srCrawlUrl);
return lstFoundFeatures;
}
for (int i = 0; i < lstFeatureDescriptions.Count; i++)
{
KeyValuePair<string, string> myKeyValPair = new KeyValuePair<string, string>(lstFeatureTitles[i], lstFeatureDescriptions[i]);
lstFoundFeatures.Add(myKeyValPair);
}
return lstFoundFeatures;
}
}
}
No, you don't need to set variables to null in both static and instance methods. The variables inside a method (even inside static method) are on the stack space of the method, so generally they will go out of scope at the end of method execution and will be targeted for garbage collection. And Generally explicitly calling the garbage collector isn't a good practice.

How to continue execution of method after returning data?

I developed a Web API for performance monitoring on machines, that when calling the resource via URL, the information is printed on the browser in json format. One of the methods has a loop that gets a new data value every second.
Is it possible to "return" the value upon each iteration and display it in json on the browser, thus updating automatically with each iteration? In other words, I'm trying to create a live update mechanism, rather than waiting for the program to execute and then displaying the json once the performance data log is completed.
public List<LogInfo> LogTimedPerfData(string macName, string categoryName, string counterName,
string instanceName, string logName, long? seconds)
{
iModsDBRepository modsDB = new iModsDBRepository();
List<MachineInfo> theMac = modsDB.GetMachineByName(macName);
if (theMac.Count == 0)
return new List<LogInfo>();
else if (instanceName == null)
{
if (!PerformanceCounterCategory.Exists(categoryName, macName) ||
!PerformanceCounterCategory.CounterExists(counterName, categoryName, macName) )
{
return new List<LogInfo>();
}
}
else if (instanceName != null)
{
if (!PerformanceCounterCategory.Exists(categoryName, macName) ||
!PerformanceCounterCategory.CounterExists(counterName, categoryName, macName) ||
!PerformanceCounterCategory.InstanceExists(instanceName, categoryName, macName))
{
return new List<LogInfo>();
}
}
else if (logName == null)
{
return new List<LogInfo>();
}
// Check if entered log name is a duplicate for the authenticated user
List<LogInfo> checkDuplicateLog = this.GetSingleLog(logName);
if (checkDuplicateLog.Count > 0)
{
return new List<LogInfo>();
}
PerformanceCounterCategory category = new PerformanceCounterCategory(categoryName, theMac[0].MachineName);
if (category.CategoryName == null || category.MachineName == null)
{
return new List<LogInfo>();
}
List<LogInfo> logIt = new List<LogInfo>();
if (category.CategoryType != PerformanceCounterCategoryType.SingleInstance)
{
List<InstanceInfo> instances = modsDB.GetInstancesFromCatMacName(theMac[0].MachineName, category.CategoryName);
foreach (InstanceInfo inst in instances)
{
if (!category.InstanceExists(inst.InstanceName))
{
continue;
}
else if (inst.InstanceName.Equals(instanceName, StringComparison.OrdinalIgnoreCase))
{
PerformanceCounter perfCounter = new PerformanceCounter(categoryName, counterName,
inst.InstanceName, theMac[0].MachineName);
string data = "";
List<UserInfo> currUser = this.GetUserByName(WindowsIdentity.GetCurrent().Name);
string timeStarted = DateTime.Now.ToString("MM/dd/yyyy - h:mm:ss tt");
string[] dataValues = new string[(int)seconds];
for (int i = 0; i < seconds; i++)
{
data = "Value " + i + ": " + perfCounter.NextValue().ToString();
dataValues[i] = data;
Thread.Sleep(1000);
}
string timeFinished = DateTime.Now.ToString("MM/dd/yyyy - h:mm:ss tt");
Log log = new Log
{
LogName = logName,
CounterName = perfCounter.CounterName,
InstanceName = perfCounter.InstanceName,
CategoryName = perfCounter.CategoryName,
MachineName = perfCounter.MachineName,
TimeStarted = timeStarted,
TimeFinished = timeFinished,
PerformanceData = string.Join(",", dataValues),
UserID = currUser[0].UserID
};
this.CreateLog(log);
logIt.Add(new LogInfo
{
LogName = logName,
CounterName = perfCounter.CounterName,
InstanceName = perfCounter.InstanceName,
CategoryName = perfCounter.CategoryName,
MachineName = perfCounter.MachineName,
TimeStarted = timeStarted,
TimeFinished = timeFinished,
PerformanceData = dataValues.ToList<string>()
});
break;
}
}
}
else
{
PerformanceCounter perfCounter = new PerformanceCounter(categoryName, counterName,
"", theMac[0].MachineName);
string data = "";
List<UserInfo> currUser = this.GetUserByName(WindowsIdentity.GetCurrent().Name);
string timeStarted = DateTime.Now.ToString("MM/dd/yyyy - h:mm:ss tt");
string[] dataValues = new string[(int)seconds];
for (int i = 0; i < seconds; i++)
{
data = "Value " + i + ": " + perfCounter.NextValue().ToString();
dataValues[i] = data;
Thread.Sleep(1000);
}
string timeFinished = DateTime.Now.ToString("MM/dd/yyyy - h:mm:ss tt");
Log log = new Log
{
LogName = logName,
CounterName = perfCounter.CounterName,
InstanceName = perfCounter.InstanceName,
CategoryName = perfCounter.CategoryName,
MachineName = perfCounter.MachineName,
TimeStarted = timeStarted,
TimeFinished = timeFinished,
PerformanceData = string.Join(",", dataValues),
UserID = currUser[0].UserID
};
this.CreateLog(log);
logIt.Add(new LogInfo
{
LogName = logName,
CounterName = perfCounter.CounterName,
InstanceName = perfCounter.InstanceName,
CategoryName = perfCounter.CategoryName,
MachineName = perfCounter.MachineName,
TimeStarted = timeStarted,
TimeFinished = timeFinished,
PerformanceData = dataValues.ToList<string>()
});
}
return logIt;
}
You might want to do some research on SignalR there are a couple tutorials out there on pushing notifications to browsers with SignalR.
Handful of possibly helpful links:
https://github.com/SignalR/SignalR/wiki
http://www.codeproject.com/Articles/322154/ASP-NET-MVC-SIngalR-and-Knockout-based-Real-time-U
http://www.msguy.com/2011/11/real-time-push-notifications-with.html

Categories