gpt4 book ai didi

c# - WP7(C#)的简单LOCK和ThreadPoolQuestion

转载 作者:行者123 更新时间:2023-12-03 13:21:58 26 4
gpt4 key购买 nike

有趣的是,我打算测试逻辑删除和并发性,在我的简单设置中,我认为到底有什么麻烦值得的。但是现在,在设置了一些愚蠢的测试类45分钟之后,我遇到了我不了解的第一个错误。

似乎我需要在列表,锁和线程上多做一些练习。
有谁知道为什么这会引发非法操作异常(请参阅随附的代码)。

对于更喜欢F5体验的人,这里是完整的解决方案(300kB)
http://www.filesavr.com/TXXXFVE40GTJK43

不要打开 View ,它们可能会使VS2010崩溃。很抱歉,您需要WP7工具,即使我非常确定此示例也可以在纯C#上运行(不运行)。

[编辑]
我更新了链接,现在可以正常工作了(使用旧代码)
我发现了第一个错误,这要归功于评论。

这有效:

    private void IncOneWithLock()
{
lock (CounterListOne)
{
IncListOne();
}
}

private void IncListOne()
{
if (CounterListOne == null)
{
Log("CounterListOne == null");
return;
}

var c = 0;
var oldList = CounterListOne.ToList();
foreach (var i in oldList)
{
CounterListOne[c++] = i + 1;
Thread.Sleep(Next(80*DelayFactor, 150*DelayFactor));
}
}

将继续测试逻辑删除的东西,并在以后的线程中发布可能的问题。迭代时更改列表-你好新手错误:-)

[/EDIT]

为方便起见,此函数中发生期望,并且它的操作期望无效:
 private void IncOneWithLock()
{
if (CounterListOne == null)
{
Log("CounterListOne == null");
return;
}

lock (this)
{
var c = 0;
foreach (var i in CounterListOne)
{
CounterListOne[c++] = i + 1;
Thread.Sleep(Next(80 * DelayFactor, 150 * DelayFactor));
}
}
}

这是测试类的完整源代码:
public class CounterClass : TestBase
{
private DispatcherTimer _dT;
public int CounterA { get; set; }

public ObservableCollection<int> CounterListOne { get; set; }
public List<int> CounterListTwo { get; set; }
public List<int> CounterListThree { get; set; }
private const int DelayFactor = 10;


public CounterClass()
{
CounterListOne = new ObservableCollection<int>();
CounterListTwo = new List<int>();
CounterListThree = new List<int>();

InitCounterLists();
StartBackgroundLogger();
}

public void LogLists()
{
lock (this)
//lock (CounterListTwo)
// lock (CounterListThree)
{
Log("====================================================");
Log("CounterListOne " + String.Join("-", CounterListOne.Select(x => x.ToString()).ToArray()));
Log("CounterListTwo " + String.Join("-", CounterListTwo.Select(x => x.ToString()).ToArray()));
Log("CounterListThree " + String.Join("-", CounterListThree.Select(x => x.ToString()).ToArray()));
Log("====================================================");
}
}

public void RunTests()
{
Log("MultiIncWithoutLocks");
//MultiIncWithoutLocks();

Log("MultiIncWithLocks");
MultiIncWithLocks();
}

public void MultiIncWithoutLocks()
{
ThreadPool.QueueUserWorkItem(x => IncOneWithoutLock());
ThreadPool.QueueUserWorkItem(x => IncOneWithoutLock());
ThreadPool.QueueUserWorkItem(x => IncOneWithoutLock());
ThreadPool.QueueUserWorkItem(x => IncOneWithoutLock());
ThreadPool.QueueUserWorkItem(x => IncOneWithoutLock());
}

public void MultiIncWithLocks()
{
ThreadPool.QueueUserWorkItem(x => IncOneWithLock());
ThreadPool.QueueUserWorkItem(x => IncOneWithLock());
ThreadPool.QueueUserWorkItem(x => IncOneWithLock());
ThreadPool.QueueUserWorkItem(x => IncOneWithLock());
ThreadPool.QueueUserWorkItem(x => IncOneWithLock());
}

private void IncOneWithoutLock()
{
var c = 0;
foreach (var i in CounterListOne)
{
CounterListOne[c++] = i+1;
Thread.Sleep(Next(80 * DelayFactor, 150 * DelayFactor));
}
}

private void IncOneWithLock()
{
if (CounterListOne == null)
{
Log("CounterListOne == null");
return;
}

lock (this)
{
var c = 0;
foreach (var i in CounterListOne)
{
CounterListOne[c++] = i + 1;
Thread.Sleep(Next(80 * DelayFactor, 150 * DelayFactor));
}
}
}

private void InitCounterLists()
{
InitCounterOne();
InitCounterTwo();
InitCounterThree();
}


private void InitCounterOne()
{
for (int i = 0; i < Next(1, 5); i++)
{
CounterListOne.Add(0);
}
}

private void InitCounterTwo()
{
for (int i = 0; i < Next(1, 5); i++)
{
CounterListTwo.Add(0);
}
}

private void InitCounterThree()
{
for (int i = 0; i < Next(1, 5); i++)
{
CounterListThree.Add(0);
}
}

private void StartBackgroundLogger()
{
_dT = new DispatcherTimer();
_dT.Tick += (a,b) => LogLists();
_dT.Interval = new TimeSpan(0,0,0,3);
_dT.Start();
}


}

最佳答案

您应该提供有关异常的更详细描述。它与foreach和CounterListOne连接:您可以在对其值进行迭代的同时对其进行更改,从而得到InvalidOperationException

关于c# - WP7(C#)的简单LOCK和ThreadPoolQuestion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5165706/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com