C# Memory management (Memory leak?) - c#

I am working on a program in WPF that basically retrieves JSON data from a WEB API and parses it then saves it to database with EF.
The problem is the memory usage of the program keeps on increasing as the time goes by. (From 20MB to 2gigs).
The following is the code that I am using.
Is there any optimization i can perform to keep the memory under a limit and say the unused objects gets deleted periodically.
void getMatchInfo_Work(object sender, DoWorkEventArgs e)
{
HttpClient client = new HttpClient();
DataConnection dc = new DataConnection();
dc.Configuration.AutoDetectChangesEnabled = false;
dc.Configuration.ValidateOnSaveEnabled = false;
do{
do
{
using (Stream s = client.GetStreamAsync("http://api.steampowered.com/IDOTA2Match_570/GetMatchHistoryBySequenceNum" +
"/v1/?key="+devkey+"&start_at_match_seq_num=" + matchseq.ToString()).Result)
using (StreamReader sr = new StreamReader(s))
using (JsonReader reader = new JsonTextReader(sr))
{
JsonSerializer serializer = new JsonSerializer();
MatchMain match;
MatchDesc player;
RootElement matchSeq = serializer.Deserialize<RootElement>(reader);
if (matchSeq.result.status == 1)
{
foreach (var m in matchSeq.result.matches)
{
match = new MatchMain();
match.MatchID = m.match_id;
match.Win = m.radiant_win;
match.Duration = m.duration;
match.StartTime = ConvertFromUnixTimestamp(m.start_time);
match.TimeStamp = m.start_time;
match.SeqNo = m.match_seq_num;
matchseq = m.match_seq_num + 1;
match.RadTower = m.tower_status_radiant;
match.DireTower = m.tower_status_dire;
match.RadRax = m.barracks_status_radiant;
match.DireRax = m.barracks_status_dire;
match.cluster = m.cluster;
match.FBTime = m.first_blood_time;
match.LobbyType = m.lobby_type;
match.HumanCount = m.human_players;
match.LeagueID = m.leagueid;
match.GameMode = m.game_mode;
dc.MatchMain.Add(match);
count++;
foreach (var p in m.players)
{
player = new MatchDesc();
player.MatchID = m.match_id;
player.AccountID = p.account_id;
player.PlayerSlot = p.player_slot;
player.HeroID = p.hero_id;
player.First = p.item_0;
player.Second = p.item_1;
player.Third = p.item_2;
player.Fourth = p.item_3;
player.Fifth = p.item_4;
player.Sixth = p.item_5;
player.Kills = p.kills;
player.Deaths = p.deaths;
player.Assists = p.assists;
player.LeaverStatus = p.leaver_status;
player.Gold = p.gold;
player.LastHits = p.last_hits;
player.Denies = p.denies;
player.GPM = p.gold_per_min;
player.XPM = p.xp_per_min;
player.GoldSpent = p.gold_spent;
player.HeroDamage = p.hero_damage;
player.TowerDamage = p.tower_damage;
player.Healing = p.hero_healing;
player.HeroLevel = p.level;
dc.MatchDesc.Add(player);
}
getMatchInfo.ReportProgress(1);
}
dc.SaveChanges();
}
else
{
result = 0;
}
}
} while (result == 1);
Thread.Sleep(60000);
}while( result>-999);
}
void getMatchInfo_Progress(object sender, ProgressChangedEventArgs e)
{
if (result != 0)
{
txtProgress.Text = count + " matches updated in database and continuing.";
}
else
{
txtProgress.Text = count + " matches updated in database and waiting for API.";
}
txtTime.Text =sw.Elapsed.Hours.ToString()+" hours "+ sw.Elapsed.Minutes.ToString()+" minutes "+
sw.Elapsed.Seconds.ToString()+" seconds";
}
Edit: Button click code
private void btnRetrieveData_Click(object sender, RoutedEventArgs e)
{
getMatchInfo.DoWork += new DoWorkEventHandler(getMatchInfo_Work);
getMatchInfo.ProgressChanged += new ProgressChangedEventHandler(getMatchInfo_Progress);
getMatchInfo.WorkerReportsProgress = true;
txtProgress.Text = "Process started";
matchseq = Convert.ToInt64(txtSeqNo.Text);
devkey = txtKey.Text;
sw.Start();
getMatchInfo.RunWorkerAsync();
}

