I have developed a little tool for my company. It's an WPF-Application. The aim is to open a pdf file from a specific folder, do some work and as soon as you saved the file, the application should enable some buttons to continue the work.
So my idea was to create a new thread, checking all view seconds if the LastWriteTime for the FileInfo was changed. As soon as that happens, the thread should enable all buttons which are not enabled yet.
privat FileInfo file; //File selected
private void Open_PDF(FileInfo file)
{
if (file == null) return;
Process.Start(file.FullName); //Open with standard pdf editor
new Thread(New ThreadStart(Wait_FileSaved)).Start();
}
private void Wait_FileSaved()
{
while (file.LastWriteTimeUtc.Equals(new FileInfo(file.FullName).LastWriteTimeUtc)
{
Thread.Sleep(2000);
}
Console.WriteLine("File saved"); //Just to check if it works
EnableButtons(btnChecked, btnDenied, btnClarify, btnPass);
}
private void EnableButtons(params Button[] buttons)
{
foreach (Button button in buttons)
button.IsEnabled = true; //Code fails here, buttons are created in another thread
//so I don't have access to the Buttons in the parallel thread
}
I have already found some results at google for this problem. According to the solutions there, I have changed my Code for EnableButtons:
private bool EnableButtons(params Button[] buttons)
{
if (!this.Dispatcher.CheckAccess())
return (bool)this.Dispatcher.Invoke((Func<Button[],bool>)EnableButtons, buttons);
foreach (Button button in buttons)
button.IsEnabled = true;
return true;
}
I am pretty sure I use Dispatcher class wrong. Do I have to create one before threading? Is the code possible without threading? (and headache ^^)
You don't have to post Documentation for Dispatcher class, I've already read this, and still have no clue what this class is for and even if it is the right class to use in this case :)
Thanks guys, Micha