Benim şu kodlarım içerisinde MainWindows Loaded metodumda diğer metodları sırayla…
- User
Benim şu kodlarım içerisinde MainWindows Loaded metodumda diğer metodları sırayla çağırıyoruz ve biri bitmeden diğeri çalışmaması için await ekledim ancak await MountBootImage(1); metodu çalıştığında bu bitmeden await TpmBypass(1); metodu da çalışıyor using Microsoft.Dism; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Configuration; using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; using System.Threading.Tasks; using System.Windows; using Wpf.Ui.Controls; using MessageBox = System.Windows.MessageBox; using Path = System.IO.Path;
namespace CleanOS_Builder { public class AppSettings { public int TPM { get; set; } }
public partial class MainWindow : FluentWindow { private AppSettings _appSettings; private Dictionary<string, Func<Task>> _methodDictionary; private Dictionary<string, string> _descriptionToPropertyMap = new Dictionary<string, string> { { "Disabling TPM/Secure Boot/Ram Requirements", "TPM" }, }; public MainWindow() { InitializeComponent(); this.Loaded += async (sender, e) => await MainWindow_Loaded(sender, e); var json = File.ReadAllText("settings.json"); var settings = JsonConvert.DeserializeObject<Dictionary<string, int>>(json); _appSettings = new AppSettings(); foreach (var setting in settings) { if (_descriptionToPropertyMap.TryGetValue(setting.Key, out var propertyName)) { var property = typeof(AppSettings).GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); if (property != null) { property.SetValue(_appSettings, setting.Value); } } } _methodDictionary = new Dictionary<string, Func<Task>> { { "Disabling TPM/Secure Boot/Ram Requirements", () => TpmBypass(1) }, }; } private async Task MainWindow_Loaded(object sender, RoutedEventArgs e) { //await ExtractIso(); foreach (var method in _methodDictionary) { var description = method.Key; if (_descriptionToPropertyMap.TryGetValue(description, out var propertyName)) { var property = typeof(AppSettings).GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); if (property != null) { int propertyValue = (int)property.GetValue(_appSettings); if (propertyValue == 1) { // TPM özelliği 1 ise ve Boot Image'ı monte etme özelliği sorgulanıyorsa, TPM özelliğinin de 1 olup olmadığına bakılır. if (propertyName == "BootImageMonteEt" && _appSettings.TPM == 0) { continue; } await method.Value.Invoke(); } } } } MountingBootImageText.Visibility = Visibility.Visible; TPMProgress.Visibility = Visibility.Visible; UnmntProgress.Visibility = Visibility.Visible; await MountBootImage(1); await TpmBypass(1); //await UnmountBootImage(1); //await MountBootImage(2); //await TpmBypass(2); //await UnmountBootImage(2); MountingBootImageText.Visibility = Visibility.Hidden; TPMProgress.Visibility = Visibility.Hidden; UnmntProgress.Visibility = Visibility.Hidden; MountingBootImageIcon.Visibility = Visibility.Visible; TPMSuccesIcon.Visibility = Visibility.Visible; UnmntSuccesIcon.Visibility = Visibility.Visible; } public async Task ExtractIso() { EnsuringControlText.Visibility = Visibility.Visible; string isoPath = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.iso").FirstOrDefault(); if (isoPath == null) { MessageBox.Show("You need to add your ISO File to the directory where the application is running!"); Environment.Exit(0); } string extractPath = Path.Combine(Directory.GetCurrentDirectory(), "Extracted"); if (!Directory.Exists(extractPath)) { Directory.CreateDirectory(extractPath); } await RunCommandAsync($"-Command Mount-DiskImage -ImagePath '{isoPath}'"); DriveInfo[] allDrives = DriveInfo.GetDrives(); DriveInfo mountedDrive = null; while (mountedDrive == null) { foreach (DriveInfo d in allDrives) { if (d.DriveType == DriveType.CDRom && d.IsReady) { mountedDrive = d; break; } } await Task.Delay(2000); } await CopyAll(new DirectoryInfo(mountedDrive.Name), new DirectoryInfo(extractPath)); await RunCommandAsync($"-Command Dismount-DiskImage -ImagePath '{isoPath}'"); EnsuringControlText.Visibility = Visibility.Hidden; EnsuringControlIcon.Visibility = Visibility.Visible; } public static async Task CopyAll(DirectoryInfo source, DirectoryInfo target) { Directory.CreateDirectory(target.FullName); foreach (FileInfo fi in source.GetFiles()) { using (Stream sourceStream = fi.OpenRead()) { using (Stream destinationStream = File.Create(Path.Combine(target.FullName, fi.Name))) { await sourceStream.CopyToAsync(destinationStream); } } } foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) { DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name); await CopyAll(diSourceSubDir, nextTargetSubDir); } } private static Task RunCommandAsync(string command) { var tcs = new TaskCompletionSource<bool>(); var process = new Process { StartInfo = new ProcessStartInfo { FileName = "powershell", Arguments = command, UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true }, EnableRaisingEvents = true }; process.Exited += (sender, args) => { if (process.ExitCode == 0) { tcs.SetResult(true); } else { tcs.SetException(new Exception($"Command `{command}` failed with exit code `{process.ExitCode}`")); } }; process.Start(); return tcs.Task; } public async Task MountBootImage(int index) { string mountPath = @"Mount\Boot" + index; // Her index için farklı bir mount path oluşturun string wimPath = Path.Combine(Directory.GetCurrentDirectory(), @"Extracted\sources\boot.wim"); if (!Directory.Exists(mountPath)) { Directory.CreateDirectory(mountPath); } DismApi.Initialize(DismLogLevel.LogErrors); try { await Task.Run(() => DismApi.MountImage(wimPath, mountPath, index, false)); } catch (Exception ex) { Console.WriteLine("Hata: " + ex.Message); } finally { DismApi.Shutdown(); } } public async Task TpmBypass(int index) { string path = AppDomain.CurrentDomain.BaseDirectory + "Mount\\Boot" + index + "\\"; // Her index için farklı bir path oluşturun string commands = @"reg load HKLM\OFF_SYSTEM {0}Windows\System32\config\SYSTEM - Assistant
Gönderdiğiniz kodda,
MainWindow_Loadedmetodu içinde bazı asenkron işlemleri sıralı olarak yürütüyorsunuz. Ancak kodun içinde bazı sorunlar var gibi görünüyor. İki asenkron çağrıMountBootImage(1);veTpmBypass(1);arasınaawaiteklemişsiniz. AncakMountBootImagemetodunuz içinde kullanarak asenkron olarak çağırıyorsunuz ve bu çağrı tamamlandığında bir hata olmamasını varsayarak diğer metodunuzu çağırıyorsunuz.