DataConnection context keeps tracking all objects that it ever saw. Put creation of dc inside the loop and dispose it every time. You don't need the context after each SaveChanges is called.

Related

Entity saves ok but will not update record no matter what I do

Context is not saving to the database no matter what i do it will insert a new record fine but not save. This is using sql server and the user had permissions ot update data have already checked this
private void btnOk_Click(object sender, EventArgs e)
{
SourceContext SourceDal = new SourceContext();
Appointment _appointment = new Appointment();
int errorCount = 0;
Patient _patient = new Patient();
_patient = SourceDal.getPatientByPatientId(txtPatientId.Text);
_patient.SSN = txtSSN.Text;
_patient.FirstName = txtPatientFirstName.Text;
_patient.LastName = txtPatientLastName.Text;
_patient.Middle = txtPatientMiddle.Text;
_patient.AddressOne = txtPatientAddressOne.Text;
_patient.City = txtPatientCity.Text;
_patient.State = txtPatientState.Text;
_patient.ZipCode = txtPatientZip.Text;
_patient.HomePhone = txtPatientHomePhone.Text;
_patient.WorkPhone = txtPatientWorkPhone.Text;
_patient.CellPhone = txtPatientCellPhone.Text;
if (rBtnHomePhone.Checked == true)
_patient.ApptPhone = txtPatientHomePhone.Text;
if (rBtnHomePhone.Checked == true)
_patient.ApptPhone = txtPatientHomePhone.Text;
if (rBtnWorkPhone.Checked == true)
_patient.ApptPhone = txtPatientWorkPhone.Text;
_patient.BirthDate = dtBirthDate.DateTime;
_patient.emailAddress = txtPatientEmail.Text;
_patient.Race = (int)dpRace.SelectedValue;
_patient.Ethnicity = (int)dpEthnicity.SelectedValue;
_patient.Language = (int)dpLanguages.SelectedValue;
_patient.AlertNote = txtPatientNotes.Text;
if (dpGender.Text == "")
{
dpGender.Focus();
errorCount = 1;
lblGenderRequired.Text = "* Gender is required.";
}
else
{
errorCount = 0;
lblGenderRequired.Visible = false;
}
_patient.Gender = dpGender.Text.Substring(0, 1);
_patient.PatientID = txtPatientId.Text;
txtPatientFirstName.Text = _patient.FirstName;
txtPatientLastName.Text = _patient.LastName;
// IF ITS SAVE NEW GO AHEAD ADD IT TO THE CONTEXT.
SourceDal.AddToPatient(_patient);
}
Add to paitent has the following
public void AddToPatient(Patient newPatient)
{
using (var myContext = new SMBASchedulerEntities(this.Connectionstring))
{
myContext.Patients.Add(newPatient);
if (newPatient.ID == 0)
{
myContext.Entry(newPatient).State = EntityState.Added;
}
else
{
myContext.Entry(newPatient).State = EntityState.Modified;
}
try
{
myContext.SaveChanges();
}
catch (DbEntityValidationException ex)
{
foreach (var entityValidationErrors in ex.EntityValidationErrors)
{
foreach (var validationError in entityValidationErrors.ValidationErrors)
{
Console.Write("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage);
}
}
}
}
}
It adds in the record fine but it just wont save the current record no matter what i do even though all the details are correct. But when i reload the form and the application the update is not there the email address is not saved no are any the other updates.
I suspect I'm not familiar with that entity framework, as I'm unfamiliar with the some of that syntax, but you should be able to use something like this:
public void AddToPatient(Patient newPatient)
{
SMBASchedulerEntities dbContext = new SMBASchedulerEntities();
if (newPatient.ID.ToString() != "0")
{//Update the record
Patient updatePatient = dbContext.Patients.Single(p => p.ID == newPatient.ID);
updatePatient.FirstName = newPatient.FirstName;
updatePatient.LastName = newPatient.LastName;
...
...
dbContext.SubmitChanges();
}
else
{//Insert a new record
Patient insertPatient = new Patient();
insertPatient.FirstName = newPatient.FirstName;
insertPatient.LastName = newPatient.LastName;
...
...
dbContext.Patients.InsertOnSubmit(insertPatient);
dbContext.SubmitChanges();
}
}
To put this another way, check to see if you need to insert or update a new patient first, before inserting it every time.

How to broadcast an NBitcoin Transaction with multiple TxIn ( transaction inputs)

I am running on TestNet. I am trying to add multiple transaction inputs TxIn() so as to spend from these inputs. When I broadcast my transaction, it returns successful but I can't see it on the block-explorer. I have handled a transaction with a single TxIn() and when I broadcast it, it was successful and i could view it on the block-explorer. I have been on this for more than 2 days now. Will greatly appreciate your help guys
var bitcoinPrivateKey = new BitcoinSecret("cNZupUgfs54DmsShwxa1wpomQcszUtuJYFvx9zWPbXrT7KsWtiUd");
var network = bitcoinPrivateKey.Network;
var address = bitcoinPrivateKey.GetAddress();
var client = new QBitNinjaClient(network);
var balance = client.GetBalance(address).Result;
var transactionId = uint256.Parse("06c0aec7543467951abad0c28998a2c1fc1cdc34e01113f8ec1fdb22be854771");
var transactionResponse = client.GetTransaction(transactionId).Result;
var tx = new Transaction();
foreach (var operation in balance.Operations)
{
OutPoint spendOutPoint = null;
var coinsReceived = operation.ReceivedCoins;
foreach (var coin in coinsReceived)
{
if (coin.TxOut.ScriptPubKey == bitcoinPrivateKey.ScriptPubKey)
{
spendOutPoint = coin.Outpoint;
tx.Inputs.Add(new TxIn()
{
PrevOut = spendOutPoint
});
}
}
}
var chimaTestDestinationAddress = new BitcoinPubKeyAddress("mxgN2AiqHjKfGvo6Y57fAe4Y754rPdKf4P");
TxOut chimaTestDestinationAddressTxOut = new TxOut()
{
Value = new Money((decimal)0.50, MoneyUnit.BTC),
ScriptPubKey = chimaTestDestinationAddress.ScriptPubKey
};
TxOut ugoChangeBackTxOut = new TxOut()
{
Value = new Money((decimal)2.98, MoneyUnit.BTC),
ScriptPubKey = bitcoinPrivateKey.ScriptPubKey
};
tx.Outputs.Add(chimaTestDestinationAddressTxOut);
tx.Outputs.Add(ugoChangeBackTxOut);
var msg = "ugo the jedi master";
var msgBytes = Encoding.UTF8.GetBytes(msg);
TxOut txDesc = new TxOut()
{
Value = Money.Zero,
ScriptPubKey = TxNullDataTemplate.Instance.GenerateScriptPubKey(msgBytes)
};
tx.Outputs.Add(txDesc);
tx.Inputs[0].ScriptSig = bitcoinPrivateKey.PubKey.WitHash.ScriptPubKey;
tx.Inputs[1].ScriptSig = bitcoinPrivateKey.PubKey.WitHash.ScriptPubKey;
tx.Inputs[2].ScriptSig = bitcoinPrivateKey.PubKey.WitHash.ScriptPubKey;
tx.Inputs[3].ScriptSig = bitcoinPrivateKey.PubKey.WitHash.ScriptPubKey;
tx.Inputs[4].ScriptSig = bitcoinPrivateKey.PubKey.WitHash.ScriptPubKey;
tx.Sign(bitcoinPrivateKey, false);
BroadcastResponse broadcast = client.Broadcast(tx).Result;
if (!broadcast.Success)
{
Console.WriteLine("ErrorCode: " + broadcast.Error.ErrorCode);
Console.WriteLine("Error message: " + broadcast.Error.Reason);
}
else
{
Console.WriteLine("Success, you can now checkout the transaction in any block explorer");
Console.WriteLine("Hash: " + tx.GetHash());
}

how to get history info from QC using OTA

Below My Code that gets info of Bug history from QC. I have problem with
AuditPropertyFactoryFilter which does not filter. AuditPropertyFactory has more than thousand rows.
If I comment
var changesList = auditPropertyFactory.NewList(changesHistoryFilter.Text); and uncomment
next line, auditPropertyFactory has several rows only but it isn't filtered as i need.
Can anyone get some advice?
public List<QCBugHistory> retrieveHistoryFromBug(string bugId)
{
List<QCBugHistory> history = new List<QCBugHistory>();
try
{
TDConnection qcConnection = new TDConnection();
qcConnection.InitConnectionEx(qcUrl);
qcConnection.ConnectProjectEx(qcDomain, qcProject, qcLogin);
if (qcConnection.Connected)
{
AuditRecordFactory auditFactory = qcConnection.AuditRecordFactory as AuditRecordFactory;
TDFilter historyFilter = auditFactory.Filter;
historyFilter["AU_ENTITY_TYPE"] = "BUG";
historyFilter["AU_ENTITY_ID"] = bugId;
historyFilter["AU_ACTION"] = "Update";
historyFilter.Order["AU_TIME"] = 1;
historyFilter.OrderDirection["AU_TIME"] = 1;
var auditRecordList = auditFactory.NewList(historyFilter.Text);
log.Info("кол-во в истории " + auditRecordList.Count);
if (auditRecordList.Count > 0)
{
foreach (AuditRecord audit in auditRecordList)
{
QCBugHistory bugHistory = new QCBugHistory();
bugHistory.actionType = audit["AU_ACTION"];
bugHistory.changeDate = audit["AU_TIME"];
AuditPropertyFactory auditPropertyFactory = audit.AuditPropertyFactory;
var changesHistoryFilter = auditPropertyFactory.Filter;
changesHistoryFilter["AP_PROPERTY_NAME"] = "Status";
var changesList = auditPropertyFactory.NewList(changesHistoryFilter.Text);
//var changesList = auditPropertyFactory.NewList("");
if (changesList.Count > 0)
{
foreach (AuditProperty prop in changesList)
{
//prop.EntityID
if (prop["AP_PROPERTY_NAME"] == "Status")
{
bugHistory.oldValue = prop["AP_OLD_VALUE"];
bugHistory.newValue = prop["AP_NEW_VALUE"];
history.Add(bugHistory);
}
}
}
}
}
}
}
catch (Exception e)
{
log.Error("Проблема соединения и получения данных из QC ", e);
}
return history;
}
Try this (in C#):
1) Having only BUG ID param (BUGID below):
BugFactory bgFact = TDCONNECTION.BugFactory;
TDFilter bgFilt = bgFact.Filter;
bgFilt["BG_BUG_ID"] = BUGID; // This is a STRING
List bugs = bgFilt.NewList();
Bug bug = bugs[1]; // is 1-based
bug.History;
2) Having the Bug object itself, just do:
bug.History;

