SoFunction
Updated on 2025-03-06

C# Sample Tutorial for Multi-process Opening of PPT

1. Background

The PPT file opening and operation are carried out in one process. If multiple PPTs are operated, the PowerPoint process will be carried out in blocking order by default. If the opened PPT is particularly large (for example, more than 1GB), it is easy to cause the PPT to be unresponsive, so almost all PPT operations cannot be carried out.

One way to solve the problem of unresponsive PPT is to detect whether the PPT process() is unresponsive. If it is unresponsive, the process will be Killed and the PPT will be reopened. This method cannot solve the problem of requiring multiple PPT operations. If multiple PPT files are large, operating multiple PPTs will frequently cause PPT unresponsiveness.

Another way to solve PPT unresponsiveness is to open PPT using a multi-process approach. By default, PPT cannot be opened with multiple processes. When using different users to open PPT, different PPT processes can be opened. See the reference document for the reasons.

2. Specific methods

The method to open PPT with multiple users first requires built-in different users, which can be implemented through code (requires administrator privileges), and can also add specified users through Windows user settings.Use code to operate users will have reference code later

The second is to start the process of encapsulating the PPT operation using the specified user (and password), and then perform PPT control (open, page turn, jump, media control, etc.) through inter-process communication (pipeline, COPYDATA message, MQTT, recommended pipeline or COPYDATA message, without relying on third-party libraries).

3. Reference code

(1) Use the code snippet of the process to open the specified user (the args parameter can pass the pipeline name or custom message)

public void StartProcess(string fileName, string userName, string password, string args)
{
 var pwd = new SecureString();
 if (!(password))
 {
  ().ToList().ForEach(c => (c));
 }

 try
 {
  var process = new Process
  {
   StartInfo =
   {
    UseShellExecute = false,
    LoadUserProfile = true,
    UserName = userName,
    Password = pwd,
    Domain = ".",
    FileName = $"\"{fileName}\"",
    Arguments = args
   }
  };
  ();
 }
 finally
 {
  ();
 }
}

(2) PPT operation segment

private void OpenPpt()
{
 var app = new ();
}

private void ClosePpt(ref Application app)
{
 try
 {
  app?.Quit();
 }
 catch (Exception e)
 {
  ();
 }

 app = null;
 ();
}

public void Open(object app, string filePath)
{
 var ppt = app as ;
 if (ppt == null)
 {
  return;
 }

 //Replace the forward slash with the backslash filePath = ('/', '\\');

 try
 {
  //Open in read-only mode, convenient to save after the operation is finished  // Open PPT using an impossible value for password (AssemblyGuid) as password, to ensure that it fails when there is a password  var presentation = (
   $"{filePath}::{Password}",
   , //ReadOnly: true
   , //Untitled: true
   ); //WithWindow: false

  //Get the real width and height  var officeWidth = ;
  var officeHeight = ;

  //Get page count  var pageNumber = ;

  var slideShowSettings = presentation?.SlideShowSettings;
  if (slideShowSettings == null)
  {
   return;
  }

  var window = ();
 }
 catch (Exception e)
 {
  ();
 }
}

public void Close(SlideShowWindow window)
{
 try
 {
  window?.();
 }
 catch (Exception e)
 {
  ();
 }
}

public void GotoSlide(SlideShowWindow window, int index)
{
 try
 {
  window?.(index);
 }
 catch (Exception e)
 {
  ();
 }
}

public void Next(SlideShowWindow window)
{
 try
 {
  window?.();
 }
 catch (Exception e)
 {
  ();
 }
}

public void Previous(SlideShowWindow window)
{
 try
 {
  window?.();
 }
 catch (Exception e)
 {
  ();
 }
}

The above is the detailed content of the sample tutorial for opening PPT with C# multi-process. For more information about opening PPT with C# multi-process, please pay attention to my other related articles!