0

In our reusable assembly I have created a static class so that I can call it in various parts of our assemblies and get access to another project and call its methods,etc...

public static class CommentViewerHelper
{
    static CommentViewerHelper()
    {
       CommentViewer = new CommentViewer();
    }

    /// <summary>
    /// Gives access to PCSCommentViewer public methods and properties.
    /// </summary>
    public static CommentViewer CommentViewer { get; private set; }
} 

this CommentViewer project has a form so users can enter comments, etc... My problem is that How can I know if users have actually opened this CommentViewer program? I mean yeah I can call its methods whenever I want but is there a way that first I can know if that program is open at all?

Bohn
  • 26,091
  • 61
  • 167
  • 254
  • 1
    Do you mean how can you know from another application on the same machine? Another application on a different machine? Please clarify. – David Apr 02 '12 at 19:53
  • Does it matter if the user has opened and closed the dialog? Or is it just that they have it open now? – Jay Apr 02 '12 at 19:54
  • these are separate DLLs on a Citrix box..same machine tho. – Bohn Apr 02 '12 at 19:54
  • 2
    Are you really launching a new program (process) or just opening a window? It's not really clear what you are trying to achieve here. – Matt Burland Apr 02 '12 at 19:55
  • 1
    You can use System.Diagnostics.Process to determine if a process is running... http://stackoverflow.com/questions/51148/how-do-i-find-out-if-a-process-is-already-running-using-c – David Apr 02 '12 at 19:56
  • @BDotA: So these are dialogs in a dll, not a separate program? Please edit your title if that is the case. – Matt Burland Apr 02 '12 at 19:56
  • @Jay: well in that dialog they are viewing comments, the call I want to make is to go and Refresh the comments, so it will have some performance to run the sql script,etc..so I was thinking if the viewer window is closed and they don't see it, then no need to refresh it at that moment – Bohn Apr 02 '12 at 19:56
  • @MattBurland: launching a new application, it is another DLL from another internal team that we should launch it to show it to user. – Bohn Apr 02 '12 at 19:57
  • 1
    @BDotA: Launching a new application would mean starting an .exe with the Process class. I'm pretty sure that's not what you are talking about however. – Matt Burland Apr 02 '12 at 20:00
  • Have you referenced the dll to your project? In that case, isn't it trivial to get the opened forms? – nawfal Apr 02 '12 at 20:22

3 Answers3

3

To check if a form is open, you can use the IsHandleCreated property.

On the other hand, if you wrote the code that starts the other form in the first place, then shouldn't you know if that happened or not?

Matt Burland
  • 44,552
  • 18
  • 99
  • 171
2

I believe what you're trying to accomplish is to not (re)load the content of the dialog many times even if it's called many times. You might do that using "lazy loading".

Create a string builder reference and set it to null. In your initialization if the reference is null then create the string builder object and load the content into it. If it's not null then you know the content has already been retrieved and you can simply use the string builder.

Jay
  • 13,803
  • 4
  • 42
  • 69
0

If you created the forms yourself, you should have no issues querying an AppDomain or the like.

If you have no direct means of accessing the .NET Application information you could try something like this:

using System.Diagnostics;
private bool ApplicationExists(string appName)
{
    foreach (Process currentProcess in Process.GetProcesses("."))
    {
        if (currentProcess.MainWindowTitle.Length == appName)
        {
            return true;
        }
    }
    return false;
}
covertCoder
  • 446
  • 3
  • 10