I make some tests with C# and Windows Forms.
At the moment I am trying to implement some animations, for example display the current time or display a loading animation.
I looked around the internet but some solutions I found looked a bit ugly.
For example the most people create a Timer and refresh the animation at a specific frequency.
But I don't think thats the purpose of a timer...
So I found a second solution.
Simply register for the Application Idle event so I can update some animations and other processing stuff as part of the main application loop.
This sounds like a better solution.
Also I am wondering how I should request a redraw of some custom animated controls.
Simply call Invalidate() or Update() whats the recommend way?
Whats the professional way of update and redraw such things like a timer or other animations of custom controls?
There is a background worker, you can read more about it here: https://msdn.microsoft.com/en-us/library/cc221403%28v=vs.95%29.aspx?f=255&MSPPError=-2147217396
Or you can use async/await - a Task based asynchronous approach
https://msdn.microsoft.com/en-us/library/hh191443.aspx
Related
I am working on Windows Forms application, that needs to start a big amount of async tasks. In other words, a small control panel, that will start and monitor the process (Task), started from this UI control panel.
But, after some researches, I realized, that there is no easy way to monitor variables' values from inside the Task.
Currently, I am looking for any way of monitoring progress in Task<>. Thinked, that I might start minimized process (Just basically copy&paste the whole code from task, that I need to start to a ConsoleApp, but, as always, that is not easy to catch the data from variables).
Any thoughts, how to do it correctly?
And found IProgress interface, but I didn't actually get the idea how it works. Just no examples.
There is a form. On the form there is a pictureBox docked on all form's surface.
When app starts, for a second a form with white surface is displayed, then
the picture is shown.
how can i get rid of that 1s white form?
Sounds like you are doing something time consuming in form_Shown event. Call Form's Refresh() method as the first thing in form shown -event and it will first draw the form, then do the time consuming things
You have not stated when you are loading the picturebox with your image. But I would try making your picturebox visible at the end of your Form_Load event or in your Form_Shown event.
Sounds more like a threading problem to me. I guess that your UI thread is doing too much work and cannot update the UI often enough.
Do all of the following:
Make sure loading and processing any data (including the images) is NOT located in the constructors.
Move that code into the appropriate FormLoad() event handler methods.
Implement loading of the images so that it runs a separate thread.
You can find some advice in this MSDN article: Give Your .NET-based Application a Fast and Responsive UI with Multiple Threads
If you are working in a .NET 4.x version, you can also use the Task Parallel Library to make working with multiple threads easier.
The upcoming .NET 4.5 also offers the even more comfortable await and asyc keywords: Asynchronous Programming with Async and Await.
I'm developing a win forms, continious 24/7 video player and I need some help deciding which Threading class suits my needs. This is my first multithreading attempt. I'm familiar with the BackgroundWorker class, couple of properties to set and events to handle, but I have not delved much deeper to the rest of the Threading classes. I'm considering my application's tasks simple but I might be wrong regarding the level of simplicity.
Here's some of the player's functionality:
Reads an SQL Server database and displays data on a DataGridView.
Adds DataRepeaterItems to a DataRepeater control (serving the purposes of a playlist) from DataGridView CellMouseDoubleClick event.
Loads the video sound on a secondary (preview) player with each CellMouseClick event.
The player's PLAY button loads and plays the video on a video player instance, updates some labels, adds a DataRepeaterItem on a secondary DataRepeater (serves the role of a playlist history) and deletes the current DataRepeaterItem, the one currently playing, from the playlist DataRepeater control.
There are a couple more buttons deleting all DataRepeaterItems and moving the Items up and down the list.
Later something like an auto-playlist functionality will be added, populating DataRepeater control with multiple items based on some criteria. (24/7 player)
Why do you guys think? Is it safe to go with BackgroundWorker or is it better to look at the other Threading classes?
Well if you are using .Net 4 or above I would recommend Task class rather than Background Worker
Task class is certainly improvement over BackgroundWorker. It has more flexibility. You can write more elegant code using Task than BackgroundWorker. For example you can avoid event handlers involved in BackgroundWorker by using concept of task continuation.
Definitely use Tasks if .NET 4 is available. They have so many useful features:
The ability to queue more work for the same Task when the current work has completed.
Marshaling of exceptions to the calling thread.
"Smart" scheduling.
and many more. Check this source for a good overview of what you can do.
In an application that I'm writing I have to load a lot of data in a listview after I push a certain button. Because this might take a while, I want to show a simple loading-screen to the user.
I did this by creating a very simple form with 1 label on it. I do a Form.Show() before I start fetching the data, and a Form.Close() once it's finished. This works partially: I get the loading screen, it closes at the right time, but as you can see the label isn't displayed.
I know I should program this loagind screen-problem with the help of a BackgroundWorker, but I'm not a very experienced C# programmer so if I could just somehow force the label to display I would be more than happy.
Is it possible to force this label to display immediately, or do I have to find another solution?
The UI Message pump has no time to update the label / refresh the screen since the loading process takes up all resources.
You can try to force the update of the label, by calling Application.DoEvents(). Although, using 'Application.DoEvents' is imho a 'code-smell', and thus indicates that the design is not that good. So, it should be avoided.
Therefore, using DoEvents is surely not the best solution to this problem. It is merely a symptom-fix.
You've mentionned the best solution already yourself: use another thread to load all the data. (You can indeed use a BackGroundWorker for this purpose, since this will abstract away a lot of plumbing code).
BackgroudWorker is very easy to use , even c# is very powerful and simple langugage
See Here
i am almost sure that , you would not need any more help with BackGroundWorker but if you have any query , you canm always post on SO ?? Collabartive Intelligence is what SO is?
How can I animate the tray icon while the main application is processing? I already tried the suggestion here, but the icon does not animate during process. I also tried putting the animation in a separate thread but still no luck.
There's a very good example of an SystemTrayNotifyIcon with Event Generator over at the CodeProject .
I've looked at this project before but didn't end up using it, due to my artistic inabilities, but the example given does the job quite well.
Take a look at http://www.codeproject.com/KB/shell/IconAnimation.aspx and http://www.c-sharpcorner.com/UploadFile/nkumar/AnimateSysteminCSharp11292005235157PM/AnimateSysteminCSharp.aspx for a more recent example.