Hi,
You should synchronize two threads to let them interfere with each other.
Here is an example to show how to do this :
The XAML part :
<Grid>
<Button Content="Illegal Access" Height="23" HorizontalAlignment="Left" Margin="191,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<ListBox Height="117" HorizontalAlignment="Left" Margin="20,12,0,0" Name="listBox1" VerticalAlignment="Top" Width="120" />
<Button Content="Legal Access" Height="23" HorizontalAlignment="Left" Margin="192,58,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
</Grid>
And the code :
private void button1_Click(object sender, RoutedEventArgs e)
{
Thread thread = new Thread(new ThreadStart(IllegalAccessThread));
thread.Start();
}
void IllegalAccessThread()
{
listBox1.Items.Add("message");
}
private void button2_Click(object sender, RoutedEventArgs e)
{
Thread thread = new Thread(new ParameterizedThreadStart(LegalAccessThread));
thread.Start(SynchronizationContext.Current);
}
void LegalAccessThread(object param)
{
SynchronizationContext context = (param as SynchronizationContext);
for (int i = 0; i < 10; i++)
{
context.Send(delegate(object state)
{
listBox1.Items.Add(string.Format("message {0} from another thread", i));
}, "");
Thread.Sleep(1000);
}
}
You should pass current SynchronizationContext to other threads to let them run their code in your context by using
Send and
Post functions.
I hope this helps you.
Good Luck.