ASP.net 中的互斥访问 + C#.net
目前,我面临着一个经过大量工作和搜索后无法解决的问题,我之前问过类似的问题,但没有得到任何答复,可能是因为我没有正确询问,所以我删除了该问题。
好的,我正在使用 MailBEE.net 对象库下载电子邮件,它工作正常,只是如果在上一个调用仍处于下载阶段时再次调用下载方法,则开始下载邮件的两个副本,这是错误的。
在 ASP.net 页面上,我正在调用一个 ASHX 处理程序来下载电子邮件
public class sync : IHttpHandler {
public void ProcessRequest(HttpContext context)
{
ApplicationData.layer Functions = new layer();
context.Response.ContentType = "text/text";
int messageCount = Convert.ToInt16(context.Request.QueryString["messageCount"]);
if (Functions.SyncMail("email", "user", "password", "pop.gmail.com", messageCount) == "Successful")
{
context.Response.Write("New Messages Downloaded.");
}
//context.Response.Write("Hello World");
}
}
我正在使用来自另一个 (ASPX) 页面的 Jquery 调用上述 ASHX 处理程序
<script type="text/javascript">
$(document).ready(function() {
asyncLoad();
});
function asyncLoad() {
document.getElementById("CustomerDetails").innerHTML = "<img alt='' src='Images/ajax-loader.gif' />" + " <span>Downloading New Messages...</span>";
$('#CustomerDetails').load("sync.ashx?messageCount=" + "10");
callAgain();
}
function callAgain() {
setInterval(asyncLoad, 20000);
}
</script>
目的是在一段时间后(20000 延迟)继续调用sync.ashx检查新消息,问题是如果sync.ashx的一个调用正忙于下载消息并且在此期间进行了新的调用,它会开始下载相同的消息副本,因为它在数据库中找不到电子邮件的ID之前的电话即将拨打。
我需要某种互斥的访问,如果一次呼叫正忙于下载消息,则不应进行另一次呼叫。
像Where IsAlreadyDownloading这样的东西
if(IsAlreadyDownloading == False)
{
Functions.SyncMail(params)
}
是一个全局标志或互斥体,一旦一个调用开始下载,就应该设置为True,一旦完成下载或发生一些异常,表明可以安全地进行另一次调用,则应该设置为False。
由于它是一个 ASP.net 应用程序,我们不知道用户何时会离开开始下载调用的页面,以及何时会导航回该页面,因此是否应该再次调用下载处理程序。
我不知道我解释得是否正确,但我希望有人能理解。谢谢。
Currently I am facing a problem which I am unable to solve after a lot of work and search, I asked a similar question before but didn't got any reply maybe because I didn't asked it correctly so I deleted that question.
Ok I am downloading emails using MailBEE.net Objects library and it is working fine except that if downloading method is called again while a previous call is still in downloading phase then two copies of messages start to download which is wrong.
on an ASP.net page I am calling an ASHX handler that downloads the emails
public class sync : IHttpHandler {
public void ProcessRequest(HttpContext context)
{
ApplicationData.layer Functions = new layer();
context.Response.ContentType = "text/text";
int messageCount = Convert.ToInt16(context.Request.QueryString["messageCount"]);
if (Functions.SyncMail("email", "user", "password", "pop.gmail.com", messageCount) == "Successful")
{
context.Response.Write("New Messages Downloaded.");
}
//context.Response.Write("Hello World");
}
}
I am calling the above ASHX handler using Jquery from another (ASPX) page
<script type="text/javascript">
$(document).ready(function() {
asyncLoad();
});
function asyncLoad() {
document.getElementById("CustomerDetails").innerHTML = "<img alt='' src='Images/ajax-loader.gif' />" + " <span>Downloading New Messages...</span>";
$('#CustomerDetails').load("sync.ashx?messageCount=" + "10");
callAgain();
}
function callAgain() {
setInterval(asyncLoad, 20000);
}
</script>
The purpose is to keep on calling sync.ashx after some time (The 20000 delay) to check new messages, the problem is if one call of sync.ashx is busy in downloading messages and a new call is made during this time, it starts to download identical copies of messages since it does not find the ids of emails in database which previous call is going to make.
I need some sort of mutually exclusive access, that if once call is busy in downloading messages then another call should not be made.
Something like
if(IsAlreadyDownloading == False)
{
Functions.SyncMail(params)
}
Where IsAlreadyDownloading is a global flag or mutex that should be set True once one call start downloading and be set false once it finish downloading or some exception has occurred indicating that another call can be made safely.
Since it is an ASP.net application we don't know when user will navigate away from the page which start download call and when it will navigate back to that page so another call to download handler should be made or not.
I don't know if I explained it properly or not but I hope someone will understand. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后,经过一番费尽心思,我终于解决了这个问题,我不知道这是否是最好的解决方案,但至少现在可以正常工作,如果您知道更好的解决方案,请在这里分享,谢谢。
这是我的解决方案。
我使用 global.asax 声明应用程序级全局变量,命名如下。
目的是模仿互斥体的行为,为此我留下了sync.ashx文件并制作了另一个aspx文件,从ashx文件转移的原因是访问应用程序级变量IsAlreadyDownloading 在通用处理程序中不可用。 aspx文件的代码如下
无论我们调用这个页面多少次,该方法都不会工作,因为如果另一个方法正在忙于工作,它会发现IsAlreadyDownloading = true,一旦完成它就会再次工作设置 IsAlreadyDownloading = false 以便新的调用可以获取它。
希望这可以帮助遇到类似问题的人。
服务器
Finally after much hair pulling and head scratching I managed to solve this problem, I don't know if it's the best solution but at least it is working properly now, if you know a better solution please share it here, thanks.
Here is my solution.
I used global.asax to declare application level global variable named as follows
The purpose is to mimic mutex like behavior and for that I left sync.ashx file and made another an aspx file, the reason for shifting from ashx file is access application level variable IsAlreadyDownloading which is unavailable in a generic handler. The code of aspx file is as follows
No matter how many times we make call to this page the method will not work as it will find IsAlreadyDownloading = true if another call of method is busy in work, as soon as it will finish it will again set IsAlreadyDownloading = false so a new call can acquire it.
Hope this help someone in similar problem.
server