9 Replies - 7859 Views - Last Post: 14 January 2013 - 04:33 PM Rate Topic: -----

#1 mpooley  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-March 11

Process.MainwindowTitle is empty string in certain circumstances

Posted 14 January 2013 - 04:57 AM

Hi all

I am just using this test code to make sure I have the principal right but have hit problems straight away.
Dim RunningProcesses() As Process
		RunningProcesses = Process.GetProcesses


		For Each instance As Process In RunningProcesses

			instance.Refresh()

			If instance.ProcessName.Contains("Heating Viewer") Then
				ListBox1.Items.Add(instance.ProcessName)

				ListBox1.Items.Add(instance.MainWindowTitle)

			End If


		Next

I wrote this last night and on first run it worked ok. When i closed "Heating Viewer.exe" it stopped adding to the listbox as expected but when I start "Heating Viewer.exe" again it finds the process name but not the MainWindow Title ? and no matter how many times I ran this code it never finds the MainWindowTitle again?

I checked Process class on MSDN and read about the cache so I added the refresh command as suggested but this makes no difference.

On running it for first time this morning after a reboot it worked for the first time again but then same as last night.

I just re-started VS 2012 to see if that would make a difference but even the first run found the processname but not the mainwindowTitle
Can anyone tell me what I am doing wrong please?

Thanks

Mike

Is This A Good Question/Topic? 0
  • +

Replies To: Process.MainwindowTitle is empty string in certain circumstances

#2 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Process.MainwindowTitle is empty string in certain circumstances

Posted 14 January 2013 - 05:56 AM

How are you starting the process you're trying to look at? If you are starting it from the code you are writing, I suspect you have a timing problem. The process will be started, and thus, have a name, well before the form shows, and before the form has a title.

If you are starting the process externally to your program, your code should work. I tested with notepad, and it works well.
Was This Post Helpful? 0
  • +
  • -

#3 mpooley  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-March 11

Re: Process.MainwindowTitle is empty string in certain circumstances

Posted 14 January 2013 - 06:00 AM

View Postlar3ry, on 14 January 2013 - 05:56 AM, said:

How are you starting the process you're trying to look at? If you are starting it from the code you are writing, I suspect you have a timing problem. The process will be started, and thus, have a name, well before the form shows, and before the form has a title.

If you are starting the process externally to your program, your code should work. I tested with notepad, and it works well.

Thanks for your reply.
That is very strange!
I am not starting the process in my code and I have tried using notepad as the process and that definitely never works! not once!

I am very confused now.
Oh BTW I forgot to mention the code is running in a timer loop.

thanks

Mike
Was This Post Helpful? 0
  • +
  • -

#4 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Process.MainwindowTitle is empty string in certain circumstances

Posted 14 January 2013 - 07:03 AM

Quote

I am very confused now.
Oh BTW I forgot to mention the code is running in a timer loop.

What interval are you using for the timer? I found that if I used 1 second (interval = 1000), I would get an exception intermittently:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.dll

Additional information: Process has exited, so the requested information is not available.


Here's a couple of things to try.

1. Put the routine into a Try/Catch block.
2. Place the Code in a Button Click event

This should tell us something, I think.
Was This Post Helpful? 0
  • +
  • -

#5 mpooley  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-March 11

Re: Process.MainwindowTitle is empty string in certain circumstances

Posted 14 January 2013 - 07:56 AM

Ok I tried that with no change to the result:
I also tried to list all windows with titles just to see which processes had titles.
 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
		Try
			Dim RunningProcesses() As Process
			RunningProcesses = Process.GetProcesses
			For Each instance As Process In RunningProcesses

				instance.Refresh()

				Dim MWT As String = instance.MainWindowTitle
				If MWT <> "" Then
					ListBox1.Items.Add(instance.MainWindowTitle)
				End If
				
			


			Next
		Catch ex As Exception
			Stop
		End Try

	End Sub 



Ok Now this is really wierd!
That didn't work! It didn't show my app or the notepad.

Then I though ! well I was just running notepad without it having been saved. although it has a title I though perhaps that's not enough
so I made a txt document and saved it.

I then re-opened it and guess what? not only did notepad show up which is sort of understandable but my app showed up as well?

Then it got worse lol I went to shut notepad to see if that changed the result. I then realised I had two notepads open, my original unsaved one and the new saved one. so being methodical I shut down the old unsaved one leaving the other one open, expecting the results to be the same but NO! Notepad doesn't show up anymore but my app does? :dontgetit:/>/>

so i shut down the other notepad instance and still got my app showing in the list.

ok I had not re-started my code at this point so i re-started it and without Notepad running my app is still showing in list so now this code appears to work but why didn't it in the first place?

Ah right! tried to repeat the original fault by re-running my code when my app was open again: that was ok. shut down app clicked button again and it wasn't there anymore, so that's ok. re-started app left it for 30 seconds and clicked button again. Now it's and not in the list!
even after a couple of minutes it's still not there!

re-started the code with the app still running and it's still not in the list!
this has got to be about the cache surely?

I hope you can follow this
and thanks for helping
Was This Post Helpful? 0
  • +
  • -

#6 torind_2000  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 57
  • View blog
  • Posts: 292
  • Joined: 22-August 11

Re: Process.MainwindowTitle is empty string in certain circumstances

Posted 14 January 2013 - 10:43 AM

