Connecting Tech Pros Worldwide Help | Site Map

ActiveFrom in a windows application

Newbie
 
Join Date: Sep 2009
Posts: 6
#1: Oct 7 '09
Hi , i am developing a winforms application.
I have four forms which is inherited from Baseform in another project .
In all four forms i am using a label to show some transaction count based on network status. i have implemented a thread which gets active form of application and settign up the text . Code works fine if application screen is active, if minimize and open any other application , i am getting an null error exception.
Please help me , how to get an active forms of an application.


Expand|Select|Wrap|Line Numbers
  1. Private Sub StartThread() 
  2. pollThread =New Thread(AddressOf PollfileStatus) 
  3. pollThread.IsBackground =True
  4. running =True
  5. pollThread.Start()
  6. End Sub
  7.  
  8. Private Sub PollfileStatus() 
  9. While (running)
  10. Try
  11. For Each e As Control In Me.ActiveForm.Controls 
  12. If (e.Name = "pbStatus") Then
  13. e.Invoke(New SetTextCallback(AddressOf Settext), New Object() {e, 10}) 
  14. End If
  15. Next
  16. Catch ex As Exception 
  17. Throw New ApplicationException(ex.Message) 
  18. End Try
  19. Thread.Sleep(6000)
  20. End While
  21. End Sub
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#2: Oct 7 '09

re: ActiveFrom in a windows application


TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#3: Oct 7 '09

re: ActiveFrom in a windows application


Suggestion: Don't use Thread.Sleep() if you can at all avoid it and don't have a solid reason for bringing your program to a sudden stop. Everything STOPS when you sleep. No background events on the thread, no paying attention to incoming events, nothing on the thread happens and most times that not really what people want.

Quote:
i have implemented a thread which gets active form of application and settign up the text . Code works fine if application screen is active, if minimize and open any other application , i am getting an null error exception.
That sounds like the correct behavior for what you coded.
In your own words, the thread gets the active form of the application...
When that application has no active form (such as when minimized) it breaks.

If you are getting a null exception then you need to see which object/variable is null. My guess is that it is 'e' around lines 11 and 12. I'm guessing that when "Me.ActiveForms.Controls" returns nothing because there are no active controls, then you try to get the name of that nothing it all breaks. One should not assume the return of any function is going to be something. One should always perform some sort of check to make sure their assumptions are right. if (e != null) for example

You do realize you are throwing the exception, right? In line 17.
Are you catching that elsewhere higher up?
Reply

Tags
windows forms