小编Had*_*adi的帖子

从工作线程但不是主线程访问时,C# - 类字段为null

不确定我做错了什么:

class MyClass
{

    private EventInfo eventInfo;

    public void OnGenerateEvent(object sender, EventArgs e)
    { 
        // Called from *main* thread 

        // Load assembly and set eventInfo here
        eventInfo = ....GetEvent(...);
        eventInfo.AddEventHandler(source, handler);

        // Call to a static method in another assembly
        someMethodInfo.Invoke(null, null);

    }


    public void OnEventChanged(object sender, EventArgs args)
    {    
        // Called from a *worker* thread created 
        // by that static method in the other assembly

        eventInfo is null here !   

        // Trying to remove handler
        eventInfo.RemoveEventHandler(.....);

    }


    // But... …
Run Code Online (Sandbox Code Playgroud)

c# multithreading delegates invoke worker

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

单词项目的字典

Dictionary<object, object> myDictionary碰巧包含单个项目的情况下,检索值对象的最佳方法是什么(如果我不关心密钥)?

if (myDictionary.Count == 1)
{
    // Doesn't work
    object obj = myDictionary.Values[0];
}
Run Code Online (Sandbox Code Playgroud)

谢谢

c# dictionary

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

随机数与CompreTo()中的GetHashCode()?

我在我的结构中使用Random类CompareTo()以相同的概率选择其中一个结构,当它们具有相同的字段值时.使用固定种子实例化Random类以获得可重现的伪随机值序列,以确保无论我使用相同输入运行多少次,我的程序都会给出相同的精确比较结果.

我正在考虑用内存引用或GetHashCode()替换随机数.这样做可以保证:

(1)选择是以相同的概率进行的,并且

(2)如果再次运行程序,我会得到相同的结果吗?

struct MyStruct : IComparable<MyStruct>
{
        private readonly float _param1;
        private readonly float _param2;
        private readonly int _randValue;

        public MyStruct(float param1, float param2)
        {
                _param1 = param1;
                _param2 = param2;
                _randValue = _random.Next();
        }

        public int CompareTo(MyStruct other)
        {
            if (_param1 < other._param1)
            {
                return -1;
            }
            else if (_param1 > other._param1)
            {
                return 1;
            }
            else if (_param2 > other._param2)
            {
                return -1;
            }
            else if (_param2 < other._param2)
            {
                return 1;
            }
            // …
Run Code Online (Sandbox Code Playgroud)

c# random compareto gethashcode

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

如何在 Hive 中的 GROUPING SETS 后重塑数据?

我想在许多不同的维度上聚合一列。我认为 GOUPING SETS 适合我的问题,但我无法弄清楚如何从 GROUPING SETS 转换/重塑结果表。

这是我使用 GROUPING SETS 的查询:

select date, dim1, dim2, dim3, sum(value) as sum_value
from table
grouping by date, dim1, dim2, dim3
grouping sets ((date, dim1), (date, dim2), (date, dim3))
Run Code Online (Sandbox Code Playgroud)

查询将生成如下表:

date        dim1    dim2    dim3    sum_value
2017-01-01  A       NULL    NULL    [value_A]
2017-01-01  B       NULL    NULL    [value_B]
2017-01-01  NULL    C       NULL    [value_C]
2017-01-01  NULL    D       NULL    [value_D]
2017-01-01  NULL    NULL    E       [value_E]
2017-01-01  NULL    NULL    F       [value_F]
Run Code Online (Sandbox Code Playgroud)

但我真正需要的是这样一张桌子:

date        dim     factor  sum_value
2017-01-01  dim1     A      [value_A] …
Run Code Online (Sandbox Code Playgroud)

hive rollup cube hiveql grouping-sets

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

比较两个列表与LINQ的更好方法?

我有2个收藏:

IEnumerable<Element> allElements
List<ElementId> someElements, 
Run Code Online (Sandbox Code Playgroud)

一起完成以下操作的简洁方法是什么:

[1]验证someElements存在的所有元素,allElements当条件失败时快速返回.

[2]获取映射到的Element对象列表List<ElementId> someElements.

每个Element对象都有一个ElementId

谢谢.

c# linq custom-lists

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

C# - 将数据从ThreadPool线程传递回主线程

当前实现:等待直到parallelCount收集值,用于ThreadPool处理值,等待所有线程完成,重新收集另一组值等等...

码:

private static int parallelCount = 5;
private int taskIndex;
private object[] paramObjects;

// Each ThreadPool thread should access only one item of the array, 
// release object when done, to be used by another thread
private object[] reusableObjects = new object[parallelCount];     

private void MultiThreadedGenerate(object paramObject)
{
    paramObjects[taskIndex] = paramObject;
    taskIndex++;

    if (taskIndex == parallelCount)
    { 
        MultiThreadedGenerate();

        // Reset
        taskIndex = 0;
    }
}

/*
 * Called when 'paramObjects' array gets filled
 */
private void …
Run Code Online (Sandbox Code Playgroud)

c# queue multithreading .net-3.5 threadpool

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

正则表达式匹配文件路径字符串的数字末尾

我有以下文件名,并希望在以下后提取数字"_R":

  • \ FileName_R 10 .txt => 10
  • \ FileName_R_ 10 .txt => 10

我已经成功使用了正则表达式:

_R_?(\d+)\.txt$
Run Code Online (Sandbox Code Playgroud)

现在,我正在寻求使其适应以下变化:

  • \ FileName_R 10 _1.txt => 10
  • \ FileName_R_ 10 _1.txt => 10
  • \ FileName_R 10 _11.txt => 10

我试过了

_R_?(\d+)_?\d+?\.txt$
Run Code Online (Sandbox Code Playgroud)

这似乎适用于后面的例子,但与第一个例子有关.

谢谢.

c# regex

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

C#中的基本锁定问题

课程:

public class SomeCollection
{
    public void IteratorReset()
    {
      index = -1;
    }

    public bool IteratorNext()
    {
      index++;
      return index < Count;
    }

    public int Count
    {
      get
      {
          return floatCollection.Count;
      }
    }

    public float CurrentValue
    {
      get
      {
        return floatCollection[index];
      }
    }

    public int CurrentIndex
    {
      get
      {
        return intCollection[index];
      }
    }
}
Run Code Online (Sandbox Code Playgroud)

包含对"SomeCollection"的引用的类:

public class ThreadUnsafeClass
{    
    public SomeCollection CollectionObj
    {
       get
       {
           return collectionObj;
        }               
    }      
}
Run Code Online (Sandbox Code Playgroud)

ClassA,ClassBClassC包含迭代CollectionObj的以下循环:

for (threadUnsafeClass.CollectionObj.IteratorReset(); threadUnsafeClass.CollectionObj.IteratorNext(); …
Run Code Online (Sandbox Code Playgroud)

.net c# multithreading locking thread-safety

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