Solved

Error: Can't create window of class wxWindowClassNR (error 1407: cannot find window class.)

Posted on 2011-09-26
10
919 Views
Last Modified: 2012-05-12
Im getting the above error and while I can still launch my program its connected to my socket thread some how.... Why am I getting it ? What does it mean and how can I fix it ? If it matters I'm using wxpython ... do I need to import a module or something ?

class ThreadClass(threading.Thread):
    def run(self):
	f =open("ipaddress.txt","r")
	self.ip = f.readline() # reads the lines
	f.close()
	print self.ip
        while True:
	    #print 'attempt'
	    ntwrk=open ("network.txt","a")
	    ntwrk.write('handle on connection attempt')
	    ntwrk.close
	    
            #print "handle one connection attempt"
            success = self.run_once()
            if success:
		#print "success"
		#print 'Success'
		ntwrk=open ("network.txt","a")
		ntwrk.write('sucess')
		ntwrk.close
               # do whatvever is appropriate
            else:
		#print "fail"
		#print 'failed'
		ntwrk=open ("network.txt","a")
		ntwrk.write('fail')
		ntwrk.close
               # do whatever is appropriate
		time.sleep(20) # adapt the delay 

    def run_once(self):
	
	host=self.ip
	#print host
        #host='169.254.159.196'
	#host='192.168.0.199'
        #host=socket.gethostname()
        port = 51269
        size = 1000000
        addr = (host,port)
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            f = open('duplicate_table2.txt', "r")
            data = f.read()         
            f.close()
            s.connect(addr)
            s.send(data)
            data = s.recv(size)
            s.close()
            #print "Got it"
            os.remove('duplicate_table2.txt')
            return True
        except socket.error:  
	    ntwrk=open ("network.txt","w")
	    ntwrk.write('socket error')
	    ntwrk.close
            #print "socket error"
        except IOError:
	    ntwrk=open ("network.txt","w")
	    ntwrk.write('IOError')
	    ntwrk.close
            #print "IOError"
        return False
        
ThreadClass().start() 

Open in new window

0
Comment
Question by:dolamitejenkins
  • 6
  • 4
10 Comments
 
LVL 16

Expert Comment

by:gelonida
ID: 36708081
Hmm,

I have difficulties believing, that the issue is related to above code.
Perhaps you accidentally changed something else in the wxWidgets related code?

Couldn't you just comment out the entire Thread class and rerun your program.

Probably you  should show us the backtrace message and the related lines of code of the Wxwidgets code.

0
 

Author Comment

by:dolamitejenkins
ID: 36712782
that's what is so confusing it does do that... almost like its not a python error but a windows os error,,, that is the entire error message  that occurs in the command window... it doesn't always appears only sometimes ... when it appears the network thread does not work... any thoughts
0
 
LVL 16

Expert Comment

by:gelonida
ID: 36713312
Some suggestions.

Create a small complete test case
----------------------------------------
Could you try to create the smallest program possible, that is still producing this error?
Mostly it is much easier to identify the problem.
and sometimes by coincidence you fall over the exact piece of code causing the problem


Look at the process status
-----------------------------------
Could you use the Process manager under windows (or ps  or top under Linux)

and Look at:
- the cpu load of your non working program
- the memory requirements of your application

Ideally you look at these values while your program is still working and lateron
when it is already dead.

Tell u sthe OS you're running on
------------------------------------------

Are you running under Linux or under Windows?
This information can help to suggest some debug strategies.

Uncomment the print statments and tell us where the socket thread dies / stops
--------------------------------------------------------------------------------------------------

you could uncomment the print statements and tell us which is the last print statement of
your socket thread.

Is it always the same????

Could you also add print statements in the wxWIdgets thread?


Tell us a little more about your program
--------------------------------------------------
I don't really care about the exact thing your program is doing, but some information might be helpful.

For example.
- Do you create any window when your program starts up.
- Do you create new windows  as reaction to the socket thread
- how do the socket thread and your WxWidget main loop communicate with each other (queues / events / . . . )

- Do you create more threads than the wxWidgets main lop and the socket thread?



0
 

Author Comment

by:dolamitejenkins
ID: 36713510
I have 3 loops running one timer thread , the socket thread and then a email thread...  I do create a window on start up ... what is unusual is the error occurs on start up of the program... so when all the thread are initiated... im gonna do the other things you suggest and post when I have a the results ...
0
 

Author Comment

by:dolamitejenkins
ID: 36714636
the code I posted is causing the error ... I commented it out and started the program 50 times no error and I un-commented it and right away the error reappeared .... is there something with threads or sockets and wxpython that I should be aware of ?
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:dolamitejenkins
ID: 36714821
I think I may have solved it by deleting wx.ContextHelpButton ... haven't had the error since ... have you heard of this ? why is this possibly the problem ?
0
 
LVL 16

Expert Comment

by:gelonida
ID: 36715727
No I don't have any idea. I never used wxWidgets :-( .


So to summarize:
- You have one socket thread
- you have one mailer thread
- you have one timer Thread (What is a timer thread ) Or do you start multiple timer threads within your loops???
No other thread is created later on

The window creation fails at startup (it fails only sometimes)

Perhaps you run out of some resources (memories / file handles, . ..  )
- What OS are you using?
- Did you check memory / cpu usage (or does your program die when the error occurs and you cannot look at CPU / memory consumption)

Could you create the window BEFORE your threads do any actual work? (You could just add a sleep() statement at the beginning of each thread, such, that you're sure the window is created before your threads start using memory / CPU?
Does the problem still occur if you don't start the mailer thread. (The less code you have to reproduce the problem the easier it is to idnetify)

Some minor comments on your code  (not explaining the bug).
- Sometimes you open 'network.txt' with 'a' mode (append) and sometimes with 'w' (create)
    Is this intentionally?
- On someplaces your code uses ntwrk.close . This hsould be ntwrk.close() with parenthesis.
    Otherwise the code has no effect.
- Your code might be nicer to read if you explicitely checked for the existence of the file to send

You could replace line 43
f = open('duplicate_table2.txt', "r")
with
fname = 'duplicate_table2.txt',
if not os.path.exists(fname):
    # log a message here if you want
    return False
f = open(fname, "r")



Instead of always repeating three lines of code you might create a logging function:

Example:

def log_msg(fname, mode, message):
    f = open(fname, mode)
    write(f, message)
    close(f)

Your code will be shorter and easier to read.

Alternatively I would look at the python logging module It is very flexible and very configurable:
A simple example of using it is:
# Add these lines at the beginning of your file:
import logging
netlogger = logging.getLogger('network')
netlogger.addHandler(logging.FileHandler('network.txt', mode='w'))
netlogger.setLevel(logging.DEBUG)
netlogger.propagate = False


Later in your code just use lines like following examples.
netlogger.info('sucess')
netlogger.error('socket error')
netlogger.warning('file %s does not exist' % fname)





0
 

Author Comment

by:dolamitejenkins
ID: 36717145
thank you
0
 
LVL 16

Accepted Solution

by:
gelonida earned 500 total points
ID: 36718028
Np problem if you're still stuck or want to get more info, then just contact me again  and send m a little more info like

Perhaps you run out of some resources (memories / file handles, . ..  )
- What OS are you using?
- Did you check memory / cpu usage (or does your program die when the error occurs and you cannot look at CPU / memory consumption)\
- does the porblem also occur without the mailer thread
- does the problem occur if you let all your threads sleep for a few seconds (and the window is thus created before all the thredas start their real activity)
0
 

Author Closing Comment

by:dolamitejenkins
ID: 36773840
thanks
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now