How to perform an action once every 1/2 second - c#

I want a XAML page to perform an action once every 1/2 second.How would I do this?I'm thinking that I could use a Timer that starts when the page is loaded and resets once it gets to 500 milliseconds, but I'm not entirely sure how to do this.It doesn't have to be extremely precise.

You can use DispatcherTimer for example. For sample code look at documentation.

Did You Know… That You Can Create A Timer Using XAML Animation?

Related

Trigger heavy task on view model's property change

I'm creating a video/frame editor and I wonder what would be the best approach for triggering a render cycle whenever the user scrubs (manually navigate to specific time) or playback is triggered.
Render is done using a WriteableBitmap back buffer, so I always need to mark the frame as dirty (in the UI thread) when the render cycle is complete.
Initially I thought about triggering render on a TimeSpan property changed, but that's doesn't look right as it may be too heavy of a task and it would hang the UI.
public TimeSpan CurrentTime
{
get => _currentTime;
set
{
_currentTime = value;
//Render frame animation based on current time.
Render();
}
}
My goals are:
Don't freeze the UI.
Drop frames if the rendering takes too much to complete or the current time is switched too fast.
Less delay possible to render when switching current time.
What's the most used (or better) approach in this case?
Maybe trigger a command?

Xamarin Forms - attaching scrollview.Scrolled to a function

When I attach a scrollview to a function like this
textScroll.Scrolled += (sender, e) => { onScrolled(); };
Each time I scroll up or down, OnScrolled() is called multiple times. I know I can get the size of the content and compare it to the ScrollY value, obviously the ScrollY value changes each time, but as far as I can see I won't know when the last call happens (per user scroll).
I only want to call this once per scroll, failing that call it each time as is happening now, but only act when I know I'm on the last call.
Is this possible?
thanks
It is possible, but with a custom renderer for each platform.
On iOS: you will want to implement delegates for DecelerationEnded and WillEndDragging. The reason for also implementing DecelerationEnded is to allow for a fling by the user and waiting for the velocity to come to 0.
On Android it is a bit more complicated. Here is a native Android SO post I followed and translated into c# in a renderer. Works pretty well for me.
Android: Detect when ScrollView stops scrolling
After having both implemented, you can call to your Xamarin.Forms view in order to notify that the view has Stopped scrolling (i.e. final call)

C# WPF call function or occurs some code every frame of a WPF application

I wanted to know if is there a way to do something (call function...) every frames in a WPF application, like "update()" in Unity, or like "Application.Idle += new EventHandler(Application_Idle)" in a Winform app ?
Thanks.
Is it related purely to UI rendering events? If so, try looking into CompositionTarget.Rendering event.
It sounds like you probably want to just use a Dispatcher timer. How do I create a timer in WPF?
The update time in unity is based on many factors but generally at 60 frames a second so the timer interval would be something like 17ms. However you should know wpf doesn't really have frames like unity so there is no update equivalent. It only updates the layout when something moves/added/changed or you call InvalidateLayout to force it to do so.
maybe you could structure your windows constructor like this as in pygame, though I'm not sure it will work
bool running = true;
public someWindow() {
// setup
running = true;
while (running) {
// do stuff
}
}

How to stop a storyboard with RepeatBehaviour="Forever"

I have a Storyboard in XAML with ReapeatBehaviour="Forever". I run the storyboard when uploading files, which can vary quite a bit in size. I can stop the storyboard no problem when the file upload is complete, but what I'd like to do is run the storyboard just one more time, rather than stopping it midflow by calling Storyboard.Stop().
How do I accomplish this?
Ah sussed it - instead of calling Storyboard.Stop(), change the RepeatBehaviour property instead:
Storyboard11.RepeatBehavior = new RepeatBehavior(1.0);

Update UpdatePanel without a control?

I currently have a process that does the following:
Read a remote file and parse complex data;
Dynamically create a bitmap image based on the data;
Display the bitmap in an Image control in an Updatepanel.
Wait 15 seconds, then repeat the whole process.
I have set myScriptManager.RegisterAsyncPostBackControl(myTimer), and run the whole process (all three steps in sequence) on myTimer_Tick, and I have set the timer's Interval to 15 seconds, which is ideal for my purposes. However, step 1 above could take as long as a minute (in very rare cases), and I'm not entirely sure what will happen in that case? What will happen if the myTimer_Tick fires after 15 seconds and start it all again, when the previous run hasn't completed?
Ideally I'd like to update the UpdatePanel programmatically when the last line of code for Steps 1 to 3 have been finished, and then use Thread.Sleep to pause for 14 seconds before repeating, as it usually takes about 1 second to do steps1-3. However, I have no idea how to make the UpdatePanel update without linking it to a control. Why can't I just say myUpdatePanel.Upate() at the end of any chunk of code?

Categories