Skip to content

Instantly share code, notes, and snippets.

@JFRode
Created May 4, 2017 14:18
Show Gist options
  • Save JFRode/b44e4978e7b22d556351f2b6b7874bea to your computer and use it in GitHub Desktop.
Save JFRode/b44e4978e7b22d556351f2b6b7874bea to your computer and use it in GitHub Desktop.
Alterando text do Form a partir de uma Thread
using System;
using System.Threading;
namespace FormsSandbox
{
public class ChecadorDeLabel
{
public EventHandler<bool> _event;
public ChecadorDeLabel()
{
}
public void ChecaSeContagemEhPar()
{
int i = 0;
while (true)
{
if (i % 2 == 0 )
{
_event.Invoke(this, true);
}
else
{
_event.Invoke(this, false);
}
Thread.Sleep(5000);
i++;
}
}
}
}
using System;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FormsSandbox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ChecadorDeLabel checador = new ChecadorDeLabel();
checador._event += new EventHandler<bool>(MetodoQualquer);
Task.Run(()=> checador.ChecaSeContagemEhPar());
}
private void MetodoQualquer(object sender, bool e)
{
BeginInvoke((Action)delegate { labelTextoDeRodape.Visible = e; });
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment