简体   繁体   中英

“Please Wait” Loading bar in Windows Phone App C# until sql statement is finished

I have a Problem with my Windows Phone App (Im not really experienced in developing C# WP8 Apps :/). I tried to find solutions in stackoverflow, but didn't found the right answer yet.

So, here is a Short Review of the Problem:

Im doing some SQL work in the Code and after this is done, the User gets a List of the Results. That fine so far, but some querys take more time and the User don't gets a response like "Please wait". I tried to implement it, but it doesn't work.

What did I do?

In the main Method "MainPage()" I initiate the SQLiteConnection with:

public SQLiteConnection conn = new SQLiteConnection(DB_PATH);

The User makes an input and press a Button to start a search (sql query). The method that is triggered by pressing the button is called "ButtonSearch_Click". The first thing I do in this method is to set a Progressbar visible that says "Please wait":

Progressbar.Visibility = Visibility.Visible;
PleaseWaitBlock.Text = "please wait...";

After this I do some work with the inputs of the User that is not really necessary in this case. Then I create a sqlCommand Object:

SQLiteCommand sqlComm = new SQLiteCommand(conn);

and use the CommandText method to enter a query, something like:

sqlComm.CommandText = "SELECT * FROM abc WHERE (column_1 LIKE '" + input + "%')";

Notice: "input" is the input of the User.

Now I execute the query by doing:

var queryname = sqlComm.ExecuteQuery<table>();

after that I loop over the queryname:

foreach (var item in queryname)
{
 // I do some work here
}

Then I make the resultbox visible for showing the results:

resultbox.Visibility = Visibility.Visible;

That's it. The Problem is that the Progressbar won't be shown this way. If I try the same without the sql query it will show the Progressbar, so that means the progressbar works.

How can I make the progressbar visible, until the sql-statement is finished?

Thank you for the answers!

Kind Regards, Youpi

It would seem that you are performing work (the SQL statement) on the same thread as the UI; this prevents the UI from doing anything (such as displaying the progress bar) while the work is being done.

There are a number of different ways you can address this, but in principle, they all come down to the same thing: perform all time-consuming work on some other thread than the UI; never block the UI thread.

One possibility might be to use BeginExecuteReader instead of ExecuteQuery, and process the results in the AsyncCallback method. If the work you do on the query results will complete quickly, this solution should suffice.

Another possibility would be to launch everything you do in response to the search click in a background worker thread. This would be necessary if the processing you do on the query results will be time-consuming. When using this approach you need to bear in mind that you cannot interact with UI elements on a non-UI thread; your worker thread action will need to do something like a BeginInvoke for a UI element in order to populate a UI control (and turn off the progress bar).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM