I have a background thread which is processing a lot of data.
The Background Thread uses IProgress in order to report back to the UserInterface (might not be best practice?):
public OrderBook UpdatedOrderBook;
internal async Task CompareQuotes(CancellationToken stop, IProgress<OrderBook> progress = null, IProgress<Exception> errorInterface = null)
{
while (true)
{
UpdatedOrderbook = GetUpdatedOrderbook();
/* Process Data */
progress.Report(UpdatedOrderBook); // uncommenting this line solves memory leak
}
}
The userinterface starts the Background task like this:
public UserInterface()
{
InitializeComponent();
CancelTask = new CancellationTokenSource();
var progressIndicator2 = new Progress<OrderBook>(ReportTriangularArbitrageProgress);
var reportError = new Progress<Exception>(ReportError);
Task botTask = Task.Run(() =>
{
Thread.CurrentThread.Name = "TriangularArbitrageCompareQuotes";
Bot.CompareQuotes(stop: CancelTask.Token,progress: progressIndicator2, errorInterface: reportError);
});
}
The UserInterface updates method is beeing called upon progress.Report. I set return in the first line to see if this will stop the leak but this does not seem to be the case.
void ReportTriangularArbitrageProgress(OrderBook updatedOrderBook)
{
return;
}
As soon as I uncomment progress.Report(MyObject)
from my background task, my memory stops to inflate. (But my ui wont update as well)
What is causing the Memory Leak and GC Pressure? How to prevent it from happening? Is there a better/proper way to update the UI?