Image loading stops

I have a form for windows. I put one boot image in a PictureBox

When I upload the form, I set

 PictureBox1.Visible = false; 

While I fire the button click event, I set

 PictureBox1.Visible = true; 

But in this case, there is some code to retrieve data from the database using the stored procedure.

When it moves to the code to retrieve data from the stored procedure, loading of the loaded image will stop.

Must not be. It should appear as loading. I used .gif to upload the image.

How can I solve this problem?

+4
source share
3 answers

Every time you have a long call inside eventHandler, you should use BackgroundWorker ! BackgroundWorker can run async code, and so your button_click eventHandler will end right after the worker starts.

 // add a BackGroundWorker bwLoadData to your form private void YOURBUTTON_Click(object sender, EventArgs e) { PictureBox1.Visible = true; bwLoadData.RunWorkerAsync(); } private void bwLoadData_DoWork(object sender, DoWorkEventArgs e) { // access your db, execute storedProcedue and store result to e.Result = YOUR_DATASET_RECORDS_OR_ANYTHING_ELSE; } private void bwLoadData_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Result != null) { // eg show data on form } else { // eg error message } } 
+4
source

Most likely, while you start the stored procedure, the user interface thread is blocked. You can use BackGroundWorker to retrieve data from a database that creates another stream and does not block the main stream.

Or you can create the stream manually and use it to retrieve data from the database. In window forms, as a best practice, it is best to use a different thread to make external system calls so as not to block the user interface thread.

using background desktop

+2
source

A possible reason for this may be sharing a single stream by loading an image and retrieving data. So you can try with multi-threaded or asynchronous calls to get data. Sorry for the previous answer about the ajax / javascipt web worker, I completely ignored the specified window form.

0
source

Source: https://habr.com/ru/post/926212/


All Articles