Standard function to use Invoke in Aspx, WinForms and WPF

  • Thread starter Thread starter zequion1
  • Start date Start date
Z

zequion1

Guest
I have prepared a function so that I can test the invoke method. I intend to have some standard lines for ASPX, WINFORMS and WPF to be able to put them in all the functions that have a control in another thread. One of the parameters is ref. I use these lines in all my functions and I thought it worked but now I find that it does not. Can we improve these lines? He thinks that it should be the minimum possible code and better if it is in a function that can be called from the rest of the functions.

I have a function that determines if the execution occurs in Aspx, WinForms or Wpf but in this function it is indicated manually with the variable Application_Type

// Code Test.
dynamic MyDyn = Name_Test.Cls_Test.Fcn_Test();


namespace Name_Test
{
public enum Aplicacion_Tipo : int
{ SinDefinir = 0,
ASPX = 1,
WinForms = 2,
WPF = 3
};
public class Cls_Test
{
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(() =>
{ // Test.
MyRet = Name_Test.Cls_Test.Fcn_Control(ThRControl, MyParam1, ref MyParam2, MyParam3);
});

MyThread.Start();
MyThread.Join();

// Thread. Return.
System.Console.WriteLine("Test(): {0}", MyParam2);
}catch(System.Exception ErrorExcp){}
return MyRet;
}

// 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_Test.Aplicacion_Tipo Application_Type = Name_Test.Aplicacion_Tipo.WPF;

// -------------------------------
// INVOKE. ACCIONES.

// PROBLEM:Stop the execution with F9 and pulse F11. The execution will not be seen again.

// -------------------------------
try{switch (Application_Type)
{ case Name_Test.Aplicacion_Tipo.WinForms: if(ThRControl.InvokeRequired) return ThRControl.Invoke(new Name_Test.Cls_Test.Fcn_ThRControl_Control(Fcn_Control), new object[]{ ThRControl, MyParam1, MyParam2, MyParam3 }); break;
case Name_Test.Aplicacion_Tipo.WPF: if(!ThRControl.Dispatcher.CheckAccess()) return ThRControl.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Name_Test.Cls_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;
}
}
}

Continue reading...
 
Back
Top