gpt4 book ai didi

c# - WPF c# - 连续执行一个任务并允许执行另一个任务

转载 作者:行者123 更新时间:2023-11-30 15:21:36 24 4
gpt4 key购买 nike

我正在使用以下过程通过 C# 运行宏:

任务 1:

 EXCELApplicationObj.GetType().InvokeMember("Run",
System.Reflection.BindingFlags.Default |
System.Reflection.BindingFlags.InvokeMethod,
null, EXCELApplicationObj, oRunArgs);

但是当这个函数运行时我想通过它的标题名来检查窗口的出现,为此我使用了这个:

任务 2:

Process[] processlist = Process.GetProcesses();
foreach (Process process in processlist)
{
if (!String.IsNullOrEmpty(process.MainWindowTitle))
{
process.ProcessName, process.Id, process.MainWindowTitle);
if (process.MainWindowTitle == "Untitled - Notepad") {
process.Kill();
}
}
}

但问题是我想在此过程中运行此并行,以便我想执行宏,无论何时出现名为“无标题 - 记事本”的新窗口,我都想关闭它。任何 stackoverflowers 都可以给我任何指导吗?听说过线程,但我对线程了解不多..

更新:我在找什么:

连续执行任务 2 并允许执行任务 1。

最佳答案

这是一个在运行某些代码时自动并行关闭窗口的示例:

static void Main() {

// execute AsyncCloseTopWindow in parallel to close a window named "Untitled - Notepad"
var thread = new Thread(AsyncCloseTopWindow);
thread.IsBackground = true;
thread.Start("Untitled - Notepad");

// execute the macro while AsyncCloseTopWindow is running
//...

// exit AsyncCloseTopWindow
thread.Interrupt();
}

private static void AsyncCloseTopWindow(object windowTitle) {
try {
while (true) {

// close any window matching the title
IntPtr hwnd = FindWindow(IntPtr.Zero, (string)windowTitle);
if (!hwnd.Equals(IntPtr.Zero)) {
SendMessage(hwnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}

// wait 30ms
Thread.Sleep(30);
}
} catch (ThreadInterruptedException) { }
}

private const UInt32 WM_CLOSE = 0x0010;

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr FindWindow(IntPtr lpClassName, string lpWindowName);

[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

关于c# - WPF c# - 连续执行一个任务并允许执行另一个任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37179696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com