小编Vla*_*rov的帖子

多线程,lambdas和局部变量

我的问题是,在下面的代码中,我可以确定实例方法将访问我认为它们将要的变量,还是可以在我还在工作时由另一个线程更改?闭包与此有什么关系,即我是否会在IEnumerable<T>这样的枚举的本地副本上工作是否安全?

为了解释我的问题,如果我从不写共享变量,我是否需要任何锁定?

public class CustomerClass
{
    private Config cfg = (Config)ConfigurationManager.GetSection("Customer");

    public void Run()
    {
        var serviceGroups = this.cfg.ServiceDeskGroups.Select(n => n.Group).ToList();

        var groupedData = DataReader.GetSourceData().AsEnumerable().GroupBy(n => n.Field<int>("ID"));
        Parallel.ForEach<IGrouping<int, DataRow>, CustomerDataContext>(
            groupedData,
            () => new CustomerDataContext(),
            (g, _, ctx) =>
            {
                var inter = this.FindOrCreateInteraction(ctx, g.Key);

                inter.ID = g.Key;
                inter.Title = g.First().Field<string>("Title");

                this.CalculateSomeProperty(ref inter, serviceGroups);

                return ctx;
            },
            ctx => ctx.SubmitAllChanges());
    }

    private Interaction FindOrCreateInteraction(CustomerDataContext ctx, int ID)
    {
        var inter = ctx.Interactions.Where(n => n.Id = ID).SingleOrDefault();

        if (inter …
Run Code Online (Sandbox Code Playgroud)

c# lambda multithreading .net-4.0 task-parallel-library

7
推荐指数
1
解决办法
638
查看次数

什么线程在silverlight WCF调用上调用已完成的事件处理程序?

假设我有Silverlight应用程序调用WCF服务:

void DoStuff()
{
    MyProxy proxy = new MyProxy();
    proxy.DoStuffCompleted += DoStuffCompleted;
    proxy.DoStuffAsync();
}

void DoStuffCompleted(object sender, DoStuffCompletedEventArgs e)
{
    // Handle the result.
}
Run Code Online (Sandbox Code Playgroud)

DoStuff由UI线程调用.什么线程最终会调用该DoStuffCompleted方法?如果我同时调用两个异步调用,是否有可能在不同的线程上同时触发两个已完成的事件?

.net c# silverlight wcf multithreading

5
推荐指数
1
解决办法
2362
查看次数