Skip to content

Instantly share code, notes, and snippets.

Created December 19, 2012 06:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4334796 to your computer and use it in GitHub Desktop.
Save anonymous/4334796 to your computer and use it in GitHub Desktop.
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem((_) =>
{
int originalThread;
Task<int> task = Task.Factory.StartNew<int>(() => Thread.CurrentThread.ManagedThreadId);
for(;;)
task = task.ContinueWith( prevT => VerifySameThread( prevT.Result, Thread.CurrentThread.ManagedThreadId ) );
}, null);
}
private int VerifySameThread(int a, int b)
{
if (a != b)
Invoke(new Action(() => MessageBox.Show(string.Format("Previous callback's thread was {0}, current thread is {1}", a, b))));
return b;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment