I have a problem with an event that comes from other thread, I can't invoke my function in first Thread.
This is the code:
namespace Gestion_Photo_CM
{
/// <summary>
/// Logique d'interaction pour MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
GestionRecherche gRech = new GestionRecherche();
Dispatcher disp = Dispatcher.CurrentDispatcher;
public MainWindow()
{
InitializeComponent();
gRech.evt_creer_objimage += afficherimage;
}
/// <summary>
/// Affichage dynamique des images
/// </summary>
/// <param name="path"></param>
public void afficherimage(Image obj)
{
if (disp.CheckAccess())
{
this.Dispatcher.Invoke(delegate () { afficherimage(obj); });
}
else
{
this.RootGrid.Children.Add(obj);
}
}
/// <summary>
/// Validation du chemin entré
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_valid_Click(object sender, RoutedEventArgs e)
{
string cheminDossier = tbfolderpath.Text;
Thread thScanDossier = new Thread(delegate () { gRech.ScanDossiers(cheminDossier); });
thScanDossier.SetApartmentState(ApartmentState.STA);
thScanDossier.Start();
}
}
}
When the programme comes to this line:
this.RootGrid.Children.Add(obj);
An Exception says that it can't use the object because it is on another Thread.