Search code examples
asp.netsql-serveriframecancellationlong-running-processes

Cancel long running IFRAMEs


I have an ASP.NET WebForm application - a main page that hosts a bunch of IFRAME "widgets". When main page loads - the widgets' pages load as well and begin their own processing, which include connecting to SQL Server to run stored procedures.

What would be the best way to cancel those IFRAME processing should user need to do so? I could probably set their SRC to another/blank page from client-side thus canceling the request, but if the IFRAME pages already called SQL Server - how do I cancel the SQL processing as well?

EDIT: To clarify the slowest point in the IFRAMEd page is the SQL Server stored procedure call, so essentially this boils down to - can I initiate cancellation of the SQL command from client?


Solution

  • One solution that came up in my mind is to have a static List of currently Running SqlCommands, whenever user invokes cancel you can extract the Command from this static list, and then cancel it. The page that is executing the command will receive the command cancel exception, it then can handle it appropriately.

    Here are the things that you need to handle:

    1. Somehow user interface should have some key, so that when you press the cancel button, user interface will then send that key to the cancel routine, and cancel routine will extract the SqlCommand using that key and call its Cancel method.
    2. Cancellation calls should be done through AJAX
    3. Proper locking should be done for thread-safety

    Update:

    I am adding some details here,

    1. Here is the link of Project http://goo.gl/noKUmj
    2. Above project is a Web Form Application
    3. You may add as many controllers as you want in this project, the routing code has already been added in Global.ascx, so new controllers should work
    4. But for new IFRAME cancellation you don't have to add another controller. You just need to pass the PageName (as key) to the Cancel Controller
    5. LIMITATION: This project has limitation that if you hit cancel button for an IFRAME, it is going to cancel all the current requests, which means in a multi-user environment, if one user cancels the IFRAME-1, all IFRAME-1 are going to be cancelled for every other user.
    6. You can cancel multiple IFRAMEs easily, as you can see the cancel button code it is executing a JavaScript code, you just need to call multiple Cancellation Codes in 1 click.

    As you may have already seen the project, it is using PageName as Key to extract the SqlCommand from List. Due to this we have the limitation as I have mentioned above. Since PageName will be same for multiple SqlCommands.

    To overcome this limitation, you can generate a GUID as Key and pass this GUID in QueryString to Form1.aspx and Form2.aspx pages, so that they add the command in List using these GUIDs. These keys should be generated at the time of host page rendering or in JavaScript.

    If you don't want to do the RnD, here is the updated project which uses GUID as Key. :) http://goo.gl/I86S7Z