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

  • SEARCH
  • Home
  • 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 1056519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:43:33+00:00 2026-05-16T17:43:33+00:00

I am using this PictureBox on a form, to this picture box I use

  • 0

I am using this PictureBox on a form, to this picture box I use the AForge Code. I pass the REFERENCE of the pictureBox into a webcam class I create that initializes the webcam and tells it where to draw its frames to….so it happily draws it frames… no problemo.

But then certain times (when I want to do stuff with said image, if a chck box is clicked)…I start this timer using simple code:

timer1.Enabled = true;

this timer’s interval is set to 33.

So now its firing along and every time through the loop my code has this:

private void timer1_Tick(object sender, EventArgs e)
{
    ...
    double value = detector.ProcessFrame(new Bitmap(picCapture.Image)); //errors here
    ...

        TimerCallback tc = new TimerCallback(sendDataFast);
        System.Threading.Timer t = new System.Threading.Timer(tc, null, 2000, Timeout.Infinite);

}

That line above has one of three errors I have seen on it (Stack traces where available):

Object is currently in use elsewhere.

Out of Memory.
(A first chance exception of type 'System.OutOfMemoryException' occurred in System.Drawing.dll)


Parameter not valid
(A first chance exception of type 'System.ArgumentException' occurred in System.Drawing.dll)

I am certain these are threading issues but I have no clue how to deal with them…I am totally lost. If the program hangs on that line above, I can usually click run again in the debugger and all is well. But I don’t want to be sloppy and just put in a willy nilly try catch that continues. I would like to figure out the root of this issue..

I saw somewhere else someone said it could be a threading issue and to put this line:
System.Diagnostics.Debug.Assert(!this.InvokeRequired, “InvokeRequired”);

So I did at the top of that time1_click method but the assert doesn’t seem to be happening, but i am not sure this was the right place for the assert… is timer1_click in a UI thread or not?

I suspect now that I reviewed my code its something with the way I initialize my webcam class:

Or within that timer1_click I also make a call to this method:

void sendDataFast(Object stateObject)
{
    EmergencyDelegate delEmergency =
           new EmergencyDelegate(mic.sendDataEmergency);

    // call the BeginInvoke function! //sendDataEmergency takes in a picture Image picImage as an argument.
    delEmergency.BeginInvoke(picCapture.Image, null, null);
}

And for completeness this is how I initialize my webcam class:

        webcam = new WebCam();
        webcam.InitializeWebCam(ref picCapture, ref picComparator, ref dataObject, this);            //guessing this is calling threading issues        

Those three errors that happen don’t happen right away, seems to happen randomly one of the three…. leads me to think its a threading issue but how else can I fix this? creating a delegate for some reason that returns that double value and is called if invoke required is true?

  • 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-16T17:43:34+00:00Added an answer on May 16, 2026 at 5:43 pm

    is timer1_click in a UI thread or not?

    Depends on which timer you are using. sendDataFast definitely isn’t because you used a System.Threading.Timer.

    If you take a look at the MSDN documentation on System.Threading.Timer you’ll see the following

    System.Threading.Timer is a simple,
    lightweight timer that uses callback
    methods and is served by thread pool
    threads. It is not recommended for use
    with Windows Forms, because its
    callbacks do not occur on the user
    interface thread.
    System.Windows.Forms.Timer is a better
    choice for use with Windows Forms.choice for use with Windows Forms.

    This is explains why you’re getting freezes.

    The callback method executed by the
    timer should be reentrant, because it
    is called on ThreadPool threads. The
    callback can be executed
    simultaneously on two thread pool
    threads if the timer interval is less
    than the time required to execute the
    callback, or if all thread pool
    threads are in use and the callback is
    queued multiple times.

    Which means if your function fails to execute in under 33 ms the function will be called again. This is probably the case and why you’re getting the exceptions you’re seeing. Two or more threads are trying to use the same file. You may also have multiple threads trying to allocate a large block of Memory and one fails to get the block of the size you’ve requested. Not sure why you’re getting the argument exception but it may be because of the previous two.

    For this reason I prefer the System.Timers.Timer. It has an AutoReset Property that set false. Then at the end of my function I call Timer.Start. You can accomplish the same thing with the other timers but its a little tricker.

    Here are three links you might find useful

    Article on Comparison of the Timer Classes

    Jon Skeet Answer on a Begin Invoke Question

    Eric Lippert Blog on what an OutOfMemory Exception likely is

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using this code, to make a request to a given URL: private static
I am trying to change images in the picture box of windows form according
In c# I am using a PictureBox on a win form. I am trying
I'm using the following code to print an image from a PictureBox. All works
Using This code: I am tring to fill the page with the data and
I have a picture box which I draw some stuff on it using a
Using this question as the base is there an alogrithm or coding example to
Since I have started using this site, I keep hearing about the Boost library.
I've been using this nifty LINQ to SQL tool for a data access layer
anyone have any experience using this? if so, is it worth while?

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.