jeudi 16 avril 2015

C#: Threads conflict in some cases, in loop conditions


Vote count:

0




Im working in a thread project. in some cases I have these problems :


Here is some piece of my code :



List<EmailAddress> lstEmailAddress = new List<EmailAddress>();
private void TimerCheckInternetConnection_Tick(object sender, EventArgs e)
{
lock (TicketLock)
{
if (UtilityManager.CheckForInternetConnection())
{
if (ApplicationRunStatus == Enum_ApplicationRunStatus.UnknownDisconnect || ApplicationRunStatus == Enum_ApplicationRunStatus.IsReady)
{
// Connect
ThreadPool.QueueUserWorkItem((o) =>
{
for (int i = 0; i < lstEmailAddress.Count; i++)
{
lstEmailAddress[i].IsActive = lstEmailAddress[i].Login();
}

this.BeginInvoke(new Action(() =>
{
// some code

}));
});
}
}
}
}


and this is EmailAddress class :



class EmailAddress
{
private Imap4Client imap = new Imap4Client();
private object objectLock = new object();

public bool IsActive;
public string Address;
public string Password;

public string RecieveServerAddress;
public int RecieveServerPort;

public bool Login()
{
lock (objectLock)
{
try
{
imap.ConnectSsl(RecieveServerAddress, RecieveServerPort);
}
catch (Exception)
{
}

try
{
imap.Login(Address, Password);
return true;
}
catch (Exception)
{
return false;
}
}

}
}


and my problem is this:


when I want to use Login procedure that belongs to EmailAddress Class, It has some conflict. As you can see, I used Lock bud any thing changed.


for more details.. if I have 3 items in lstEmailAddress , the Login procedure has to called 3 times by this code. but every time, the login procedure will work on same username and password. so all my emails could not login correctly. if I remove threadpool, it will be ok.


thanks for any helping...



asked 14 secs ago

Elahe

463






C#: Threads conflict in some cases, in loop conditions

Aucun commentaire:

Enregistrer un commentaire