How to perform parallel process On Same PST

How to perform parallel process On Same PST
-> Need To use Same PST Path in two Personalstore Object on same time

@HarshP2022

Unfortunately, you cannot work on same PST using different PersonalStorage objects. Could you please share some more detail about your use case? We will then check the possibility of implementation of this feature.

I Want to process the pst as below:

suppose I want to process abc.pst. and PST has a total of 10000 messages. now I want to extract/process those records into 5 instances by using the same pst files, so my processing speed is faster.

@HarshP2022

We have logged a ticket for your case in our issue tracking system as EMAILNET-40656. We will inform you via this forum thread once there is an update available on it.

@HarshP2022

It is possible to open a PST and allow subsequent opening of the file using FileShare.Read parameter.

using var pst = PersonalStorage.FromStream(File.Open(FileName, FileMode.Open, FileAccess.Read, FileShare.Read));

Thus, we can read multiple PersonalStorage instances of the same pst file in parallel as shwon below.

internal class Program
{
    private static readonly string dirName = @"D:\Files\pst";
    private static readonly string FileName = Path.Combine(dirName, "Outlook.pst");

    public static async Task Main()
    {
        // Just for illustration purposes, let's get the IDs of folders that have > 100 messages.
        List<string> foldersWithMessages;

        using (var pst = PersonalStorage.FromFile(FileName))
        {
            foldersWithMessages = GetAllFolders(pst.RootFolder)
                .Where(f => f.ContentCount > 100)
                .Select(f => f.EntryIdString).ToList();
        }

        // For each folder found, run the message retrieving in parallel.
        var task1 = ReadMessagesAsync(foldersWithMessages[0]);
        var task2 = ReadMessagesAsync(foldersWithMessages[1]);
        var task3 = ReadMessagesAsync(foldersWithMessages[2]);
        var task4 = ReadMessagesAsync(foldersWithMessages[3]);
        var task5 = ReadMessagesAsync(foldersWithMessages[4]);

        await task1;
        await task2;
        await task3;
        await task4;
        await task5;
    }

    /// <summary>
    ///     Gets all folders recursively.
    /// </summary>
    /// <param name="rootFolder">The root folder.</param>
    /// <returns></returns>
    private static List<FolderInfo> GetAllFolders(FolderInfo rootFolder)
    {
        var folderList = new List<FolderInfo>();

        foreach (var folder in rootFolder.EnumerateFolders())
        {
            folderList.Add(folder);
            folderList.AddRange(GetAllFolders(folder));
        }

        return folderList;
    }

    /// <summary>
    ///     Reads the messages.
    /// </summary>
    /// <param name="folderId">The folder identifier.</param>
    private static void ReadMessages(string folderId)
    {
        // Open the file for reading and allow subsequent file opening.
        using var pst =
            PersonalStorage.FromStream(File.Open(FileName, FileMode.Open, FileAccess.Read, FileShare.Read));

        var folder = pst.GetFolderById(folderId);

        Console.WriteLine($"Start reading {folder.DisplayName}");

        foreach (var message in folder.EnumerateMapiMessages())
        {
            // Console.WriteLine(message.Subject);
        }

        Console.WriteLine($"End reading {folder.DisplayName}");
    }

    /// <summary>
    ///     Reads the messages asynchronously.
    /// </summary>
    /// <param name="folderId">The folder identifier.</param>
    private static async Task ReadMessagesAsync(string folderId)
    {
        await Task.Run(() => ReadMessages(folderId));
    }
}

It’s Working
Thank You for your Support

@HarshP2022

Please feel free to ask if you have any question about Aspose.Email, we will be happy to help you.