Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8201125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:42:06+00:00 2026-06-07T06:42:06+00:00

I have an WPF application called app1 which has a windows named window1. When

  • 0

I have an WPF application called app1 which has a windows named window1. When the user clicks the close button of window1, the app does not close but the window1 hides (this.hide()).

I want to check if another instance of the application is already running when it is started; if so, I want to show the already running instance and terminate the new one.

How can I do that?

I know how to check the process and how to close the current app but I don’t know how to show a window from another WPF process which is running…

In my App startup event I do this :

private void Application_Startup(object sender, StartupEventArgs e)
{
    if(Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Count() > 1)
    {
        Application.Current.Shutdown(0);
    }
}  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T06:42:08+00:00Added an answer on June 7, 2026 at 6:42 am

    I found how to do the work!

    My problem solved with “Reed Copsey” help and with Windows SendMessage API.
    for doing this thing I wrote these codes in my window1.xaml.cs file :

    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using System.Windows.Interop;
    
    
    namespace app1
    {
        public partial class window1: Window
        {
            public window1()
            {
                InitializeComponent();            
            }
            private void window1_Loaded(object sender, RoutedEventArgs e)
            {
                HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
                source.AddHook(new HwndSourceHook(WndProc));
            }
            private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
            {
                System.Windows.Forms.Message m = System.Windows.Forms.Message.Create(hwnd, msg, wParam, lParam);
                if (m.Msg == WM_COPYDATA)
                {
                    // Get the COPYDATASTRUCT struct from lParam.
                    COPYDATASTRUCT cds = (COPYDATASTRUCT)m.GetLParam(typeof(COPYDATASTRUCT));
    
                    // If the size matches
                    if (cds.cbData == Marshal.SizeOf(typeof(MyStruct)))
                    {
                        // Marshal the data from the unmanaged memory block to a 
                        // MyStruct managed struct.
                        MyStruct myStruct = (MyStruct)Marshal.PtrToStructure(cds.lpData,
                            typeof(MyStruct));
    
                        // Display the MyStruct data members.
                        if (myStruct.Message == "Show Up")
                        {
                            this.Show();
                        }
                    }
                }
                return IntPtr.Zero;
            }
    
            internal const int WM_COPYDATA = 0x004A;
    
            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
            internal struct MyStruct
            {
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
                public string Message;
            }
            [StructLayout(LayoutKind.Sequential)]
            internal struct COPYDATASTRUCT
            {
                public IntPtr dwData;       // Specifies data to be passed
                public int cbData;          // Specifies the data size in bytes
                public IntPtr lpData;       // Pointer to data to be passed
            }
    
    
        }
    }
    
    
    

    And I wrote these codes in my App.xaml.cs :

    
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Windows;
    using System.Windows.Threading;
    using System.Runtime.InteropServices;
    using System.Security;
    
    namespace app1
    {    
        public partial class App : Application
        {       
            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
            internal struct MyStruct
            {
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
                public string Message;
            }
    
    
    
            internal const int WM_COPYDATA = 0x004A;
            [StructLayout(LayoutKind.Sequential)]
            internal struct COPYDATASTRUCT
            {
                public IntPtr dwData;       // Specifies data to be passed
                public int cbData;          // Specifies the data size in bytes
                public IntPtr lpData;       // Pointer to data to be passed
            }
            [SuppressUnmanagedCodeSecurity]
            internal class NativeMethod
            {            
                [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
                public static extern IntPtr SendMessage(IntPtr hWnd, int Msg,
                    IntPtr wParam, ref COPYDATASTRUCT lParam);
    
    
                [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
                public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
            }
    
            private void Application_Startup(object sender, StartupEventArgs e)
            {            
                if (System.Diagnostics.Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Count() > 1)
                {
                    IntPtr hTargetWnd = NativeMethod.FindWindow(null, "window1");
                    if (hTargetWnd == IntPtr.Zero)
                    {                    
                        return;
                    }
                    MyStruct myStruct;
                    myStruct.Message = "Show Up";
                    int myStructSize = Marshal.SizeOf(myStruct);
                    IntPtr pMyStruct = Marshal.AllocHGlobal(myStructSize);
                    try
                    {
                        Marshal.StructureToPtr(myStruct, pMyStruct, true);
    
                        COPYDATASTRUCT cds = new COPYDATASTRUCT();
                        cds.cbData = myStructSize;
                        cds.lpData = pMyStruct;
                        NativeMethod.SendMessage(hTargetWnd, WM_COPYDATA, new IntPtr() , ref cds);
    
                        int result = Marshal.GetLastWin32Error();
                        if (result != 0)
                        {                       
                        }
                    }
                    finally
                    {
                        Marshal.FreeHGlobal(pMyStruct);
                    }
                    Application.Current.Shutdown(0);
                }            
            }  
    
        }
    }
    
    

    And that’s it. 😀

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C# WPF application with a web browser control (System.Windows.Controls.WebBrowser) called wB.
I have a wpf application which has combo box and multiple text boxes. On
I am developing a WPF application, and I have created a user control called
I have a Wpf application project called WpfTest which references WpfTestLib (A class library).
I have a WPF application that has just one button. When the button is
I have a WPF application which uses a WPF user control. The user control
I have a WPF application that has a variable x which is an instance
I have a WPF application with form that has a textbox named txtStatusWindow. I
I have a WPF application that has user validation. This is rendered in the
I have WPF Application where I have One main form and other user controls

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.