Inital Requrement: Need to update some data based on schedule basis.

 

One Solution: Develop a dotnet console app and attached it to Windows schedular and set a fixed time for execution.
 
It is working and everying is fine.
 
New requirement: Client wants to execute schedular functionality on demand.
 
One Solution: Create a new User Interface(.aspx(aspx.cs)) and add link/button for capture user action and start exe file which is executed by schedular.
 
If you search on line with title like "Execute any executable from asp.net" Most of the time you see the following code
 
try
{
 
       // Create An instance of the Process class responsible for starting the newly process.
      System.Diagnostics.Process process1 = new System.Diagnostics.Process();
 
       // Set the filename name of the file you want to execute/open
       process1.StartInfo.FileName = AppConfig.EXE_PATH_WITH_NAME; 
 
       process1.StartInfo.Arguments = OPLM.Common.Util.ShedulerRunningType.Manual.ToString();//Argument Passing
 
       process1.Start();// Start the process without blocking the current thread
 
       Thread.Sleep(20000);//Assume within 20 seconds it will finish processing.
 
       //process.WaitForExit method is there you may wait after close finished that executable
 
       process.Close();
}
catch(Exception ex)
{
      Logger.log(ex.Message)
}
 
Issue with above code:
        * If you try to catch exception which was raised in Executable you never catch that. Because it is completely independent process.
 
New AppDomain creation and execution inside that is one of the the dotnet solution for that. Actually Appdomain is a isolated environment which is created inside executing Assembly and any time we can destroy it. (To know in details about AppDomain please search on msdn/or other resources.)
 
try
{
      //Create a new appdoamin for execute my exe in a isolated way.
      AppDomain sandBox = AppDomain.CreateDomain("sandBox");
      try
      {
          //Exe executing with arguments
          sandBox.ExecuteAssembly(OPLMConfig.SPEC_TABLE_SCHEDULER_PATH, null, new string[]{OPLM.Common.Util.ShedulerRunningType.Manual.ToString()});
      }
      finally
      {
          AppDomain.Unload(sandBox);//destry created appdomain and memory is released.
       }
}
catch(Exception ex)//Any exception that generate from executable can handle
{
    Logger.log(ex.Message)
}
 
Limitation of this approach.
      *The executable must be dotnet assembly. No native exe will work there.
 

 

Comments

romel's picture

Can the following approach be possible ? 
 

  1. First calling the Dot.Net Assembly from ASP.Net using any of the approaches above.
  2. Then issueing a native assembly execute command from that Dot.net assembly. More specifically with DllImport
habib's picture

Thanks for your comments. In free time i will try to implement your suggested technique or if you free then you can and give me feedback.

But problem might be if i use that inside my dotnet managed code and if any exception raised inside that native/com component then i can not catch that exception due to it is completly different execution environment then managed execution environment.

If any one get free time he/she can explore that and give us feedback that it is possible or not or may some indetail information.

 

romel's picture

Intsead of making the thread to sleep for 20 secs, can't we just use the Process.Exit event callback process? Because for some prcess it may take more than 20 secs. So we run in a risk of closing a process in the middle of an incomplete - which could disastrous.

habib's picture

Best way to use Process.WaitForExit method. It will block the current working thread untill the process is finished. But if the senario like I can wait maximum 10 sec for finish that process then WaitForExit has wait argument where we can pass timeinterval. Within that time period it must be finish otherwise. It is like timeout argument of any component which expose execution timeout argument. I see some people use that Thread.Sleep method instead of that. But personally I do not recommended to use Thread.Sleep in this contex. Again thanks for your comment!

Add new comment