I have an automated mail client application, I need to show a 'Connecting...' picture during the period when the mail is retrieved.
// *************************************
private void Form1_Load(object sender, EventArgs z)
{
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 20000;
aTimer.Enabled = true;
CheckMail();
}
// *************************************
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
CheckMail();
}
// *************************************
void CheckMail() {
SetPictureSwitch();
//picEmail.visible=true
{
// procedure to check mail and update Grid list....
}
SetPictureSwitch();
//(picEmail.visible=false)
}
// *************************************
// picEmail is a simple image with wait... icon
private void SetPictureSwitch()
{
if (picEmail.InvokeRequired)
{
picEmail.Invoke(new MethodInvoker(
delegate ()
{
picEmail.Visible = !picEmail.Visible;
}));
}
else
{
picEmail.Visible = !picEmail.Visible;
}
Application.DoEvents();
}
// *************************************
private void btnRefresh_Click(object sender, EventArgs e)
{
CheckMail();
}
I tried any kind of thread management but without success, the pic didn't get displayed or receive a 'cross thread error'. If I use the btnRefresh only then it works fine.
What's the right way to make the pic visible when CheckMail() starts and to hide it when CheckMail() is completed?
Why it is so hard to set visible=true ? (I'm sorry I come from VB6 :))