Example 13: Handling events |
Several objects in the Workspaces .NET SDK generate events when certain things happen such as when a user has successfully been authenticated or when an file upload has completed. The following examples show how to make use of these events.
Classes that generate events:
ApiSession
SignInManager
DownloadManager
UploadManager
In this example the SignInCompleteEventHandler method is added to the SignInCompleted event of the SignInManager. Once this has been done, any time a user is successfully authenticated on the instance of the ApiSession class the event will be fired and the SignInCompleteEventHandler method would be called.
public ApiSession InitializeSession (string workspacesServer, string userEmail) { // create a new ApiSession object ApiSession apiSession = new ApiSession(workspacesServer); // get a reference to the SignInManager ISignInManager signInMgr = apiSession.SignInManager; // register our event handler signInMgr.SignInCompleted += SignInCompleteEventHandler; // login the user using OAuth LoginResult loginResult = apiSession.StartSessionWithOAuth(userEmail); // return the ApiSession return apiSession; } private void SignInCompleteEventHandler(object sender, EventArgs eventArgs) { Console.WriteLine("User signin completed"); }
Whenever the authentication token for a user expires the Workspaces .NET SDK will attempt to obtain a new token. If the SDK is unable to create a new token it will fire a SessionExpired event. In this example the SessionExpiredEventHandler method is added to the ApiSession instance.
public ApiSession InitializeSession(string workspacesServer) { // create a new ApiSession object ApiSession apiSession = new ApiSession(workspacesServer); // register our event handler apiSession.SessionExpired += SessionExpiredEventHandler; // return the ApiSession return apiSession; } private void SessionExpiredEventHandler(object sender) { Console.WriteLine("Session expired"); }
When a file is being uploaded it might be good to provide feedback to a user on how the upload is progressing. By providing an event handler to the UploadManager an application could do just that. This example adds the OnUploadProgressChangedEventHandler method to the OnUploadProgressChanged event of an instance of the UploadManager class. At regular intervals the Workspaces .NET SDK will fire this event as a file is being upload.
public void UploadFile(ApiSession apiSession, SubmitDocumentsVdrJson uploadInfo, int workspaceId, string destinationName, string filename) { // get the UploadManager UploadManager upldMgr = apiSession.GetUploadManager(); // register our event handler upldMgr.OnUploadProgressChanged += OnUploadProgressChangedEventHandler; // upload the file upldMgr.UploadDocumentToRoom(uploadInfo, workspaceId, destinationName, filename, null); } private void OnUploadProgressChangedEventHandler( object sender, IFileRequestData fileRequestData, int progress, UploadStatus status) { Console.WriteLine("Uploading " + fileRequestData.LocalPath + ", " + progress + " percent complete."); }