开发者

Problems stopping a thread when signaled from a menu event?

This samplet is from a project I am working on. My client uses software which uses his webcam to take snapshots if motion is detected within it's view. The problem is that the software is unable to email the images to his account as they are taken. I wrote this project to monitor the folder where the snapshots are saved, and set it up to send any new snapshots in an email to the defined account, this way he'll get an alert on his cell as it happens, along with a snapshot of what was caught in motion. Yes I am aware of the fact that there are numerous applications that have this feature included within their webcam software, but my client has hired me so he can avoid having to replace his current software as he is comfortable using.

There are two steps to the Start() function. Step 1 is a delay before the monitor starts, Step 2 is the launch of the monitor itself. So far I am unable to get the Stop() function to kill Step 1 from counting down in the statusBar of the GUI.

The Monitor.Stop() function works fine when run by itself within the console, but it doesn't work when it's run from within the self.OnConnect() event handler within the interface? I have tried multiple variations of threading structures using: threading, thread, kthread, etc., but all have ended with the same result.

Ideally I want to click Connect under the File menu -> Connect label changes to Disconnect -> monitor starts with Step 1 -> statusBar displays time remaining until Step 2.

At this point I want to be able to stop the countdown by hitting Disconnect under the File menu but when I click it the countdown continues on? Each variation of the thread functions I have tried have successfully stopped the thread when run from within a console, but I am so far unable to figure out why the countdown fails to stop when called from within my gui?

Any help would be greatly appreciated! :D

Btw, make sure to replace the two "C:\Replace\With\Valid\Path" entries at the bottom of the script with a valid path on your system if you chose to run it.

import os, sys, thread, time, wx



class Frame(wx.Frame):

    def __init__(self, parent, id=-1, title="A Frame", path="", pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE):
        wx.Frame.__init__(self, parent, id, title, pos, size, style)

        self.path = path
        self.StatusBar = wx.StatusBar(self, -1)
        self.StatusBar.SetFieldsCount(3)
        self.StatusBar.SetStatusWidths([-2, -1, -1])
        self.InitMenuBar()

    def InitMenuBar(self):
        menuBar  = wx.MenuBar()
        menuFile = wx.Menu()
        menuHelp = wx.Menu()
        self._Connect = menuFile.Append(101, "&Connect", kind=wx.ITEM_CHECK)
        menuFile.AppendSeparator()
        menuFile.Append(104, "E&xit")
        menuBar.Append(menuFile, "&File")
        menuBar.Append(menuHelp, "&Help")
        self.SetMenuBar(menuBar)
        self.Bind(wx.EVT_MENU, self.OnConnect, self._Connect)

    def OnConnect(self, event):
        #print [event.IsChecked()]
        mon = Monitor("", "", "", self.path, "60", self.StatusBar)
        if event.IsChecked():
            print "Set Menu Label Disconnected"
            self._Connect.SetItemLabel("Disconnect")
            print "Start Monitor"
            mon.Start()
            print "Start Finished"
        else:
            print "Set Menu Label Connected"
            self._Connect.SetItemLabel("Connect")
            print "Stop Monitor"
            mon.Stop()
            print "Stop Finished"


class Monitor:

    def __init__(self, email, password, recipient, path, timeout, statusBar=None):

        self.email     = email
        self.password  = password
        self.recipient = recipient
      开发者_如何学运维  self.path      = path
        self.timeout   = timeout
        self.statusBar = statusBar
        #self.lock      = thread.allocate_lock()

    def Start(self):
        #self.lock.acquire()
        self.running = True
        thread.start_new_thread(self.Run, ())
        #self.lock.release()

    def Stop(self):
        #self.lock.acquire()
        self.running = False
        #self.lock.release()

    def IsRunning(self):
        return self.running

    def Run(self):
        start = NewestByModTime(self.path)
        count = int(self.timeout)
        while self.running:
            #print self.running
            # Step 1 - Delay the start of the monitor for X amount of seconds, updating the
            # statusbar/console each second to relfect a countdown. remove one from count each
            # loop until the count equals 0, than continue on to Step 2.
            if count > 0:
                if self.statusBar:
                    self.statusBar.SetStatusText("Monitoring will begin in %s seconds" % (count))
                else:
                    sys.stdout.write("Monitoring will begin in %s seconds\r" % (count))
                    #sys.stdout.flush()
                count -= 1
                time.sleep(1)
            # Step 2 - Start the monitor function which monitors the selected folder for new
            #files. If a new file is detected, send notification via email with the new file
            #as an attachment. (for this project, files in the folder will always be jpg images)
            # *NOTE* I Have not tested the Stop() function during Step 2 just yet, but I would
            # assume it would fail just the same as . *NOTE*
            if count == 0:
                current = NewestByModTime(self.path)
                if current[1] > start[1]:
                    print "Activity Detected"
                    start = current
                    print "Sending Notification Email"
                    #sendMail(self.email, self.password, self.recipient, "JERK ALERT!!",
                    #         "Some jerkoff is in your place right now, wanna see who it is??", "%s\\%s" % (self.path, start[0]))
                    print "Notification Email Sent!"
        print 
        self.running = False


def NewestByModTime(path):
    stat = ["", 0]
    for a in os.listdir(path):
        new = os.path.getmtime("%s\\%s" %(path, a))
        if new > stat[1]:
            stat = [a, new]
    return stat

if __name__ == "__main__":
    # Run GUI
    app   = wx.PySimpleApp()
    frame = Frame(None, -1, "Test Frame", "C:\\Replace\\With\\Valid\\Path", size=(800, 600))
    frame.Show()
    app.MainLoop()
    del app

    ## Run Console
    #mon = Monitor("", "", "", "C:\\Replace\\With\\Valid\\Path", "60", None)
    #mon.Start()
    #time.sleep(10)
    #mon.Stop()


Just figured it out.. Why are my biggest problems always one line fixes?? :P

Apparently I overlooked the fact that I was recreating the variable mon each time self.OnConnect was called, I kinda clued in on this mistake when I got frustrated enough to furiously click connect/disconnect a billion times and watched the counter swap between different countdowns 59, 43, 58, 42, etc. LOL

I changed mon to self.mon and added in if not hasattr(self, "mon") to stop self.mon from recreating itself each time. So far the problem is solved and the countdown stops perfectly.

def OnConnect(self, event):
    #print [event.IsChecked()]
    mon = Monitor("", "", "", self.path, "60", self.StatusBar)
    if event.IsChecked():
        print "Set Menu Label Disconnected"
        self._Connect.SetItemLabel("Disconnect")
        print "Start Monitor"
        mon.Start()
        print "Start Finished"
    else:
        print "Set Menu Label Connected"
        self._Connect.SetItemLabel("Connect")
        print "Stop Monitor"
        mon.Stop()
        print "Stop Finished"

To:

def OnConnect(self, event):
    #print [event.IsChecked()]
    if not hasattr(self, "mon"):
        self.mon = Monitor("", "", "", self.path, "60", self.StatusBar)
    if event.IsChecked():
        print "Set Menu Label Disconnected"
        self._Connect.SetItemLabel("Disconnect")
        print "Start Monitor"
        self.mon.Start()
        print "Start Finished"
    else:
        print "Set Menu Label Connected"
        self._Connect.SetItemLabel("Connect")
        print "Stop Monitor"
        self.mon.Stop()
        print "Stop Finished"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