在我的wpf窗口中,我隐藏了窗口并将其从关闭的任务栏中删除。
如何从运行过程中激活该窗口。我尝试了很多方法但没有成功。
这是我激活隐藏窗口的示例代码。
private void checkIfProcessRunning()
{
// get the name of our process
string proc = Process.GetCurrentProcess().ProcessName;
// get the list of all processes by that name
Process[] processes = Process.GetProcessesByName(proc);
// if there is more than one process...
if (processes.Length > 1)
{
// Assume there is our process, which we will terminate,
// and the other process, which we want to bring to the
// foreground. This assumes there are only two processes
// in the processes array, and we need to find out which
// one is NOT us.
RzLogger.WriteLine("process are running count:" + processes.Length);
// get our process
Process p = Process.GetCurrentProcess();
int n = 0; // assume the other process is at index 0
// if this process id is OUR process ID...
if (processes[0].Id == p.Id)
{
RzLogger.WriteLine("other process are at 1");
// then the other process is at index 1
n = 1;
}
// get the window handle
IntPtr hWnd = processes[n].MainWindowHandle;
RzLogger.WriteLine("main handle is:" + hWnd);
// if iconic, we need to restore the window
if (IsIconic(hWnd))
{
RzLogger.WriteLine("is minimized");
ShowWindowAsync(hWnd, SW_SHOWNORMAL);
ShowWindowAsync(hWnd, SW_RESTORE);
SetForegroundWindow(hWnd);
}
// bring it to the foreground
SetForegroundWindow(hWnd);
// exit our process
Application.Current.Shutdown();
return;
}
// ... continue with your application initialization here.
}
问题是我总是处理为0。
有办法做到这一点吗?我不想在任务栏中显示任何内容
答案 0 :(得分:0)
以下是如何使用管道的示例。
如果你非常快地启动实例,那么除了这个之外使用一个命名的互斥体可以解决一些问题(在这种情况下你最终会得到超过1个服务器 - 所以在早期创建一个命名的互斥锁,如果存在的话 - 只需发送在管道上显示()并关闭。
从VS2017默认Wpf-Project创建。编译它,启动Debug-Build,转到输出文件夹并再次启动exe以显示它的作用。
<强> MainWindow.xaml 强>
<Window x:Class="interproc.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:interproc"
mc:Ignorable="d"
Title="MainWindow"
Height="350"
Width="525"
Loaded="Window_Loaded">
<Grid>
<TextBlock Name="tb"
Background="Yellow"
Text="..."
Margin="50" />
</Grid>
</Window>
<强> MainWindow.xaml.cs 强>
using System;
using System.Windows;
using System.IO.Pipes;
using System.Threading;
using System.Windows.Threading;
namespace InterprocessCommunicationViaPipes
{
/// <summary>
/// Commands used to communiate via pipe
/// </summary>
public enum EPipeCommands : byte
{
None, Show, Hide, Close
};
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Title = DateTime.UtcNow.ToString("o");
}
/// <summary>
/// Name of the pipe used for interprocess communication
/// </summary>
private const string PipeName = "MyCoolApp";
/// <summary>
/// prevents double Close() calls
/// </summary>
private bool isClosing = false;
/// <summary>
/// Server
/// </summary>
private NamedPipeServerStream pipeServerStream = null;
/// <summary>
/// Thread server is running in
/// </summary>
private Thread ServerThread;
void ActOnPipeCommand(EPipeCommands c)
{
Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
new ThreadStart(
delegate
{
tb.Text += $"\n{DateTime.UtcNow:o} recieved {c}\n";
switch (c)
{
case EPipeCommands.None:
return;
case EPipeCommands.Hide:
Hide();
break;
case EPipeCommands.Show:
if (this.WindowState == WindowState.Minimized)
WindowState = WindowState.Normal;
Visibility = Visibility.Visible;
break;
case EPipeCommands.Close when !isClosing:
Close();
break;
case EPipeCommands.Close:
tb.Text += $"Already closing.\n";
break;
default:
tb.Text += $"Unmapped pipe action: {c.ToString()}\n";
break;
}
}));
}
/// <summary>
/// Server running?
/// </summary>
/// <returns></returns>
bool CheckIsRunning()
{
NamedPipeClientStream clientStream = new NamedPipeClientStream(PipeName);
try
{
clientStream.Connect(1000);
}
catch (TimeoutException)
{
tb.Text = $"No Server found.";
return false;
}
clientStream.WriteByte((byte)EPipeCommands.Show);
return true;
}
EPipeCommands InterpretePipeCommand(int v)
{
if (Enum.TryParse<EPipeCommands>($"{v}", out var cmd))
return cmd;
return EPipeCommands.None;
}
/// <summary> Creates the server, listens to connectiontrys,
/// reads 1 byte & disconnects </summary>
/// <param name="data"></param>
void PipeServer(object data)
{
pipeServerStream = new NamedPipeServerStream(
PipeName, PipeDirection.InOut,
2, PipeTransmissionMode.Byte);
do
{
pipeServerStream.WaitForConnection();
if (pipeServerStream.IsConnected && !isClosing)
{
ActOnPipeCommand(
InterpretePipeCommand(
pipeServerStream.ReadByte()));
}
pipeServerStream.Disconnect();
}
while (!isClosing);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (CheckIsRunning())
Close();
else
{
ServerThread = new Thread(PipeServer);
ServerThread.Start();
tb.Text = "Starting new pipe server.\n";
Closing += (a, b) => isClosing = true;
}
}
}
}