Issue with +CDS AT COMMAND

I'm issue when I get +CDS in AT COMMAND throught c# using SerialPort, any times I get this +CDS truncated, example:
+CDS: 25
0002970C91555868047414212181414094882121814140948830
Why I've this problem, why any times work nice?
I'm starting SerialPort:
public PortCOM(string porta)
: base(porta, 115200, Parity.None, 8, StopBits.One)
{
this.StatusPort = StatusPorta.Ready;
this.DiscardNull = true;
this.ReadTimeout = 21000;
this.RtsEnable = true;
this.DtrEnable = true;
this.ReceivedBytesThreshold = 9;
this.NewLine = "\r\n";
this.ReadBufferSize = 1024;
}
public static void TestPort()
{
var p = new PortCom("COM12");
if (!p.IsOpen)
p.Open();
p.StatusPort = StatusPorta.Ready;
p.DataReceived += new SerialDataReceivedEventHandler(p_DataReceivedSample);
p.PinChanged += new SerialPinChangedEventHandler(p_PinChanged);
p.ErrorReceived += new SerialErrorReceivedEventHandler(p_ErrorReceived);
p.Disposed += new EventHandler((obj, porta) =>
{
Console.WriteLine(((PortaCOM)obj).ToString());
});
if (Console.ReadKey().Key == ConsoleKey.B)
{
p.Close();
p.Dispose();
}
}
static void p_DataReceivedSample(object sender, SerialDataReceivedEventArgs e)
{
var p = (PortaCOM)sender;
try
{
Console.WriteLine(p.ReadExisting());
var sb = new StringBuilder();
sb.Append(p.ReadExisting());
int y = sb.ToString().IndexOf("\r\n");
var stop = Stopwatch.StartNew();
stop.Start();
while (y == -1)
{
sb.Append(p.ReadExisting());
y = sb.ToString().IndexOf("\r\n");
if (stop.Elapsed.TotalSeconds > 10)
break;
}
stop.Stop();
var _retorno = sb.ToString();
var cmt = regCMT.Match(_retorno);
var succ = regSucess.Match(_retorno);
var report = regStatusReport.Match(_retorno);
var erro = regError.Match(_retorno);
#region Resposta
if (cmt.Success)
{
var smss = new SMS();
var source = cmt.Groups[3].Value;
SMS.Fetch(smss, ref source);
var resposta = new Resposta()
{
Mensagem = smss.Message,
Data = smss.ServiceCenterTimeStamp,
Sender = smss.PhoneNumber,
Operadora = p.OperadoraName.NomeOperadora.ToString()
};
GravaResposta().ToAsync(Scheduler.TaskPool).Invoke(p, cmt.Groups[3].Value);
p.IsError = false;
}
#endregion
#region StatusReport
if (report.Success)
{
RecebeReport(p, report.Groups[2].Value.Trim());
p.IsError = false;
}
#endregion
}
catch (Exception err)
{
Console.WriteLine(err.Message);
}
}
Please I really need help with it, I'm glad for any help!
+cds is alert for incoming message with message location on SIM memeory.
so here its seems in PDU mode data. and it is seems may be flash message content.
Convert the data PDU mode to Text mode to receive message.
review this ATSMS library

