ContentFrame.Navigate - Storyboard not showing at the Page_Load - silverlight

I have a simple silverlight Navigation Application.
I need to start a Storyboard when a Page is navigated on a content frame.
At first everything seemed to work fine. But now I need to do some treatment (that takes about 3 seconds) when the Page loads, just before showing the Storyboard.
Now I do not see my Storyboard animation anymore. Even if the Storyboard is started after my data is loaded.
I can emulate my problem using a Thread.Sleep like that it does the same thing :
private void Page_Loaded(object sender, RoutedEventArgs e)
{
System.Threading.Thread.Sleep(3000);
Storyboard1_Test.Begin();
}

Triggering UI changes from an event handler sometimes have this kind of issues. Usually using Dispatcher.BeginInvoke method solve it.
So just try lunching Storyboard1_Test.Begin(); from the Dispatcher.BeginInvoke
Some code (hope you don't mind i'm using VB.NET):
Dispatcher.BeginInvoke(Sub()
Storyboard1_Test.Begin()
End Sub)
By using the dispatcher.BeginInvoke you verify that the job is excuted on the UI thread when the thread is available.

Related

Visual Studio "Not Responding" when I change a datetimepicker value in Winforms [duplicate]

I have a datetimepicker in C#. When I click on it, it expands to show a monthly calendar, when I click the left arrow to go back a month, it changes the value and calls my event. The event includes too much code to include here but it calls several functions needless to say.
The problem I'm having is that when I click that left arrow it gets stuck in some sort of loop and keeps descending through the months and I can't stop it. One of the functions that is being called contains a Application.DoEvents() and if I comment that out it doesn't get stuck in the loop, but I need that command to update another section of the interface. Any idea why this is happening?
I can duplicate it sometimes with this code, sometimes it just does it a couple times, sometimes it gets stuck in the loop.
private void DateTimePickerValueChangedEvent(object sender, EventArgs e)
{
afunction();
}
private void afunction()
{
listView1.Clear();
panel1.Visible = true;
Application.DoEvents();
}
I also have the same problem. In my case, instead of calling DoEvents I'm updating a Crystal Report view. The only workaround I found is to update my view upon the CloseUp event instead of ValueChanged or TextChanged.
Scott, how did you finally corrected your problem ?
The DateTimePicker ValueChanged event is buggy. Per Microsoft Windows Forms Team on this page https://connect.microsoft.com/VisualStudio/feedback/details/1290685/debugging-datetimepicker-event-hangs-vs:
"The DateTimePicker control installs a mouse hook as part of its functionality, but when the debugger has the WinForms application stopped on a breakpoint, it allows the possibility of a deadlock if VS happens to get a mouse message. For now, the deadlock is unfortunately a consequence of the DateTimePicker's design. The mouse hook is installed when the drop down is clicked to display the calendar. This means that breakpoints should not be sent in any event handlers which would be called while the calendar is active. We are currently investigating whether it is possible to address this issue and we will update this thread with further information if we are able to make a fix available."
Without seeing any of the code, try these steps:
Comment out the entire event handler
to see how fast it runs with nothing
attached to it.
Uncomment lines one at a time to see
which ones are causing the most
problems.
Analyze those method calls.
...
Profit!
You could try a couple of things. Get rid of the DoEvents inside of the ChangedEvent.
Call the doevents inside of a seperate function after maybe a period of time (thread.sleep() ?).
I know doevents does cause issues but I rarely use it.
event procedure ValueChanged :
set parameter in sender.tag
enableTimer and execute parameter using sender.tag
example:
private void DateTimePicker_ValueChanged(object sender, EventArgs e)
{
DateTimePicker ThisSender = (DateTimePicker)sender;
Timer.Tag = ThisSender.Name.ToString() + "=" + ThisSender.Value;
Timer.Enabled = true;
}

WPF: How to call a method at a specific point in time while an animation is running

My problem is the following:
I've got an animation which makes a panel slide until it disappears (I animate its margin property). As soon as the animation ends, I want the panel to go back where it was at the start. So I set the autoreverse property to true, animation duration doubles automaticly, and everithing runs fine. Problem is I'm still missing something fundamental in my program: when the animation has run midway, that is when the panel disappears, I have to do some processing, calling a method, which makes the panel content change its UI.
So, is there a way to do this?
Thanks
As #deafjeff suggests, you could make 2 animation instead of using autoreverse. Register a handler for the Completed event for the first animation. Do the UI changes and start 2nd animation in the hanlder code. Code is like followig:
private void onFirstAnimationCompleted(object sender, EventArgs e)
{
DoUIChange();
SecondAnimation.Start();
}

