Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Avoid hang in WPF application using WindowsFormsHostControl

$
0
0

public partial class MainWindow : Window
    {
        public IntPtr MainWindowHandle { get; set; }

        [DllImport("user32.dll", SetLastError = true)]
        private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        /// <summary>
        public MainWindow()
        {
            InitializeComponent();

            try
            {
                //External exe inside WPF Window
                var  panel = new System.Windows.Forms.Panel();

                var windowsFormsHost = new WindowsFormsHost();

                windowsFormsHost.Child = panel;

                this.grid.Children.Add(windowsFormsHost);



                ProcessStartInfo psi = new ProcessStartInfo(@"C:\WindowsFormsApplication1.exe");

                psi.WindowStyle = ProcessWindowStyle.Normal;

                var process = Process.Start(psi);

                process.WaitForInputIdle(); // true if the associated process has reached an idle state.

                System.Threading.Thread.Sleep(3000);

                IntPtr hwd = process.MainWindowHandle;
                SetParent(process.MainWindowHandle, panel.Handle);  // loading exe to the wpf window.

            }
            catch (Exception ex)
            {
                //Nothing...
            }
        }
    }

Is there any way to avoid WPF application hanging when it hosts an external process? The external process does a long running operation in Main Thread.

The WindowsFormsApplication have a button. On click of user button, it executes an infinite while loop.


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>