Link to home
Start Free TrialLog in
Avatar of itnifl
itniflFlag for Norway

asked on

Querying the status of an object in C# MVC from jQuery

I have the following method in a C# MVC controller that gets called by jQuery Ajax:
[HttpGet]
      public void SyncLDAPUsers() {
         Task.Factory.StartNew(() => {
            lEngine.SyncUsersToDatabase();
         });
      }
      [HttpGet]
      public ActionResult GetLDAPSyncItemsOverview() {
         return Json(new { SyncMaxItems = lEngine.SyncMaxItems, SyncCurrentItemCount = lEngine.SyncCurrentItemCount, SyncStatus = lEngine.SyncStatus, SyncedNameList = lEngine.SyncCurrentNames }, JsonRequestBehavior.AllowGet);
      }

Open in new window

SyncLDAPUsers gets called first and starts the sync of ldap users to the local database. Then I have a deferred javascript function, that polls via setInterval on to GetLDAPSyncItemsOverview every 4 seconds. It is supposed to retrieve the sync status and update the UI with the names that have been synced (SyncedNameList).  But the list is always empty, and SyncMaxItems and also SyncCurrentItemCount is always the initial value 0. I know that lEngine.SyncUsersToDatabase() is running, I can step through the execution and the database gets updated. But it seems that each time when I query the controller with ajax, then lEngine is reinitialized to a different instance of itself while the original lEngine is doing it's job.

I would like to start an instance of lEngine that I can query the status of, so that I can inform via the UI how the job is going.
SOLUTION
Avatar of Jatin Prajapati
Jatin Prajapati
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Mlanda T
Mlanda T
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of itnifl

ASKER

Setting lEngine as static is probably the easiest to do. I have looked at SignlaR before, and decided to do it as easy as possible instead. But I bet SignlarR is way cooler. Something like socket.io, which I am used to.