GUI of a WPF application is freezing

The Whole GUI is loaded. After clicked on certain button, the GUI is not responsive: any buttons are no response. If I switch to other app and switch back, the GUI is OK immediately.
Behind the clicked button, it is a handle sending requests to back end. I debugged app, and data has always been returned.
Since the app is huge and I'm not familiar with it, I cannot extract a simple model of sending requests and processing data.I wonder possible causes, since I have no idea now.
Best regards,
-----Add More-----
The back end request sending is in a threadpool thread; when getting data, no UI controls are updated directly. Only presenters (view model) are updated, which are binding to UI control.
I think as sa-ddam213 have suggested and I too believe that you are executing a code block in background Thread or Task.
I think problem is that
- On button click, start execution in background.
- You have kept a flag for checking if background process is running for CanExecute() for the Button's Command.
- UI checks CanExecute() for the Button's Command for a while then does not as mentioned in another question here - Is Josh Smith's implementation of the RelayCommand flawed?.
- Process returns in background.
- UI does not knows the background process is completed and as it has stopped checking the CanExecute() for the Button's Command, UI will not come to know itself.. (you need to tell it)
- Therefore UI is not responding but when user will click somewhere in the application or do an in-out as you said, it will back again.
Solution
- By using InvalidateRequerySuggested method you can invoke the command requery on UI thread so UI will recheck the CanExecute() for each Control and/or Button Command.
// Forcing the CommandManager to raise the RequerySuggested event
CommandManager.InvalidateRequerySuggested();
The InvalidateRequerySuggested method forces the CommandManager to raise the RequerySuggested event. The RequerySuggested event informs a command source to query the command it is associated with to determine whether or not the command can execute.
For more refer:
- How does CommandManager.RequerySuggested work?
- MSDN - CommandManager
- MSDN - CommandManager.InvalidateRequerySuggested Method
Its just a wild guess but try dropping the priority of the logic inside your click event
Example
private void Button_Click(object sender, RoutedEventArgs e)
{
Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)delegate
{
// all your code here.
});
}

Image not being rendered as soon as I add it to a page

I am a newbie in Windows phone app development. On one of my pages I have a button that does some substantial amount of work. So when I press the button the screen freezes and then resumes when all the work is done. Now I wanted to show some waiting image or page to be shown during this while.
So I created and destroyed an image on top on the page at the start and end of button click action respectively but it still freezes and the screen renders only after all the work is done making it indifferent. I am using Silverlite C#.
You'll want to push the heavy work onto a background thread. What's currently happening is that the work is being done on the UI thread, so it 'freezes' since the UI thread is waiting for the work to be done. There are various ways you can push the work to a background thread. One way would be to use a BackgroundWorker.
A simple example, which doesn't include things like reporting when the work is done, would look like this
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(BW_DoWork);
bw.RunWorkerAsync();
private void BW_DoWork(object sender, DoWorkEventArgs e)
{
//Your heavy work code
}
Important thing to remember when you work on a background thread, is that if you want to change anything on the UI, you need to marshal the data to the UI thread and update it there. You can do that like this:
Deployment.Current.Dispatcher.BeginInvoke(() => {
myTextBlockExample.Text = "Changing the UI";
}
If you tried to change the TextBlock without the Dispatcher.BeginInvoke method, you'll get a Invalid Cross-Thread Access error. If you see that error in WP development, it's likely that you're trying to update a UI element from a non-UI thread.
You can put your code that displays the loading image before calling bw.RunWorkerAsync.

Question about WPF Dispatcher.BeginInvoke called from same thread! Why?

I'm relatively new to WPF. I'm examining some code that looks like this:
private void button_Click(object sender, RoutedEventArgs e)
{
//Queue on dispatcher in the background so it doesn't make the UI slow
Dispatcher.BeginInvoke(new dMyDelegate(PerformOperation), DispatcherPriority.Background);
}
From the comment, I'm guessing the original code felt that this was necessary to make the UI more responsive, however, my understanding is that Dispatcher.BeginInvoke simply runs something on the UI thread. Since the buttn_Click is already on the UI thread, what's the point? Perhaps I'm misunderstanding Dispatcher and BeginInvoke. I'm assumming that Dispatcher in this case, is the dispatcher owned by the class this method is in, which is MainWindow.xaml. Can someone enlighten me?
Thanks
Well, it's asking for "background" priority, so it's only going to get executed when any more important events have been processed... If this is part of a big screen refresh, it'll effectively wait until all of that has happened before executing. Even so, if it's going to be doing anything long-running (or making any potentially blocking calls) then you're right, it really shouldn't be running on the UI thread at all.

Resources