Shouldn't you put a messagebox or something in your catch section?
Sorta pointless to wrap it in one and not see what it caught if there's an error

This post has been edited by torind_2000: 14 January 2013 - 10:44 AM

Was This Post Helpful? 0
  • +
  • -

#7 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Process.MainwindowTitle is empty string in certain circumstances

Posted 14 January 2013 - 11:23 AM

I still think it has to do with timing. I did a timer routine like this...
    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        Static count As Integer = 1
        Dim RunningProcesses() As Process
        RunningProcesses = Process.GetProcesses
        For Each instance As Process In RunningProcesses

            If instance.ProcessName.Contains("Temp") Then
                instance.Refresh()
                Dim name As String = instance.ProcessName & vbNewLine
                Dim title As String = instance.MainWindowTitle
                TextBox1.Text = name
                TextBox1.Text &= title & "  " & CStr(count) & vbNewLine
                Application.DoEvents()
                If title = "" Then
                    While title = ""
                        instance.Refresh()
                        title = instance.MainWindowTitle
                        TextBox1.Text &= "."
                        Application.DoEvents()
                    End While
                    TextBox1.Text &= title & vbNewLine
                    Application.DoEvents()
                    Debug.Print("Aha!")
                End If
                count += 1
            End If
        Next


I used TextBoxes for ease of debugging.
What this does is grabs the instance of the Process we are interested in, then checks for the title being an empty string. If it is, it keeps trying to get the title, outputting a dot for each null string it finds, then if the title shows up, it drops out of the While loop and shows the title. The count is in there to allow you to see that it has grabbed the instance again, and the DoEvents lets the TextBox Display while the code keeps running, and to show the error when it happens, and before the Breakpoint.

Start with a fresh project, make a TextBox and Timer, and paste in this code.

And then I made a separate program called Temp.Exe that looks like this...

Public Class Form1
    Dim rnd As New Random
    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        End
    End Sub

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Timer1.Interval = rnd.Next(1200, 2000)
        Timer1.Start()
    End Sub
End Class



Make a new project called "Temp", test it (it should run for a random time between 1.2 and 2 secnds, then end. If you try to run Temp with the debugger, it will not close down Temp.vshost, so make a Release version, close your Visual Studio Temp project. Right-Click on the Release\Temp.exe file, and tell it to pin it to the Taskbar.

In the first program's code editor, set a breakpoint on the Debug.Print("Aha!") line , run it, then start clicking on the Temp Taskbar icon. Allow it to load and exit each time.

Our machines may be different in a number of ways, but in my case, I get a "no title" about once in 15-40 tries, but it has not failed to get a title after about 10 tires (10 dots)

This post has been edited by lar3ry: 14 January 2013 - 11:25 AM

Was This Post Helpful? 0
  • +
  • -

#8 mpooley  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-March 11

Re: Process.MainwindowTitle is empty string in certain circumstances

Posted 14 January 2013 - 01:04 PM

In the test app what timer interval are you using?

I have tried 10 to 2000

I get Temp flickering in the textbox and then an
error 'invalidOperationException' 'Process has exited,so the requested information is not available'
Was This Post Helpful? 0
  • +
  • -

#9 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Process.MainwindowTitle is empty string in certain circumstances

Posted 14 January 2013 - 02:33 PM

View Postmpooley, on 14 January 2013 - 02:04 PM, said:

In the test app what timer interval are you using?

I have tried 10 to 2000

I get Temp flickering in the textbox and then an
error 'invalidOperationException' 'Process has exited,so the requested information is not available'

I used 1000 (1 second). It would appear that our machines are very different. I use an Acer 64 bit AMD running at 2.9 GHz, so it's pretty quick.

Try changing rhe timer in your code to 1000, then change the rnd.Next values in Tem.exe to hgher values, say, 2000 to 4000. Experiment with the Temp.exe Timer settings. Basically what you want to do is to make the timer in Temp keep the program up longer than it takes to grab all the processes and output the info to the TextBox.

Better still, modify the Temp program to not use the time at all. Just delete all the code between the Class and End Class statements. Then add two lines right after the Debug.Print in the test code, like this:
                    Debug.Print("Aha!")
                Else
                    instance.Kill()


This will close Temp.exe as soon as it has detected a title. If you keep the breakpoint on the Debug.Print, you will stop when it has a null string for a title, and otherwise, you can just keep clicking the TaskBar icon.

It took 47 tries to fail for me, on the first test, 77 on the second.

This post has been edited by lar3ry: 14 January 2013 - 02:37 PM

Was This Post Helpful? 0
  • +
  • -

#10 mpooley  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-March 11

Re: Process.MainwindowTitle is empty string in certain circumstances

Posted 14 January 2013 - 04:33 PM

All I could get was 'Temp' flickering in the text box! it never finds the window.
I adapted your code a bit to see more clearly what was happening.

So I put it back into a button event and used a listbox again. Plus I added a label.
I get the process name every time i click but never get the title

I put a counter in the while loop and displayed that in the label to make sure it was iterating.

If I leave it,It counts into the hundred thousands and I still don't the title!

It must be something to do with my machine.
My O/S is win 7 and it's a fast quad core intel.
But to be honest I can't see why that would make a difference.

I think this is not going to work!
perhaps I'll try another way to do this.

Thank you for trying to help. :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1