C# Label not showing at loading screen - c#

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?

Related

Release button while other button execute methods WPF 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#

How to keep UI unlocked while loading child screens in winform

I have very generic question. I tried google it but couldn't find anything good.
So the question is: I have winform application where based on option selected from menu bar, child form/view is loaded and these child forms are used to display reports.
in order to generate report, these child form contains report selection criteria like date, name etc and these value comes from DB. based on values available in DB, we add controls in the view.
Problem comes when screen takes lots of time to load, it freeze UI/application during loading that I don't want.
I also tried run tasks async but since it is using view during the processing, this option failed.
ThreadPool.QueueUserWorkItem(delegate { method.Invoke(presenter, results); });
presenter is my viewpresenter and results is parameters to invoke it. application concluding presenter name at run time and calling its default method using methodinfo.invoke().
Is there any other way to achive the same.
Point: I cannot redesign the whole application to separate out complete DB logic from UI logic as it is legacy application and this might break the application.
If "freezing" is the problem, you could use doevents, see here: Use of Application.DoEvents()
Or, you could go the really old fashioned, vb6 way: run the code that freezes your UI synchronously, but from a modal window shown above your ui. You can event present the user with the progress indicator.

Possible to show a loading window when datagridview is filled with data?

My app consists of 3 tabs, each tab has a DGV, tab 1 and 2 do not contain that much data within the DGV but in tab 3 it can be between 100-5000 rows
The problem im having is that when i start my app, and move to tab 3 it takes a while before anything is displayed..
Im looking for a way to display a loading form to the user while the data is being added to the DGV..
How would i accomplish something like this?
Start loading data on a background thread and monitor the progress(displayed as progress bar).If you are doing it on main thread than you'll hang your interface.
Theres one good article but it is in VB.
Long running operations should be running in a seperate worker thread, commonly BackgroundWorker class. This prevents the UI from locking up during said operation. You can also choose to display a loading dialogue while this thread runs.
The other answers here are correct. But since you specifically asked about a loading window, I remembered a very nice example in Code Project.
In a project I was involved with, we took this example and made it independent. We added static methods called Start and Stop, and the Start method would create a thread, and then load the window, while the Stop method signaled the form it needs to close gracefully.
Hope this helps!

Creating a Form and using Form.ShowDialog on a background thread

Using Winforms,
If I'm on a thread that is not the "main" ui thread can I safetly
Create a Form,
Call ShowDialog on that form
It seems like I would be obeying the rule of:
Winforms controls must be modified on the thread they were created in.
From some of the reading I've done it seems like ShowDialog will create its own message pump so that I don't need to worry about the Form being created on a thread that already has a message pump.
The background of the question is that I get a callback where I'd like to display some Winforms UI, but I don't have access to any other UI that I could use to Invoke to the main thread.
That's roughly correct, albeit that it is pretty important that you call the thread's SetApartmentState() method to switch the thread to STA. Important for many UI operations, including the clipboard, drag and drop and the shell dialogs. And that you usually have a crummy Z-order problem when the form that you create on the thread is not in the foreground and hides behind another window. Or has the nasty habit of actually do move in the foreground when the user doesn't expect it and grab a mouse click or keystroke unexpectedly.
These are hard problems to fix, they do make your app flaky. There's no good reason to not have a reference to invoke to, you can also pass it to the class some way some how. You've always got Application.OpenForms[0] to fall back on, if really necessary.
Yes, you can do that, but if you want the dialog to actually act like a modal dialog (i.e., block the parent Window, which I assume you want since you are calling ShowDialog) then be prepared to be disappointed.
What problem are you actually trying to solve here. It sounds like you want a modal dialog that doesn't block, which is a bit strange. If you explain the problem at hand there may exist a solution you have not yet considered.

DataGridView blocks the UI Thread

I am working on application which uses a DataGridView control. Since there is a lot of data I use the VirtualMode to use paging, but even with this feature this take a while to update DataGridView. So I created a control which spins and entertain an user. Unfortunately there are some issues with this solution, namely when the entertainment control spins the UI Thread does handle any of requests from it because it's so busy with this DataGridView.
What should I do to access the UI Thread even if it is so busy?
How do you do to "entertain" an user when you use the DataGridView or is there any main to prevent UI Thread from blocking by DataGridView?
btw. I know that Application.DoEvents probably could help, but I prefer not to use it.
Drop the priority of the DataGridView thread relative to the UI one or just throw in the occasional Thread.Sleep in the DGV data load function.
Combined with a little tuning of your page sizes that should sort it.
I'm not sure what your implementation looks like, so forgive me if you already know this: Setting the VirtualMode property to "true" doesn't make the DataGridView automatically populate itself via just-in-time loading. Rather, it just gives you as the developer the ability to do so. If you're using virtual mode but still populating the entire thing at once, then you really aren't gaining anything. Instead you'll have to implement your own paging algorithm. This is actually pretty easy to do. Please take a look at this MSDN topic for an example. Hopefully that helps!

Categories

Resources