简体   繁体   中英

Silverlight WCF Data Services Save Sequentially

Im trying to create a method that allows me to save in a sequential flow ie example code:

Private sub BlahWithSave()

'PERFOR ACTIONS
Blah()
Blah2()

'SAVE CHANGES TO DB
General.SaveState() 

'CARRY ON PERFORMING ACTIONS AFTER SAVE CARRIED OUT
Blah3()
Blah4()

End Sub

Currently Ive been mucking around with ManualResetEvent and AutoResetEvent but havent got it going so I thought i would ask. Here is my last iteration of the SaveState method:

#Region " SAVE CHANGES "
    Private Shared ManualWaitEvent As System.Threading.ManualResetEvent
    Public Shared Sub SaveState()
        ManualWaitEvent = New System.Threading.ManualResetEvent(False)

        MyDataContext.BeginSaveChanges(Sub(result As IAsyncResult)
                                       ManualWaitEvent.Set()
                                       Deployment.Current.Dispatcher.BeginInvoke(Sub()
                                                                                     Dim     response As DataServiceResponse = MyDataContext.EndSaveChanges(result)
                                                                                 End Sub)
                                   End Sub, MyDataContext)
        ManualWaitEvent.WaitOne()
    End Sub
#End Region

The problem is that it just stops at the ManualWaitEvent.WaitOne and never gets into the BeginSaveChanges callback. Any ideas on where im going wrong? Or another idea on how I can acomplish this.

Thanks

In Silverlight you must not block the UI thread, otherwise the application won't be able to process any user input, networking and bunch of other things. It will also freeze the browser window. All in all a really bad user experience.

The suggested way to program this is to use callbacks, which means your code gets split into several pieces (either several methods, or delegates).

Take a look at the async CTP for Visual Studio (http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=9983) which makes some of this much easier. It allows you to write the code almost like you did, and the compiler does all the splitting into callbacks for you.

If you really need to block, then you could start a background thread in SL and do it there, but then you have to remember that the callbacks from async APIs (like BeginSaveChanges) will be executed on a different thread (depend on the API, sometimes it's the UI thread, sometimes it's another background thread).

Don't try to force it to become synchronous but use the call back functions.

For example: you could split up the original call into two method calls and set the second part as the callback of the save. You'll need to update how you save of course, but that shouldn't be too hard.

Private sub BlahWithSave()
    'PERFOR ACTIONS
    Blah()
    Blah2()

    'SAVE CHANGES TO DB
    General.SaveState(BlahWithSavePart2)
End Sub

Private sub BlahWithSavePart2()

    'CARRY ON PERFORMING ACTIONS AFTER SAVE CARRIED OUT
    Blah3()
    Blah4()

End Sub

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