Posts

Showing posts with the label Threading

Donate

Pass By Reference Not Allowed In UI Threading Of Windows Forms In C#

The code below does not allow pass by reference to be used in UI threading particularly method Invoker. private void ShowPassMeta( ref int passedSound) { if (lblPassSoundex.InvokeRequired) { lblPassSoundex.Invoke((MethodInvoker) delegate { ShowPass( ref passedSound); }); } else { lblPassSoundex.Text = (++passedSound).ToString(); } } The solution is to store the method parameter in another variable as shown below: private void ShowPassMeta( ref int passedSound) { var passedSound1 = passedSound; if (lblPassSoundex.InvokeRequired) { lblPassSoundex.Invoke((MethodInvoker) delegate { ShowPassMeta( ref passedSound1); }); } else { lblPassSoundex.Text = (++passedSound1).ToString(); } } Cheers!

Check If Controls On another WPF Window Have Been Rendered Using Multithreading

Given you have two WPF windows namely MainWindow and OtherWindow. You basically want to check if the controls in main window have successfully loaded using statements in OtherWindow. The solution is to use Application dispatcher object. if (Application.Current.Dispatcher.CheckAccess()) { while ( true ) { foreach (Window window in Application.Current.Windows) { if (window.Title.Equals( "Project Title" )) { if (window.FindName( "your_control" ) != null ) { //do your stuff here... } } } break ; } } else { Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(CheckMainWindow)); } Cheers!

Donate