Skip to content

Commit 604bae2

Browse files
author
Ariel Silahian
committed
vmChartStudy race condition fixed
1 parent 54a2cac commit 604bae2

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

ViewModel/vmChartStudy.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,23 @@ private void _study_OnCalculated(object? sender, BaseStudyModel e)
140140
}
141141

142142
private void QUEUE_onReadAction(BaseStudyModel item)
143+
{
144+
var dispatcher = System.Windows.Application.Current?.Dispatcher;
145+
if (dispatcher == null || dispatcher.CheckAccess())
146+
{
147+
ProcessQueueItem(item);
148+
return;
149+
}
150+
151+
dispatcher.BeginInvoke(new Action(() => ProcessQueueItem(item)), System.Windows.Threading.DispatcherPriority.Background);
152+
}
153+
154+
private void ProcessQueueItem(BaseStudyModel item)
143155
{
144156
try
145157
{
146158
lock (_LOCK)
147159
{
148-
// Check if we're disposed or clearing
149160
if (_disposed || _seriesMarket == null || _dataByStudy == null || _seriesByStudy == null)
150161
return;
151162

@@ -159,26 +170,22 @@ private void QUEUE_onReadAction(BaseStudyModel item)
159170
if (item.MarketMidPrice > 0)
160171
_lastMarketMidPrice = (double)item.MarketMidPrice;
161172

162-
// Reuse PlotInfo if possible (avoid allocation)
163173
var pointToAdd = _plotInfoPool.Get();
164174
pointToAdd.Date = item.Timestamp;
165175
pointToAdd.Value = (double)item.Value;
166176

167177
bool isAddSuccess = _dataByStudy[keyTitle].Add(pointToAdd);
168178

169-
// If successfully added, proceed with adding it into the series
170179
if (isAddSuccess)
171180
{
172181
double oaDate = pointToAdd.Date.ToOADate();
173182

174183
if (item.IsIndependentMetric)
175184
{
176-
// Update only the matching series for independent metrics
177185
UpdateIndependentSeries(keyTitle, oaDate, pointToAdd, item);
178186
}
179187
else
180188
{
181-
// Update all series for synchronized metrics (backward compatible)
182189
UpdateAllSynchronizedSeries(keyTitle, oaDate, pointToAdd, item);
183190
}
184191
}

0 commit comments

Comments
 (0)