This project is read-only.

Re: I am having trouble with the async operation of the interface [sqlwinrt:4...

Dec 13, 2013 at 3:57 PM
Thank you for your response , I am studying this information and reviewing my code. This helps a lot.
I am having one other problem I encountered this morning. In Visual Studio I got a message to update Sqlite for Windows Phone and so I did - what I have now in my references is
Sqlite for Windows Phone(Sqlite.WP80,version 3.8.2)
I tried it and of course I had problems so I went back to your website (CodePlex) and read that I had to run the PowerShell script and that worked. I also downloaded the latest source code for the Windows Phone and copied it into my SQliteWinRTPhone project subdirectory - actually I brought this up and built also.
I went back to my project fixed the reference and included the SQliteWinRTPhone project and then built everything. It seemed to work.
However , when I try to run it in the emulator I get the following error when I try to open a database.
I am trying to execute
db = New Database(localFolder, "RealEstateProLite.sqlite")
and I am getting this error
An exception of type 'System.TypeLoadException' occurred in RealEstatePro.DLL but was not handled in user code
It worked fine before I did the upgrade and I have the old version before upgrade working on my laptop.
Any ideas where I went wrong?
Thanks again,
Jerry
In a message dated 12/13/2013 6:32:13 A.M. Eastern Standard Time, [email removed] writes:

From: andywigley

Jerry,

A line that contains a call to an async method acts like a synchronous call as long as you call it using the await keyword – so within a method, there is no special difficulty with using async methods.

If you create a method yourself that contains calls to other async methods, and you change the return type of the method to be Task or Task<T> (for example, if a method returned an int before, change it to return a Task<int>) then you can call your new method again using await and nothing bad will happen! It will work the same as synchronous code.

The difficulty comes with code that sits inside an event handler method (so that’s a significant percentage of most XAML-based apps). An event handler method is essentially a method that returns void and which the caller calls in a ‘fire-and-forget’ fashion. There’s actually no way of modifying the way that event handlers are called to somehow work in the await keyword into there. The result is that, the caller fires an event – for example, the framework fires the Application_Launching event – and your code in your Application_Launching event handler starts to execute in a synchronous fashion up to the point where it encounters a call to an async method, such as await mydatabase.OpenAsync(). At that point, your event handler method suspends and returns to the caller which carries on as if the event handler code had executed to completion. Your event handler code will resume later on when the database open has completed, but by then the original caller – the framework – doesn’t care and has gone onto execute other things, such as loading your page and executing the code for that. This is why you can sometimes get sequencing problems.

The answer is to implement some kind of gatekeeper method to ensure that the database is fully prepared and open before anyone tries to read from it. Download the sample on my blog post: SQLite-WinRT:Database programming on Windows Phone and Windows 8 for an example of how I did it – look at the GetDatabaseAsync() method in app.xaml.cs – that waits until the LoadDatabase method has set the DBLoaded ManualResetEvent.

See my other post that talks about this problem some more: Beware the perils of async/await in application lifecycle event handlers (in fact in *any* event handlers)

Andy

Dec 13, 2013 at 3:58 PM
Thank you for your response , I am studying this information and reviewing my code. This helps a lot.

I am having one other problem I encountered this morning. In Visual Studio I got a message to update Sqlite for Windows Phone and so I did - what I have now in my references is

Sqlite for Windows Phone(Sqlite.WP80,version 3.8.2)

I tried it and of course I had problems so I went back to your website (CodePlex) and read that I had to run the PowerShell script and that worked. I also downloaded the latest source code for the Windows Phone and copied it into my SQliteWinRTPhone project subdirectory - actually I brought this up and built also.

I went back to my project fixed the reference and included the SQliteWinRTPhone project and then built everything. It seemed to work.

However , when I try to run it in the emulator I get the following error when I try to open a database.

I am trying to execute
db = New Database(localFolder, "RealEstateProLite.sqlite")
and I am getting this error
An exception of type 'System.TypeLoadException' occurred in RealEstatePro.DLL but was not handled in user code

It worked fine before I did the upgrade and I have the old version before upgrade working on my laptop.

Any ideas where I went wrong?


Thanks again,

Jerry
Dec 13, 2013 at 4:10 PM

Oops – I screwed up. Thanks for the feedback.

I’ve just reverted a change I made that was made for the wrong reasons anyway. Please pull down the source again and hopefully you should have better luck this time.

Andy

Dec 13, 2013 at 4:35 PM
Hi Andy,

I am not sure it was your error.

Here is what I found.

I found that in my project references , it was pointing to the wrong subdirectory containing the sqlitewinrt.winmd file , it was pointing to the old one not the newly generated one so I changed this and now it works fine.

Do I still need to pull down a new version of the source?

Thanks Jerry
Dec 13, 2013 at 4:40 PM

Good to know!

No – the previous version clearly works OK. I just pulled out an additional constructor which upon thinking about it, adds no value. So just carry on with what you’ve got!