Click here to Skip to main content
       

Silverlight / WPF

 
QuestionDispatcherTimer Tick event not fired PinmemberPraveen Raghuvanshi17-Jan-13 4:54 
Hi,
 
I am working on a WPF application. The application uses Infragistics controls such as Ribbon Controls and XamDockManager. It has a File Menu wherein I have provided a Save menu item to save the items on the canvas to a file. On click of Save button, Save Dialog opens to provide the File name and file is saved at the defined location.
The saving process is quite time consuming and in order to show something to the user, I have used the WaitCursor.
I have used a class similar to the one mentioned(UiServices.ShowWaitCursor) in the below thread.
 
http://stackoverflow.com/questions/7346663/how-to-show-a-waitcursor-when-the-wpf-application-is-busy-databinding[^]
 
http://stackoverflow.com/questions/3480966/display-hourglass-when-application-is-busy[^]
 
The usage is like this
 
public void WriteToFile(string fileName)
{
UiServices.SetBusyState();
_customFileWriter.WriteToFile(fileName);
}
 
This works fine, if we just save the things on the canvas into a file. However, if fails in the below scenario.
The application has a canvas where I display some UI elements. On double click of the UI element, we launch a Windows form in the Infragistic ContentPane.
We have a modeless dialog opened and perform File -> Save and provide a file name, the cursor stays indefinitely. It goes only once we click on the Canvas. The DispatcherTimer_Tick is fired only when we click on the Canvas which resets the cursor.
 
The DispatcherPriority is ApplicationIdle.
Another observation is, this happens only when we open the Save dialog through the File -> Save. It works fine if we open the Save dialog through Ctrl + S key combinations.
 
Is there a way I can debug the non firing of DispatcherTimer.Tick event or any other way to handle this?
 
I created a simple WPF application and it works fine in that.
 
MainWindow.xaml
 
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
 
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Menu Height="36" Name="menu1" Margin="12,0,439,197">
            <MenuItem Header="_File" Height="22" Name="menuitem1" Width="200" Margin="10, 10, 5, 5" HorizontalAlignment="Left" VerticalAlignment="Top" Background="Chocolate">
                <MenuItem Header="Save..."  Click="MenuItem_Click"/>
            </MenuItem>
        </Menu>
        <Canvas Height="156" HorizontalAlignment="Left" Margin="81,77,0,0" Name="canvas1" VerticalAlignment="Top" Width="304" Background="#FF945050">
            <Button Canvas.Left="128" Canvas.Top="49" Content="Button" Height="23" Name="button1" Width="75" Click="button1_Click" />
            <TextBox Canvas.Left="48" Canvas.Top="128" Height="23" Name="textBox1" Width="120" />
        </Canvas>
    </Grid>
</Window>
 
MainWindow.cs
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
 
namespace WpfApplication1
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			InitializeComponent();
		}
 
		private void button1_Click(object sender, RoutedEventArgs e)
		{
			System.Windows.Forms.Form form = new System.Windows.Forms.Form();
			form.Height = 200;
			form.Width = 400;
			form.TopMost = true;
			form.Show();
		}
 
		private void MenuItem_Click(object sender, RoutedEventArgs e)
		{
			string fileName = SaveFileDialog();
			UiServices.SetBusyState();
			System.Threading.Thread.Sleep(3000);
		}
 
		private string SaveFileDialog()
		{
			// Create SaveFileDialog
			Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
 
			// Display SaveFileDialog by calling ShowDialog method
			Nullable<bool> result = dlg.ShowDialog();
 
			string filename = string.Empty;
			// Get the selected file name 
			if (result == true)
			{
				// Open document
				filename = dlg.FileName;
			}
			return filename;
		}
	}
 
	/// <summary>
	/// Sets the busy state on the application.
	/// Displays a wait cursor for long running tasks.
	/// </summary>
	public static class UiServices
	{
		/// <summary>
		/// A value indicating whether the UI is currently busy
		/// </summary>
		private static bool _isBusy;
 
		/// <summary>
		/// Sets the state as busy.
		/// </summary>
		public static void SetBusyState()
		{
			SetBusyState(true);
		}
 
		/// <summary>
		/// Sets the state to busy or not busy.
		/// </summary>
		/// <param name="busy">if set to <c>true</c> the application is now busy.</param>
		private static void SetBusyState(bool busy)
		{
			if (busy != _isBusy &&
				Application.Current != null &&
				Application.Current.Dispatcher != null)
			{
				_isBusy = busy;
				Mouse.OverrideCursor = busy ? Cursors.Wait : null;
 
				if (_isBusy)
				{
					new DispatcherTimer(TimeSpan.FromSeconds(0), DispatcherPriority.ApplicationIdle, DispatcherTimer_Tick, Application.Current.Dispatcher);
				}
			}
		}
 
		/// <summary>
		/// Handles the Tick event of the dispatcherTimer control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private static void DispatcherTimer_Tick(object sender, EventArgs e)
		{
			var dispatcherTimer = sender as DispatcherTimer;
			if (dispatcherTimer != null)
			{
				SetBusy(false);
				dispatcherTimer.Stop();
			}
		}		
	}
}
 
Thanks in advance!
AnswerRe: DispatcherTimer Tick event not fired PinmemberPaulo Zemek23-Jan-13 6:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 9 Jul 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid