Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否有一个;“工人”;在mstest中,并行测试是否具有唯一id?_C#_Multithreading_Mstest_Worker - Fatal编程技术网

C# 是否有一个;“工人”;在mstest中,并行测试是否具有唯一id?

C# 是否有一个;“工人”;在mstest中,并行测试是否具有唯一id?,c#,multithreading,mstest,worker,C#,Multithreading,Mstest,Worker,我正在使用MSTest,我想知道Parallelize workers是否有一个唯一的ID,如果是,如何获得这些ID?我假设1 worker=1 thread,但是thread.CurrentThread.ManagedThreadID不是该worker独有的。我需要一种方法来获取有关工人的唯一ID。有人知道这是否可能吗 这就是我的工作 [assembly: Parallelize(Workers =1, Scope = ExecutionScope.ClassLevel)] 我想说两个工人,

我正在使用MSTest,我想知道Parallelize workers是否有一个唯一的ID,如果是,如何获得这些ID?我假设
1 worker=1 thread
,但是
thread.CurrentThread.ManagedThreadID
不是该worker独有的。我需要一种方法来获取有关工人的唯一ID。有人知道这是否可能吗

这就是我的工作

[assembly: Parallelize(Workers =1, Scope = ExecutionScope.ClassLevel)]
我想说两个工人,有两个不同的班级。我想同时运行的每个类

启动worker时,我会在manager中创建一个新对象,请参见下面的
ThreadManager
。但是,正如您所看到的,我用于获取当前线程的方法不会应用于超过1个工作线程

public static class ThreadManager
{
    public static IConfiguration Configuration { get; set; }
    private static Dictionary<int, ThreadObject> threadDictionary;

    static ThreadManager()
        => threadDictionary = new Dictionary<int, ThreadObject>();

    public static void AddThread()
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("AppSettings.json");
        Configuration = builder.Build();

        threadDictionary.Add(Thread.CurrentThread.ManagedThreadId, new ThreadObject());
        }

    public static void RemoveThread()
            => threadDictionary.Remove(Thread.CurrentThread.ManagedThreadId);

    public static ThreadObject GetThread()
    {
        ThreadObject _thread = null;
            threadDictionary.TryGetValue(Thread.CurrentThread.ManagedThreadId, out _thread);
        return _thread;
    }
}
公共静态类线程管理器
{
公共静态IConfiguration配置{get;set;}
私有静态字典;
静态线程管理器()
=>threadDictionary=新字典();
公共静态void AddThread()
{
var builder=new ConfigurationBuilder()
.SetBasePath(目录.GetCurrentDirectory())
.AddJsonFile(“AppSettings.json”);
Configuration=builder.Build();
添加(Thread.CurrentThread.ManagedThreadId,new ThreadObject());
}
公共静态void RemoveThread()
=>threadDictionary.Remove(Thread.CurrentThread.ManagedThreadId);
公共静态ThreadObject GetThread()
{
ThreadObject _thread=null;
threadDictionary.TryGetValue(Thread.CurrentThread.ManagedThreadId,out\u Thread);
返回线程;
}
}

您是否考虑过您在这里是否解决了错误的问题?为什么,具体来说,这是一个只需要在测试期间解决的问题,而不会在实际使用中出现的问题?我需要从静态类访问驱动程序。这个静态类需要能够识别它使用的工作人员。我这样做是因为一些复杂的原因。当我们有一个selenium网格并使用NUnit3时,这种方法是有效的,但一旦我们切换到MSTest V2并放弃网格,我们就开始讨论这个问题。我可以花一些时间尝试重新构建,但如果我可以获得分配给工作者的唯一ID,应用程序可以获得该ID,那么就更容易知道了。就是这个吗?如果是这样的话,看来你运气不好。