不确定我做错了什么:
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) 在Dictionary<object, object> myDictionary碰巧包含单个项目的情况下,检索值对象的最佳方法是什么(如果我不关心密钥)?
if (myDictionary.Count == 1)
{
// Doesn't work
object obj = myDictionary.Values[0];
}
Run Code Online (Sandbox Code Playgroud)
谢谢
我在我的结构中使用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) 我想在许多不同的维度上聚合一列。我认为 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) 我有2个收藏:
IEnumerable<Element> allElements
List<ElementId> someElements,
Run Code Online (Sandbox Code Playgroud)
一起完成以下操作的简洁方法是什么:
[1]验证someElements存在的所有元素,allElements当条件失败时快速返回.
和
[2]获取映射到的Element对象列表List<ElementId> someElements.
每个Element对象都有一个ElementId
谢谢.
当前实现:等待直到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) 我有以下文件名,并希望在以下后提取数字"_R":
我已经成功使用了正则表达式:
_R_?(\d+)\.txt$
Run Code Online (Sandbox Code Playgroud)
现在,我正在寻求使其适应以下变化:
我试过了
_R_?(\d+)_?\d+?\.txt$
Run Code Online (Sandbox Code Playgroud)
这似乎适用于后面的例子,但与第一个例子有关.
谢谢.
课程:
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,ClassB并ClassC包含迭代CollectionObj的以下循环:
for (threadUnsafeClass.CollectionObj.IteratorReset(); threadUnsafeClass.CollectionObj.IteratorNext(); …Run Code Online (Sandbox Code Playgroud) c# ×7
.net ×1
.net-3.5 ×1
compareto ×1
cube ×1
custom-lists ×1
delegates ×1
dictionary ×1
gethashcode ×1
hive ×1
hiveql ×1
invoke ×1
linq ×1
locking ×1
queue ×1
random ×1
regex ×1
rollup ×1
threadpool ×1
worker ×1