Release button while other button execute methods WPF C# - c#

I have the following problem:
I have to Buttons.
After a click on the first button the program start a while loop.
I like to leave this while loop after click on the second button.
How I can do this?
Have anyone an idea?
Thank enter image description here

You need to use multiple threads to accomplish this. I suggest reading up on the subject and looking at some examples.
There are a number of different classes/systems that can do this, because newer, better ones were developed over time. BackgroundWorker is an example of an old system that I wouldn't recommend for new development. I believe the current best practices (at the time of writing) are to use the Dispatcher (in WPF) as well as async/await.
Here is some documentation:
MSDN - Threading Model
Building Responsive UI using Async Await in C#

Related

Windows Forms process animations

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

Video Player Threading

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.

how can I achieve an event without a click or sender or anything?

Say for example if I want to see if ms word is started but I don't want to make a timer ticking every millisecond or if the user doesn't click a button to notify like crazy. Is there some system way to do this? Like ms word starts and it sends some signal to my application? What I am trying to accomplish is actually more sophisticated but the ms word example would suffice... .net or c++/qt/mfc solutions are what I am looking for. 10x!
This article describes a method, using WMI, for receiving an event when processes are added, removed or modified:
http://weblogs.asp.net/whaggard/archive/2006/02/11/438006.aspx
It should be a simple step from this to check if it is MS word or not based on the executable name.
You can use WMI to monitor process creation:
http://weblogs.asp.net/whaggard/archive/2006/02/11/438006.aspx
This may overlap with app starting, but not exactly the same thing.
If you wanted to do something with the app once it started the .net UI Automation could also tell you when windows are created and other such events.
Alternativly, MSAA, Windows Automation, WindowHook are other APIs you might use.

How to Pause and resume a process in windows application

I have created a windows application which is used to read every row from spreadsheet and pass each row of data to stored procedure. Assuming this work takes place for 2 hours(since there are large number of data's). And i have placed two buttons pause and cancel. On click of pasue, I need to pause the work of updating the work and need to resume when the button is pressed again. Please provide some inputs regarding this. I am using Visual studio 2003(with 1.1 framework) and SQL 2005. I am using c# for programming
Could you guys please explain how to put the main thread in background(while paused)... Please respond soon as it is urgent.
The operation of reading rows should be done on a separate thread. On that thread you should have something like this (note it is just the idea and not an exact code):
while(!mCancel)
{
if(mPaused==true)
{
Thread.Sleep(1000);
}
else
{
//do here the row reading
}
}
The mCancel and mPaused are global flags that you set to true or false from the UI buttons. When you press Pause button you set mPaused to true. At this point the loop will sleep for 1 sec and then reiterate and check for the flag again and if needed will sleep again.
Another way would be to use the ManualResetEvent class. See link for details. YHis class is used for signalign between threads, like the main thread and a worker thread for instance. Here is a sample.
Edit: The definitive guide to multi-threading by Joseph Albahari. Read this and all your threading problems will be solved.
In order to track the state of your pause button, you need to let the application process incoming messages from time to time. The implementation depends on the programming language being used and possibly the application framework.

C# Label not showing at loading screen

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?

Categories

Resources