如何在“后台"运行服务器端操作? [英] How to run a Server-side operation "in background"?

查看:87
本文介绍了如何在“后台"运行服务器端操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我知道已经问过并回答了在Lightswitch的服务器端运行Operations(查询,存储过程等)的问题,实际上,我的问题是如何进一步实现该实现.

I am aware that the question to run Operations (queries, stored procedures, etc.) on server-side in Lightswitch has been already asked and answered, in fact my problem is how to go further in that implementation.

我通常会创建一些虚拟"对象,在ApplicationData中的操作实体上,我使用ServerGenerated类中的_Inserting方法,并从那里运行任何类型的查询.

I usually create some "dummy" Operation entities in the ApplicationData, I work on the _Inserting method in the ServerGenerated classes and I run any kind of query from there.

但是,现在我需要处理一个长达10分钟的操作,Lightswitch会留下请稍候...".图标,这并不令人愉快.我当然可以调整应用程序的超时时间,但我想获得 这种行为:

However, now I need to deal with an operation that can last up to 10 minutes, and Lightswitch would be left with the "Please wait..." icon, which is not pleasant. I can of course adjust the timeout of the application but I would like to obtain this kind of behavior:

-用户创建一个"Operation". (参数:开始时间,结束时间,结果)

-The user creates a "Operation" (parameters: StartTime, EndTime, Result)

-用户运行" 操作"并将参数StartTime设置为DateTIme.now

- User "Runs" the "Opeation" and parameter StartTime is set to DateTIme.now

-用户是否关闭了应用程序,但操作仍在后台运行.

- User closes the app or not, but the operation continues to run in background.

我试图展示我所有的方法,但没有一个起作用.

I try to show all my approaches, none of them works.

我尝试过的基本方法是:

The basic approach I tried is:

partial void PVA_BatchOperations_Inserting(PVA_BatchOperation entity)
        {
           Thread oThread = new Thread(new ThreadStart(() => { MakeSave(entity); })); //TODO: Pass reference ? 
            oThread.Start();
}

和MakeSave是这样的:

and the MakeSave is like this:

        private void MakeSave(PVA_BatchOperation entity)
        {
            Thread.Sleep(10000);
            Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
            {
                DataWorkspace batchDW = new DataWorkspace();
                PVA_BatchOperation op = (from ops in batchDW.ApplicationData.PVA_BatchOperations where ops.Id == entity.Id select ops).FirstOrDefault();
                op.timeEnd = DateTime.Now;
                op.result = "Complete";
                batchDW.ApplicationData.SaveChanges();
            });
            
}

 在当前线程上创建数据工作区是无效的,因为它不是由LightSwitch运行时创建的."

 "It is not valid to create a Data Workspace on the current thread because it was not created by the LightSwitch runtime."

它看起来可能很复杂,实际上是,但是我也尝试过:

It may look complex, in fact it is,  but I also tried:

        private void MakeSave(PVA_BatchOperation entity)
        {
            Thread.Sleep(10000);
  PVA_BatchOperation op = (from ops in this.DataWorkspace.ApplicationData.PVA_BatchOperations where ops.Id == entity.Id select ops).FirstOrDefault();
                op.timeEnd = DateTime.Now;
                op.result = "Complete";
                this.DataWorkspace.ApplicationData.SaveChanges();
 
            
}

哪个让我知道:在当前线程上访问该对象无效."

Which gets me: "It is not valid to access this object on the current thread."

基本思想是获得用户启动"行为.操作,然后在完成时将完成时间写入实体.我似乎无法实现这种行为.

The basic idea is to obtain the behavior that the user "launches" the operation, then when finished it writes into the entity the completion time. I don't seem able to achieve this behavior.

有什么建议吗?

提前谢谢

乔瓦尼·麦吉尼

推荐答案

我认为通过web-api进行命令没有此限制:

I think that commanding via web-api has not this limitation:

用于基于Web API的命令和报告的可下载光开关解决方案


这篇关于如何在“后台"运行服务器端操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