How does the event dispatch thread work?

If I understand your question correctly you’re wonder why you can’t do this: public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { showGUI(); } }); counter.start(); } The reason why you can’t do it is because the scheduler makes no guarantees… just because you invoked showGUI() and then you invoked counter.start() … Read more

Communication between BroadcastReceiver and Activity – android

You can use observers , like public class MyReceiver extends BroadcastReceiver { public MyReceiver() { } @Override public void onReceive(Context context, Intent intent) { ObservableObject.getInstance().updateValue(intent); } } public class MainActivity extends Activity implements Observer { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ObservableObject.getInstance().addObserver(this); } @Override public void update(Observable observable, Object data) { Toast.makeText(this, String.valueOf(“activity observer … Read more

C# DllImport with C++ boolean function not returning correctly

I found the solution for your problem. Your declaration should be preceded with this marshaling: [return:MarshalAs(UnmanagedType.I1)] so everything should look like this: [DllImport(“Whisper.dll”, EntryPoint=”Exist”, CallingConvention=CallingConvention.Cdecl)] [return:MarshalAs(UnmanagedType.I1)] public static extern bool Exist([MarshalAs(UnmanagedType.LPStr)] string name); I tested it in my very simple example and it worked! EDIT Why this happens? C defines bool as 4 bytes int … Read more

MethodInvoker vs Action for Control.BeginInvoke

Both are equally correct, but the documentation for Control.Invoke states that: The delegate can be an instance of EventHandler, in which case the sender parameter will contain this control, and the event parameter will contain EventArgs.Empty. The delegate can also be an instance of MethodInvoker, or any other delegate that takes a void parameter list. … Read more

Invoking methods with optional parameters through reflection

According to MSDN, to use the default parameter you should pass Type.Missing. If your constructor has three optional arguments then instead of passing an empty object array you’d pass a three element object array where each element’s value is Type.Missing, e.g. type.GetParameterlessConstructor() .Invoke(BindingFlags.OptionalParamBinding | BindingFlags.InvokeMethod | BindingFlags.CreateInstance, null, new object[] { Type.Missing, Type.Missing, Type.Missing }, … Read more

Best Way to Invoke Any Cross-Threaded Code?

You also could use an extension method and lambdas to make your code much cleaner. using System.ComponentModel; public static class ISynchronizeInvokeExtensions { public static void InvokeEx<T>(this T @this, Action<T> action) where T : ISynchronizeInvoke { if (@this.InvokeRequired) { @this.Invoke(action, new object[] { @this }); } else { action(@this); } } } So now you can … Read more