question

zequion-6374 avatar image
0 Votes"
zequion-6374 asked ·

Control.Dispatcher.Invoke to a Delegate Función Leave the Computer Locked

I need to update many times a control that is executing inside a thread and for that reason I execute control.invoke to a delegated function. The problem is that the execution is stuck.

I have prepared this namespace to test the execution in WPF and in WinForms using an Enum.
For Test Execute:Name_Invoke_Test.Cls_Invoke_Test.Fcn_Test();

 namespace Name_Invoke_Test
 {   
     public enum Aplicacion_Tipo : int
     {   SinDefinir = 0,
         ASPX       = 1,
         WinForms   = 2,
         WPF        = 3
     };
     public class Cls_Invoke_Test
     {   // [System.Diagnostics.DebuggerStepThroughAttribute]
         public static dynamic Fcn_Test()
         {   dynamic MyRet = null, MyParam1 = null, MyParam2 = null, MyParam3 = null;
             System.Windows.Controls.TextBox ThRControl = new System.Windows.Controls.TextBox();
    
             try
             {   // Inicialización.
                 ThRControl.Text = "MyText";
                 MyParam2        = 10;
    
                 // Thread.
                 System.Threading.Thread MyThread = new System.Threading.Thread(() => 
                 {   // Thread. Function. Control.Invoke Execute Delegate Function and Update Control.
                     MyRet = Name_Invoke_Test.Cls_Invoke_Test.Fcn_Control(ThRControl, MyParam1, ref MyParam2, MyParam3);
                 });
    
                 MyThread.Start();
                 MyThread.Join();
    
                 // Thread End. PROBLEM:The thread does not end because the execution has been blocked.
                 System.Console.WriteLine("Test(): {0}", MyParam2);
             }catch(System.Exception ErrorExcp){}
             return MyRet;
         }
    
         // Function. Control.Invoke Delegate Function and Update Control.
         // Alt. Delegados.
         public delegate dynamic Fcn_ThRControl_Control(dynamic ThRControl, dynamic MyParam1, ref dynamic MyParam2, dynamic MyParam3 = null);
         // [System.Diagnostics.DebuggerStepThroughAttribute]
         public static dynamic Fcn_Control(dynamic ThRControl, dynamic MyParam1, ref dynamic MyParam2, dynamic MyParam3 = null)
         {   dynamic MyRet = null;
    
             try
             {   // Aplicacion_Tipo.
                 Name_Invoke_Test.Aplicacion_Tipo Aplicacion_Tipo = Name_Invoke_Test.Aplicacion_Tipo.WPF;
    
                 // -------------------------------
                 // INVOKE. ACCIONES.
                 // -------------------------------
                 try{switch (Aplicacion_Tipo)
                     { case Name_Invoke_Test.Aplicacion_Tipo.WinForms: if(ThRControl.InvokeRequired)            return ThRControl.Invoke(new Name_Invoke_Test.Cls_Invoke_Test.Fcn_ThRControl_Control(Fcn_Control), new object[]{ ThRControl, MyParam1, MyParam2, MyParam3 }); break;
                       // PROBLEM: IT STAYS BLOCKED.
                       case Name_Invoke_Test.Aplicacion_Tipo.WPF:      if(!ThRControl.Dispatcher.CheckAccess()) return ThRControl.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new Name_Invoke_Test.Cls_Invoke_Test.Fcn_ThRControl_Control(Fcn_Control), new object[]{ ThRControl, MyParam1, MyParam2, MyParam3 }); break;
                     }}catch (System.Exception ErrorExcp_No_Mostrar){}
                
                 // Code Continue.
                 ThRControl.Text = "MyText Modified";
                 MyParam2        = 20;
                 MyRet           = "OK";
             }catch(System.Exception ErrorExcp){}
             return MyRet;
         }
     }
 }


windows-wpf
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DaisyTian-MSFT avatar image
0 Votes"
DaisyTian-MSFT answered ·

I modified two places in your code base on my understanding.
1. Remove MyThread.Join(); in line13. Thread.Join blocks the calling thread until the thread represented by this instance terminates. In your code , you block MyThread , and it can't go to continue.

2. I refer to Dispatcher.Invoke Overloads and find your invoke parameter is not in this list. I update it using Invoke(Delegate, DispatcherPriority, Object[]). Then I update line 38 as below:

  case Control_Invoke.Aplicacion_Tipo.WPF:     
                             if(!ThRControl.Dispatcher.CheckAccess()) 
                                 return ThRControl.Dispatcher.Invoke(new Control_Invoke.Cls_Invoke_Test.Fcn_ThRControl_Control(Fcn_Control), System.Windows.Threading.DispatcherPriority.Background,new object[]{ ThRControl, MyParam1, MyParam2, MyParam3 }); 
                             break;

Run your code and get the "OK" for MyRet. If I misunderstand your question, please point out.


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.