简体   繁体   English

Silverlight WCF 数据服务按顺序保存

[英]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.目前我一直在处理 ManualResetEvent 和 AutoResetEvent 但还没有开始,所以我想我会问。 Here is my last iteration of the SaveState method:这是我对 SaveState 方法的最后一次迭代:

#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.问题是它只是停在 ManualWaitEvent.WaitOne 并且永远不会进入 BeginSaveChanges 回调。 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.在 Silverlight 中,您不能阻塞 UI 线程,否则应用程序将无法处理任何用户输入、网络和其他一些事情。 It will also freeze the browser window.它还将冻结浏览器 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.看看 Visual Studio 的异步 CTP (http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=9983),它使其中一些变得更容易。 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).如果你真的需要阻塞,那么你可以在 SL 中启动一个后台线程并在那里执行,但是你必须记住来自异步 API(如 BeginSaveChanges)的回调将在不同的线程上执行(取决于 API,有时是 UI 线程,有时是另一个后台线程)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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