In my app I begin an import process by uploading an XML file. Once it is uploaded, a series of stored procedures are run and the xml file is parsed, and data is inserted into many different tables. As it progresses, a Stats
table is updated to show the progress of the insert. The Stats
table has 4 columns: BatchId
, BatchCount
, ProcessCount
, and ErrorCount
. BatchCount
is the count of all records. If the records are inserted correctly without issue ProcessCount
is incremented by 1, if it fails ErrorCount
is incremented by 1. (These two columns are always changing during the import process)
My main goal is to display the import progress using SignalR. I have a hub method that is polling just fine, as I'm seeing updated timestamps in my UI. The problem however, is that the count is either always 0 (initial value), or very very occasionally it will read once and show something random like 985. If it does read a value, it only does it once and will not change again. Here is my hub method:
public void BeginPolling()
{
while (true)
{
var stats = _repository.GetImportStats();
var message = "Preparing file...";
if (stats != null)
{
message = DateTime.Now + " - Count: " + stats.ProcessCount.ToString();
}
else
{
message = DateTime.Now + " - Stats result returned null.";
}
//the message displays in a div on my UI
Clients.Caller.showProgress(message);
//I have tried various sleep times (1000, 5000, 10000)
Thread.Sleep(5000);
}
}
Below is my _repository.GetImportStats
method, for testing purposes it simply grabs the first (and only) record:
public Stats GetImportStats()
{
return DataContext.Stats.FirstOrDefault();
}
Also for testing, I tried just putting a button on my UI that makes an ajax call to the same GetImportStats()
method, and it always returns the ProcessCount
just fine, so I think the problem lies within my SignalR implementation. Any help is appreciated!