Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1079
  • Last Modified:

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

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
dolamitejenkins
Asked:
dolamitejenkins
  • 6
  • 4
1 Solution
 
gelonidaCommented:
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
 
dolamitejenkinsAuthor Commented:
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
 
gelonidaCommented:
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
dolamitejenkinsAuthor Commented:
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
 
dolamitejenkinsAuthor Commented:
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
 
dolamitejenkinsAuthor Commented:
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
 
gelonidaCommented:
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
 
dolamitejenkinsAuthor Commented:
thank you
0
 
gelonidaCommented:
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
 
dolamitejenkinsAuthor Commented:
thanks
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now