Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4239490
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:02:31+00:00 2026-05-21T03:02:31+00:00

This samplet is from a project I am working on. My client uses software

  • 0

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! 😀

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()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T03:02:31+00:00Added an answer on May 21, 2026 at 3:02 am

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

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on a project which uses XSL-Transformations to generate HTML from XML.
I'm working on a project which uses OpenGL framebuffer/renderbuffer to draw freehand lines. This
I'm working with some Fortran code (which I'd never used until this project...) and
I am trying to run a sample project from This site . Jboss starts
I've been using the sample code from this d3 project to learn how to
I have downloaded the sample code from this site for FTP Client http://www.lysesoft.com/products/andftp/index.html But
I have made one sample project which can add and edit records from SQLite
I'm working on a project on the iPhone where I'm recording audio from the
I'am working on a iPad project and this project needs to talk to a
I'm currently working on this project that implies some DSP skills. I must extract

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.