任务正在修改的访问对象/变量 [英] Access object/variable being modified by the Task
本文介绍了任务正在修改的访问对象/变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试访问由Task.Factory.StartNew任务调用的函数正在修改的变量.该代码在WCF服务中,当我将URL切换为https时,它停止工作,并且我没有从Task接收更新的值.
I am trying to access the variable which is being modified by a function called by Task.Factory.StartNew task. The code is in WCF Service and when I switch the URL to https, it stops working and I don't receive updated value from the Task.
namespace EDSWcfService
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class Service1 : IService1
{
TaskHandler objTaskHandler = new TaskHandler();
public string GetData(int value)
{
Task.Factory.StartNew(()=>objTaskHandler.TaskFunction());
string strTemp = string.Format("You entered: {0}", value) + " and Task returned string = " + objTaskHandler.GetMessage();
return strTemp;
}
public string GetMessage()
{
return objTaskHandler.GetMessage();
}
}
public class TaskHandler
{
private string strMessage = string.Empty;
int i = 0;
public void TaskFunction()
{
strMessage = "Entered Task";
}
public string GetMessage()
{
i++;
return strMessage + i.ToString();
}
}
}
它一直困扰着很多人.我有一个需要从WCF服务每秒获取更新状态的客户端,该服务又依次调用了一个任务来进行计算并在每次客户端调用时提供更新的值.
It has been troubling a lot. I have a client that need to get the updated status per second from the WCF Service which in turns invoked a task to do the calculation and supply the updated value every time client makes a call.
谢谢
推荐答案
Maybe objTaskHandler should be made static?
这篇关于任务正在修改的访问对象/变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文