I have an app in VFP wich is in migrating process to .NET c#, as a aprt of this we have to desktops for the app
desktop one in vfp: It has all menus, those menus save the selected option in a SQLServer table if the option is implmented on .net.
desktop two in wpf: It has a timer that read the options clicked by the users on desktop one (vfp)
If an option is found by desktop two, and this option match with the user expected, it try to open the option and set it as topmost.
This work well but some times throw an win32 Exception Access denied
[ClassInterface(ClassInterfaceType.AutoDual)]
[ProgId("MyClass")]
public class DesktopsAux
{
public string[] Desktops;
public string desktopname = "My App! .Net Desktop";
public IntPtr hWnd;
public DesktopsAux()
{
try
{
var q = from d in Process.GetProcesses()
where d.MainWindowTitle.Contains(desktopname)
select d.MainWindowTitle;
Desktops = q.ToArray();
}
catch (NullReferenceException)
{
}
}
public bool DeskTopForUser(string userName)
{
// The error happen here, performing this query
var q = from d in Process.GetProcesses()
where d.MainWindowTitle.Contains(desktopname)
select new {d.MainWindowTitle , d.Handle};
var list = q.ToList();
var q2 = from a in list
where a.MainWindowTitle.Contains(userName)
select a;
if (q2.Count() > 0)
{
hWnd = q2.First().Handle;
return true;
}
return false;
}
}
public class Desktop
{
public string MainWindowTitle { get; set; }
public IntPtr Handle { get; set; }
}
The error happen in DeskTopForUser Method just when perform the query.
Why this error happen? How can I do to prevent this error?