Search Unity

Using 3rd winform control get an exception my window closed

Discussion in 'Scripting' started by domcylai, Mar 28, 2019.

  1. domcylai

    domcylai

    Joined:
    Mar 28, 2019
    Posts:
    4
    HI guys,

    Sorry about my poor English , past few days I trying build a 3rd windows form control (Krypton ) and call it from my new unity project ,but some exception make my window close (Dispose) . now I got the stacktrace information by using StackTrace() , shown in the following figure:



    then I checked mono assembly source and location that problem position, but I am not
    experienced enough to fix it, so ask someone can find problems and give me some advice,thank you.

    More code plz see "...mono-master\mcs\class\System.Windows.Forms\System.Windows.Forms\NativeWindow.cs"


    Code (CSharp):
    1.         internal static IntPtr WndProc(IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam)
    2.         {
    3.             IntPtr result = IntPtr.Zero;
    4.             Message    m = new Message();
    5.             m.HWnd = hWnd;
    6.             m.Msg = (int)msg;
    7.             m.WParam = wParam;
    8.             m.LParam = lParam;
    9.             m.Result = IntPtr.Zero;
    10.                    
    11. #if debug
    12.             Console.WriteLine("NativeWindow.cs ({0}, {1}, {2}, {3}): result {4}", hWnd, msg, wParam, lParam, m.Result);
    13. #endif
    14.             NativeWindow window = null;
    15.            
    16.             try {
    17.             object current = null;
    18.             lock (window_collection) {
    19.                 current = window_collection[hWnd];
    20.             }
    21.  
    22.             window = current as NativeWindow;
    23.             if (current == null)
    24.                 window = EnsureCreated (window, hWnd);
    25.  
    26.             if (window != null) {
    27.                 window.WndProc (ref m);
    28.                 result = m.Result;
    29.             } else if (current is ArrayList) {
    30.                 ArrayList windows = (ArrayList) current;
    31.                 lock (windows) {
    32.                     if (windows.Count > 0) {
    33.                         window = EnsureCreated ((NativeWindow)windows[0], hWnd);
    34.                         window.WndProc (ref m);
    35.                         // the first one is the control's one. all others are synthetic,
    36.                         // so we want only the result from the control
    37.                         result = m.Result;
    38.                         for (int i=1; i < windows.Count; i++)
    39.                             ((NativeWindow)windows[i]).WndProc (ref m);
    40.                     }
    41.                 }
    42.             } else {
    43.                 result = XplatUI.DefWndProc (ref m);
    44.             }
    45.             }
    46.             catch (Exception ex) {
    47. #if !ExternalExceptionHandler
    48.                 if (window != null) {
    49.                     if (msg == Msg.WM_PAINT && window is Control.ControlNativeWindow) {
    50.                         // Replace control with a red cross
    51.                         var control = ((Control.ControlNativeWindow)window).Owner;
    52.                         control.Hide ();
    53.                         var redCross = new Control (control.Parent, string.Empty);
    54.                         redCross.BackColor = Color.White;
    55.                         redCross.ForeColor = Color.Red;
    56.                         redCross.Bounds = control.Bounds;
    57.                         redCross.Paint += HandleRedCrossPaint;
    58.                     }
    59.                      window.OnThreadException (ex);
    60.                 }
    61. #else
    62.                 throw;
    63. #endif
    64.             }
    65.             #if debug
    66.                 Console.WriteLine("NativeWindow.cs: Message {0}, result {1}", msg, m.Result);
    67.             #endif
    68.  
    69.             return result;
    70.         }


     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Well, this is wrong callstack. The Dispose was called from wndproc because of exception happend before. And you may catch it in yout thread main func. I bet the exception is about you can't control window from aside thread.
     
  3. domcylai

    domcylai

    Joined:
    Mar 28, 2019
    Posts:
    4
    Thank you reply so quickly, I test this in an unity-UI-button-object's onClick.AddListenner delegate, when I click unity button create new thread to call KryptonExplorer.dll make new window. I can't get any exception info even if I use try..catch, I try to make a break point at "KryptonExplorer.Form1 F3 = new KryptonExplorer.Form1();" code , but the debuger can't jump to next line (ps: VS 2015 with vs tools for unity) .

    finally the thread I made dead there. if I don't make new thread then unity project dead too.here is my code:

    Code (CSharp):
    1. public class script : MonoBehaviour
    2. {
    3. void Start()
    4. {
    5. this.GetComponent<Button>().onClick.AddListener(delegate ()
    6. {
    7. Thread t = new Thread(() =>
    8. {
    9. KryptonExplorer.Form1 F3 = new KryptonExplorer.Form1();
    10. F3.Show();
    11. });
    12. t.Start();
    13. });
    14. }
     
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Exception happes in your anonymous method. Add try..catch to it to see real error.
     
  5. domcylai

    domcylai

    Joined:
    Mar 28, 2019
    Posts:
    4
    I create a message box after Form1 InitializeComponent() , and it's ok at this point . but when I click message box ok button the problem occur again.



    Code (CSharp):
    1. namespace KryptonExplorer
    2. {
    3.     public partial class Form1 : KryptonForm
    4.     {
    5.         public Form1()
    6.         {
    7.             InitializeComponent();
    8.  
    9.             kryptonNavigator.SelectedIndex = 0;
    10.             kryptonNavigatorToolkit.SelectedIndex = 0;
    11.             System.Windows.Forms.MessageBox.Show("5" + this.ToString());
    12.             //Dir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
    13.         }
    14. ...
    15.  
    16. }
    17.  


    Just now I build a new WindowsFormsApplication , test the same code and I success call form1 by unity.
    Sorry for my poor knowledge and I don't know what's the problem when I use 3rd winform control .