An effective way to display multiple labels - vb.net

I am looking for an effective, quick way to show multiple labels in a quick fashion. What's a good way to do this other than replacing all the .text properties of the labels one after another? It is my understanding that whenever you update a .text property the UI has to be repainted which will add to latency if you have 50+ controls to update.
I know threading is an option but when I tried this I didn't see much of a difference as I wasn't able to load 2 labels at once, I still had to wait for the UI thread before the labels would update. What other ways are there to effectively load 50+ labels quickly? The way it is now takes quite awhile(3-4s) and I feel this could be lowered. The information is being taken from a backend system so I don't have the option for datasets/etc.

You have to separate the code that retrieves the data from the database (which you do using a background thread) and the code that updates the UI (which should happen as quickly as possible because you don't want to block the UI for too long).
My suggestion would be to use the BackgroundWorker component to do retrieve the data for all 50 labels. When the BackgroundWorker raises the RunWorkerCompleted event you call the forms' SuspendLayout function, update all the values of the labels and then you call ResumeLayout. SuspendLayout stops a control from redrawing until you call ResumeLayout.
More info: BackgroundWorker, SuspendLayout, ResumeLayout

Related

How do I show a “Loading . . . please wait” with gif image message in vb.net for a long loading form?

vb.net
I have a form(called Form1) that is very slow because it contain datagridview how load data from sql server.As a result the form takes a long time to loaded.
So I create a form_Wait and in the load event of Form1 I put
Form_wait.showdialog()
Load_datagridveiw1("Select * from table1")
Form_wait.close()
My problem is the Form_Wait will be appear but the gift inside it (rectangular progress bar) is not showen ?
I used VS 2015
You cannot meaningfully show and animate GIF in a same thread as your UI sits. You would have to use a BackGroundWorker, which is basically a separate thread. Then the animation will work smoothly and the UI thread will run fine too. There are plenty of BackGroundWorker tutorials on the web.
But a huge warning horn rings reading your words. If it is such a huge amount of data causing the slow loading, I would strongly suggest to reconsider how do you fetch the data. Introducing meaningful filters and pagination are basic ways to tackle this.

Is there a way to execute code in VB.NET after *any* event

In VB.NET, is there a way to execute code after any event for which I have written an explicit handler, other than placing a call as the last line of each individual event handler? In ASP.NET I can put code in PreRender but there is no equivalent of that in VB.NET because there is no page life cycle.
I understand that for desktop apps the model is very different and that PreRender doesn't fit the desktop model but I hoped it would illustrate what I meant. In ASP.NET I often determine whether controls are visible, or enabled, in PreRender, after events have been processed and the underlying database has changed as a result. It seems reasonable to want to do something similar in VB.NET - multiple events can alter the underlying database, and multiple controls may need refreshed as a result, so write a routine that determines the visibility and enablement of the controls, and call it after explicitly-handled events have been dealt with.
I've tried the form validating/validated events but can't make them work.

Exporting thousand lines from datagridview to excel using vb.net

Hi I'm currently having a datagridview with thousand (about 3000+) rows of record and 9 columns. I was using this method I found to export the datagridview to excel. There was no problem exporting hundred of rows but when it comes to thousand of rows, it hangs and it wouldn't respond to anything.
What problem could it be? and if there's any other way that is faster/better than this?
Thank you!
The code is being executed on the UI thread so of course your application would freeze while it's executing. If you want the UI to remain responsive then you'd have to execute the code on a secondary thread. The issue there is that the data is coming from a control, so that part at least must be executed on the UI thread.
I would suggest that one possibility is to use a BackgroundWorker and do the work in the DoWork event handler. You can set up a loop that calls ReportProgress, which raises the ProgressChanged event on the UI thread and allows you to get the data in pages, then write it out to your spreadsheet on the background thread. I'll follow up with an example.

How to update a NSTextField Value during an action?

I am running a lengthly task in an Action and I would like to have a display of where I am at. For that I created a Text Field and I tried it with setStringValue:
[textField setStingValue: [NSSting stringWithFormat:#"%ld",currentValue]]
The code works but unfortunately it is not updating the NSTextField after every iteration but rather when the whole Action is done.
What am I doing wrong?
This is because applications with the Cocoa framework use an event loop to perform operations, and events occur in a completely serial fashion.
An event is basically any kind of action that the framework designer could not predict or found convenient to have run in a delayed manner. Since you can't predict when clicks will be performed, they need to be considered events; and for efficiency reasons (since you don't want to repaint a component multiple times if you don't need to), the repaint actions are events too.
Your action runs in response to a user event (for instance, a click on a button is an event) and therefore blocks all other events waiting in the queue until it's complete. However, components are repainted in response to a different, framework-triggered event, and as such the text field must wait until your action completes to repaint itself. This is why you cannot visually change the value of a text field from inside an action.
In order to notify your user of the progress of your task, you'll need to run it on a different thread. There's a lot to say about threads, so you should probably read some about them. I'm also sure that there are plenty of examples of how to run a long action in a background thread and update the UI accordingly for Cocoa all over the Internet.
When you click on a UI component, and it enters the Action block, the code is running on the main thread, the same thread that is painting the UI. If you run a long running operation in that block, it isn't going to paint until you are done because it is busy doing whatever you have it doing - you have hijacked the paint thread.
As said elsewhere, you need to spawn another thread, and then have the new thread perform the long running operation, and occasionally send messages to have the UI be updated by the main thread.
As a next step, go read the Apple documentation on NSThread, specifically:
+ (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument
Be aware that threading is a non-trivial domain area, and be ready for some wierd behavior if you aren't careful.

How to call multiple subs with a BackgroundWorker

I have not yet used BackgroundWorker but believe I need to use these in my code.
My app does a lot of database work, running many SQL queries in sequence. My problem appears typical, that the main form becomes unresponsive.
I want to be able to display progress using a progressbar and a toolstripstatuslabel. I am doing this already, but without the BackgroundWorker.
My code is - unsurprisingly - divided into a several subs, which are called in sequence by the main form.
All the examples I have seen include just simple BackgroundWorker DoWork events. What is the correct way to deal with calling other code? Just call the modules as usual in the DoWork event? I do understand these must contain no user interface code.
Am using VB.Net 2010
Thanks!