My app crashes after several keypresses I make. It's not what I need, of course. Has it something to do with Garbage Collector? I searched online, but I still don't understand exactly how to manage this. How to fix this?
using System;
using System.Linq;
using System.Windows;
using System.Diagnostics;
using System.Runtime.InteropServices;
using FlaUI.UIA3;
using Application = FlaUI.Core.Application;
using AutoHotkey.Interop;
namespace random
{
public partial class MainWindow : Window
{
delegate void Test();
public MainWindow()
{
var program = Process.GetProcessesByName("notepad").First();
var app = Application.Attach(program.Id);
var mainWindow = app.GetMainWindow(new UIA3Automation());
var ahk = AutoHotkeyEngine.Instance;
Test rand = () => ahk.ExecRaw($"send {(new Random()).Next(100, 200)}");
ahk.ExecRaw("#IfWinActive ahk_exe notepad.exe");
ahk.ExecRaw($"RCtrl::DllCall({Marshal.GetFunctionPointerForDelegate(rand)})");
InitializeComponent();
}
}
}