2013-06-27 10 views
6

जब मेरा टाइमर बंद हो जाता है तो मैं वर्तमान में सक्रिय एप्लिकेशन का नाम प्राप्त करना चाहता हूं। 20 सेकंड की रिकॉर्डिंग के बाद मुझे वर्तमान सक्रिय एप्लिकेशन नाम दिखाना चाहिए। मैंने कुछ कोड की कोशिश की। आप यहां देख सकते हैं। लेकिन यह टाइमर स्टॉप के बाद मुझे कुछ भी नहीं दिखा रहा है।वर्तमान सक्रिय एप्लिकेशन का नाम प्राप्त करें

सी # कोड:

public class Win32wrapper 
{ 
    private System.Timers.Timer pingTimer; 
    private Timer recordTimer; 

    private List<HarvestApp.ProcessInformation> ProcessList = new List<HarvestApp.ProcessInformation>(); 

    [DllImport("user32.dll")] 
    public static extern IntPtr GetForegroundWindow(); 

    [DllImport("user32.dll")] 
    public static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out uint processId); 

    private DateTime recordStartTime; 

    public void startTimer(int pingTimerValue=5000, int recordTimerValue=20000) 
    { 

     pingTimer = new System.Timers.Timer(pingTimerValue); 
     pingTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); 
     pingTimer.Interval = pingTimerValue; 
     pingTimer.Enabled = true; 

     recordTimer = new Timer(recordTimerValue); 
     recordTimer.Elapsed += new ElapsedEventHandler(OnRecordEvent); 
     recordTimer.Interval = recordTimerValue; 
     recordTimer.Enabled = true; 

     recordStartTime = DateTime.Now; 
    } 

    private void OnTimedEvent(object source, ElapsedEventArgs e) 
    { 
     Console.WriteLine("The Ping Elapsed event was raised at {0}", e.SignalTime); 


     //Record through win32dll the application foreground caption 

     GetActiveFileNameTitle(); 

     //Store into collection object, Push into ArrayList, Push into process id 
    } 


    public String GetActiveFileNameTitle() 
    { 
     IntPtr hWnd = GetForegroundWindow(); 
     uint processId; 
     GetWindowThreadProcessId(hWnd, out processId); 
     Process p = Process.GetProcessById((int)processId); 
     //p.MainModule.FileName.Dump(); 
     return p.ProcessName; 
    } 

     private void OnRecordEvent(object source, ElapsedEventArgs e) 
    { 
     Console.WriteLine("The Record Elapsed event was raised at {0}", e.SignalTime); 

     ProcessInformation procTemp = GetMaxRunTimeForApplicationsBetween(recordStartTime, DateTime.Now); 
     Harvest_TimeSheetEntry tempEntry = new Harvest_TimeSheetEntry(procTemp, recordStartTime, DateTime.Now); 

     //Add to the list of the specific day Only not the entire 
     // Globals._globalController.harvestManager._TIMESHEETENTRYDICTIONARY[recordStartTime.Date].Add(tempEntry); 


     Globals._globalController.getDayViewWindow.Dispatcher.BeginInvoke(new Action(delegate() 
     { 
      Globals._globalController.getDayViewWindow.addHarvestEntrytoView(tempEntry); 
     })); 
     //Clean Out the ProcessList? 

     recordStartTime = DateTime.Now; 
    } 


    public void stopTimer() 
    { 
     pingTimer.Stop(); 
     recordTimer.Stop(); 
    } 
+0

आप वर्तमान विंडो के शीर्षक पता करना चाहते हैं? http://stackoverflow.com/questions/115868/how-do-i-get-the-title-of-the-current-active-window-using-c – Lucas

+0

मुझे आवेदन का नाम चाहिए। अगर पृष्ठभूमि में नोटपैड चल रहा है, तो मुझे वहां नाम "नोटपैड" के रूप में दिखाना चाहिए। – Dinesh

+1

सवाल यह है: क्या आप विंडो शीर्षक या अग्रभूमि ऐप का प्रक्रिया नाम चाहते हैं? – Andre

उत्तर

12

संपादित करें: शीर्षक missleading है।

देखो अपने कोड:

private void OnTimedEvent(object source, ElapsedEventArgs e) 
{ 
    Console.WriteLine("The Ping Elapsed event was raised at {0}", e.SignalTime); 

    //Record through win32dll the application foreground caption 

    // You miss the output or usage of the return value here. 
    GetActiveFileNameTitle(); 
    // Try 
    var procName = GetActiveFileNameTitle(); 
    Console.WriteLine(procName); 

    //Store into collection object, Push into ArrayList, Push into process id 
} 

कार्य उदाहरण:

[DllImport("user32.dll")] 
public static extern IntPtr GetForegroundWindow(); 

अग्रभूमि विंडो मिलता है और करने के लिए:

[DllImport("user32.dll", SetLastError = true)] 
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); 

ProcessID निर्धारित करने के लिए। ProcessID के साथ आप प्रक्रिया का नाम प्राप्त करने में सक्षम हैं। प्रक्रिया कक्षा के साथ: http://msdn.microsoft.com/de-de/library/system.diagnostics.process.mainmodule.aspx

class Program 
{ 
    static void Main(string[] args) 
    { 
     Thread.Sleep(5000); // Test it with 5 Seconds, set a window to foreground, and you see it works! 
     IntPtr hWnd = GetForegroundWindow(); 
     uint procId = 0; 
     GetWindowThreadProcessId(hWnd, out procId); 
     var proc = Process.GetProcessById((int)procId); 
     Console.WriteLine(proc.MainModule); 
     Console.ReadKey(); 
    } 

    [DllImport("user32.dll")] 
    public static extern IntPtr GetForegroundWindow(); 

    [DllImport("user32.dll", SetLastError = true)] 
    public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); 
} 
संबंधित मुद्दे