线程阻塞问题

时间:2013-04-17 00:39:52

标签: c# multithreading blocking

我遇到线程问题或者BeginInvoke阻塞了我的数据操作的整个过程,我启动线程或像这样调用

private void StartRetweetMarch(string twStatusId)
{
    foreach (var line in _emailBox.Lines)
    {

        //new Action<object>(Hashtags).BeginInvoke(new Email { twStatus = twStatusId, login = line.Split(':')[0], password = line.Split(':')[1] }, null, null);
        new Thread(new ParameterizedThreadStart(Hashtags)).Start(new Email { twStatus = twStatusId, login = line.Split(':')[0], password = line.Split(':')[1] });
        //_threads.Add(new Action<object>(Hashtags).BeginInvoke(new Email { login = line.Split(':')[0], password = line.Split(':')[1] }, null, null));
        //Thread.Sleep(40000);w
        // new Thread(new ParameterizedThreadStart(Hashtags)).Start(new Email { login = line.Split(':')[0], password = line.Split(':')[1] });
    }
}

但是只有第一次打电话给这个foreach电话,其他电话正在等待或类似的事情,我不知道该怎么做。

1 个答案:

答案 0 :(得分:0)

在Hashtags方法中,它看起来不是线程安全的。例如,_proxies变量,我不确定这种类型,但如果你只是使用List&lt;串GT;类型,然后你必须用独占锁写。

//Outside of Method...
//Create lock Object
private static object _lock = new Object();

//Inside of Hashtags Method...
//exclusive lock. Inside of code block is thread safe. 
lock (_lock) {
    prox = _proxies[new Random().Next(0, _proxies.Count)];
    _proxies.Remove(prox);
}