小编use*_*375的帖子

ReaderWriterLockSlim 是否能够抵抗 ThreadAbortException?

我想检查以下代码是否能够抵抗 ThreadAbortException 并且不会导致孤儿锁。如果不是,那么避免孤儿锁的最佳模式是什么?

ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();

public void DoStaff()
{
  _lock.EnterWriteLock();
  //Is this place where ThreadAbotException can corrupt my code, or is there JIT optimalization which prevent this from happening??? 
  try
  {
    ...
  }
  finally
  {
    _lock.ExitWriteLock();
  }
}
Run Code Online (Sandbox Code Playgroud)

根据以下链接http://chabster.blogspot.cz/2013/07/a-story-of-orphaned-readerwriterlockslim.html,有(或至少有)可能的方法如何创建孤儿锁,但我正在运行示例编码了一段时间,没有任何运气。

我正在使用.NET 4.0

调试和发布中的行为有什么区别吗?

.net c# readerwriterlockslim threadabortexception

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

Autofac-解决多线程环境中的依赖关系

public class MultithreadTester
{

    public void Run()
    {
        var builder = new ContainerBuilder();
        builder.RegisterType<ManualWork>().As<IWork>();
        builder.RegisterType<ColabManualWork>().As<IColabWork>();
        builder.RegisterType<RelaxAfterManualWork>().As<IRelax>();

        var container = builder.Build();

        //#1 - Simple single thread
        using (var scope = container.BeginLifetimeScope())
        {
            var work = scope.Resolve<IWork>();
            work.DoWork();
        }

        //#2 - Resolving dependecies in worker threads in scopes of these threads without passing lifetime scopes are container into implementation
        using (var scope = container.BeginLifetimeScope())
        {
            var work = scope.Resolve<IColabWork>();
            work.DoWork();
        }

        //#3 - Resolving dependecies in worker threads when original scope is …
Run Code Online (Sandbox Code Playgroud)

c# multithreading dependency-injection autofac

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

Automapper Mapper.CreateMap是线程安全的吗?

在我们当前的项目中,我们在类的静态构造函数中注册映射,这些构造函数由多个线程调用.静态构造函数中的映射仅与该类相关.但是仍然可以同时运行多个CreateMap调用.此外,偶尔(主要是复制/过去的问题)相同的映射可以在不同类的静态构造函数中注册.

我试图谷歌是否Mapper.CreateMap是线程安全的.我发现只有以下内容:

2012年AutoMapper线程安全的Map Map.Map中,有一个注释,nemesv的回答是CreateMap不是线程安全的,它永远不会.

但是我发现GitHub上的一个问题静态DynamicMap和CreateMap API应该是 2014年的线程安全标记,在3.2版本中标记为已关闭.这表明CreateMap现在应该是线程安全的.

你能确认CreateMap是线程安全的吗?我进行了一些测试,它看起来应该是,但如果有更深入了解的人可以确认这些信息就可以了.

编辑 经过一些额外的测试后,似乎CreateMap行为非常有趣:

我使用以下代码进行测试

    public void Test()
    {
        var items = new List<EntityA>();
        for (int i = 0; i < 100000; i++)
        {
            items.Add(new EntityA { FirstName = "A" + i });
        }

        ManualResetEvent stopChangingMappingFunction = new ManualResetEvent(false);

        Thread t1 = new Thread(() =>
        {

            int i = 1;
            while (true)
            {
                if (stopChangingMappingFunction.WaitOne(TimeSpan.Zero))
                    return;

                var i1 = i++;
                Mapper.CreateMap<EntityA, EntityB>().ForMember(x => x.Age, y => y.ResolveUsing(new Func<EntityA, object>(a …
Run Code Online (Sandbox Code Playgroud)

c# multithreading automapper

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

如何在Windows中获取所有打开的命名管道列表并避免可能的异常?

获取命名管道列表在理想情况下非常简单,可以在这里找到: 如何在Windows中获取所有打开命名管道的列表?

但提到了解决方案

var namedPipes = Directory.GetFiles(@"\\.\pipe\");
Run Code Online (Sandbox Code Playgroud)

偶尔会有不可预测的结果.上面的链接中提到了其中一个(路径异常中的无效字符).今天我遇到了我自己的异常--ArgumentException"第二个路径片段不能是驱动器或UNC名称.参数名称:path2".

问题是.net中是否有任何真正可行的解决方案来获取所有打开的命名管道的列表?谢谢

.net c# named-pipes

4
推荐指数
1
解决办法
4991
查看次数

从服务定位器到依赖注入

我准备了一些示例应用程序,基于这些应用程序,我想讨论转移到依赖注入而不是服务定位器的问题。我在DI领域还很新,所以请耐心等待。示例应用使用Simple Injector作为DI库编写。Bootstrapper中的注册按预期工作。每当我需要IMessageBox接口和新实例ComputationCores时,我都会使用它。

我阅读了一些有关DI的文章,因此我知道应该有一些Composition根目录以及它应该如何工作。但是我发现只是非常基本的示例,没有实词的复杂性。

样例代码:

public class DependencyResolver
{

    public static Func<Type, object> ResolveMe;

    public static T GetInstance<T>() where T : class
    {
        return (T)ResolveMe(typeof (T));
    }
}

public interface IMessageBox
{
    void ShowMessage(string message);
}

public class StandardMessageBox : IMessageBox
{
    public StandardMessageBox()
    {
        Console.WriteLine("StandardMessageBox constructor called...");
    }

    ~StandardMessageBox()
    {
        Console.WriteLine("StandardMessageBox destructor called...");
    }

    public void ShowMessage(string message)
    {
        Console.WriteLine(message);
    }
}

public interface IComputationCoreAlpha
{
    int RunComputation(int myParam);
}

public class SyncComputationCoreAlpha : IComputationCoreAlpha
{
    public SyncComputationCoreAlpha()
    {
        Console.WriteLine("SyncComputationCoreAlpha …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection service-locator simple-injector

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

当在连续运行在不同线程中并由用户代码关闭的表单上使用时,TopMost不起作用

我有以下示例代码

   [STAThread]
    static void Main(string[] args)
    {
        Thread thread = new Thread(() =>
        {
            using (var mww = new Form1())
            {
                Application.Run(mww);
            }
        });
        thread.Start();
        thread.Join();

        thread = new Thread(() =>
        {
            using (var mww = new Form1())
            {
                Application.Run(mww);
            }
        });
        thread.Start();
        thread.Join();

        thread = new Thread(() =>
        {
            using (var mww = new Form1())
            {
                Application.Run(mww);
            }
        });
        thread.Start();
        thread.Join();
    }
Run Code Online (Sandbox Code Playgroud)

其中Form1定义为:

public partial class Form1 : Form
{
    private readonly Timer _myTimer = new Timer();

    public Form1() …
Run Code Online (Sandbox Code Playgroud)

.net c# winforms

0
推荐指数
1
解决办法
1399
查看次数