问题描述:

I have a problem with a threads or BeginInvoke's blocking the whole process of my data operations, I start threads or invokes like this

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] });

}

}

But only first call of this foreach call, the other ones are pending or something like this, I don't know what to do.

网友答案:

Inside of Hashtags method, it looks like not thread safe. For instance, _proxies variable, I'm not sure this type, but if you are just using List< string> type, then you have to write with exclusive lock.

//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);
}
相关阅读:
Top