how to shift paper on printing

We have a big Printer just like FotoCopy Machine. When it print it can group/shift the paper.
for example: paper 1 to 10 --> shift left, 11 to 25 --> shift right, 26 to 30 --> shift left, and so on.
i think i have to make a Threading.Thread.Sleep(5000) .
but i don't know how to implemented into my code:
LeOrdre_BL oOrdr_BL = new LeOrdre_BL();
protected void Page_Load(object sender, EventArgs e)
{
var xtraReport_Pricipal = new LetrVoiture();
if (oOrdr_BL.Get_All_LetrVoit_BL().Count != 0)
{
foreach (var oItem in oOrdr_BL.Get_All_LetrVoit_BL())
{
var xtraReport_Other = new LetrVoiture();
//Performing pre-rendering operations: binding, passing parameters and etc.
Session["IdVoit"] = oItem.LettreVoidID;
Session["Mode"] = oItem.ModlMod;
Session["PortDu"] = oItem.LibPort;
Session["Enlev"] = oItem.LibExpr;
Session["Unite"] = oItem.EnlvUnite;
Session["Colis"] = oItem.NbrColis;
Session["Poids"] = oItem.LePoids;
Session["LeCR"] = oItem.EnlvCrmb;
Session["LeVD"] = oItem.EnlvDecl;
Session["Code_Exp"] = oItem.ClientID;
Session["RS_Nom_Exp"] = oItem.RsNomExp;
Session["Addr_Exp"] = oItem.AddrExp;
Session["Cp_Exp"] = oItem.CpExp;
Session["Ville_Exp"] = oItem.VilleExp;
Session["Tel_Exp"] = oItem.TelExp;
Session["Fax_Exp"] = oItem.FaxExp;
Session["Code_Dest"] = oItem.CodeDest;
Session["RS_Nom_Dest"] = oItem.RaisonSoc;
Session["Addr_Dest"] = oItem.Adresse;
Session["Cp_Dest"] = oItem.Cp;
Session["Ville_Dest"] = oItem.Ville;
Session["Tel_Dest"] = oItem.Tel;
Session["Fax_Dest"] = oItem.Fax;
xtraReport_Other.CreateDocument();
xtraReport_Pricipal.Pages.AddRange(xtraReport_Other.Pages);
}
xtraReport_Pricipal.PrintingSystem.ContinuousPageNumbering = true;
using (MemoryStream ms = new MemoryStream())
{
try
{
PdfExportOptions opts = new PdfExportOptions();
opts.ShowPrintDialogOnOpen = true;
xtraReport_Pricipal.ExportToPdf(ms, opts);
ms.Seek(0, SeekOrigin.Begin);
byte[] report = ms.ToArray();
Page.Response.ContentType = "application/pdf";
Page.Response.Clear();
Page.Response.OutputStream.Write(report, 0, report.Length);
//Page.Response.End();
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
finally
{
ms.Close();
}
}
}
//Response.AppendHeader("REFRESH", "4;URL=Ordre.aspx");
}
Thanks you in advance,
Stev

Categories

Resources