0

I am currently developing a .NET MAUI Blazor hybrid application for Windows and have a requirement to track user activity in the Outlook app, specifically, to identify whether the user is spending time on reading mails or replying to them. resently I'm able to monitor the user actively use outlook or not but unable to find the user rear or write any mail when use outlook, please help me to achive this.

So far, I've been able to calculate the focus time of the app using the following code: I create a WindowsApplicationMonitor.cs class which help me to Identify user actively using outlook or not.

using System;
using System.Runtime.InteropServices;
using MAUIDemo.Services;

namespace MAUIDemo.PlatformServices.Windows
{
    public class WindowsApplicationMonitor : IApplicationMonitor
    {
        [DllImport("user32.dll")]
        static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll")]
        static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

        private DateTime? outlookStartTime;

        public bool IsApplicationActive(string applicationName)
        {
            System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName(applicationName);

            if (processes.Length > 0)
            {
                // Get the handle of the active window
                IntPtr hWnd = GetForegroundWindow();

                // Get the process ID associated with the active window
                uint processId;
                GetWindowThreadProcessId(hWnd, out processId);

                // Check if the active window belongs to the application
                return processId == processes[0].Id;
            }
            else
            {
                return false;
            }
        }

        public TimeSpan GetApplicationActiveTime(string applicationName)
        {
            if (IsApplicationActive(applicationName))
            {
                if (outlookStartTime == null)
                {
                    outlookStartTime = DateTime.Now;
                }

                return DateTime.Now - outlookStartTime.Value;
            }
            else
            {
                TimeSpan elapsed = TimeSpan.Zero;

                if (outlookStartTime != null)
                {
                    elapsed = DateTime.Now - outlookStartTime.Value;
                    outlookStartTime = null;
                }

                return elapsed;
            }
        }
    }
}

In the .razor page of my .NET MAUI Blazor project, I have the following: here the data will be update when user goes out of focus from outlook.

@page "/nonitor"

@inject MAUIDemo.Services.IApplicationMonitor ApplicationMonitor
@using System.Timers
@using MAUIDemo.Services

@inject MAUIDemo.Services.IApplicationMonitor ApplicationMonitor
@inject MAUIDemo.Services.ActivePeriodService ActivePeriodService
@using System.Timers


<h3>Outlook Status</h3>
<button class="btn btn-primary" @onclick="ClearList">Clear List</button>


<p>@statusMessage</p>

<table class="table">
    <thead>
        <tr>
            <th>Start Time</th>
            <th>End Time</th>
            <th>Duration (hh:mm:ss)</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var period in activePeriods)
        {
            <tr>
                <td>@period.Start</td>
                <td>@period.End</td>
                <td>@String.Format("{0:D2}:{1:D2}:{2:D2}", period.Duration.Hours, period.Duration.Minutes, period.Duration.Seconds)</td>
            </tr>
        }
    </tbody>
</table>

@code {
    private string statusMessage = "";
    //private List<ActivePeriod> activePeriods = new List<ActivePeriod>();
    private List<ActivePeriod> activePeriods => ActivePeriodService.ActivePeriods;
    private ActivePeriod currentPeriod;
    private Timer timer;

    protected override void OnInitialized()
    {
        timer = new Timer(1000); // Update every second
        timer.Elapsed += UpdateStatus;
        timer.Start();
    }

    private bool lastStatus = false; // keeps track of the last known status of the Outlook application (whether it was active or not).

    private void UpdateStatus(Object source, ElapsedEventArgs e)
    {
        var isOutlookActive = ApplicationMonitor.IsApplicationActive("OUTLOOK");

        if (isOutlookActive != lastStatus) // Only update the status message and UI when the status changes
        {
            statusMessage = isOutlookActive ? "Outlook is active." : "Outlook is not active.";

            if (isOutlookActive)
            {
                if (currentPeriod == null)
                {
                    currentPeriod = new ActivePeriod { Start = DateTime.Now };
                }
            }
            else
            {
                if (currentPeriod != null)
                {
                    // Create a new instance when adding to the list
                    activePeriods.Add(new ActivePeriod { Start = currentPeriod.Start, End = DateTime.Now });
                    currentPeriod = null; // This will ensure a new instance is created each time
                }
            }

            InvokeAsync(() => StateHasChanged()); // Update the UI
        }

        lastStatus = isOutlookActive; // Remember the last status for the next time
    }




    public void Dispose()
    {
        timer?.Dispose();
    }

    public class ActivePeriod
    {
        public DateTime Start { get; set; }
        public DateTime End { get; set; }
        public TimeSpan Duration => End - Start;
    }

    void ClearList()
    {
        ActivePeriodService.ActivePeriods.Clear();
    }

}

I want to monitor the user read or write email whenever user actively use outlook.

1 Answer 1

0

You need to use the Outlook Object Model (Outlook.Application object). You can then subscribe to various Outlook events that fire when the user selects a message (Explorer.SelectionChange), opens a message (new or reply in an inspector - Application.Inspectors.NewInspector), sends a message (Application.ItemSend), etc.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.