thread differences from button_Click and timer_Elapsed

  • Thread starter Thread starter Victor Reboucas
  • Start date Start date
V

Victor Reboucas

Hi, I'm experiencing some kind of thread behavior problem in a WinForms app
in Vb.Net 2.0.

Actually I'm doing some huge interop using DirectShow to interact with my
miniDV camera.

The problem is that if I put the code to read the camera's timecode inside a
button_click routine it works just fine, but, if I put that code to run on a
timer_elapsed event I get error (in particular, NotImplementedException from
the interop layer).

The question is: what is different in calling a method in a button click
routine and in a timer elapsed event (in threads meaning), is that the "user
interaction" (button click) that creates a new thread automatically or sth
like that?


Thanks for any clue,
Victor
 
[...]
The question is: what is different in calling a method in a button click
routine and in a timer elapsed event (in threads meaning), is that the
"user interaction" (button click) that creates a new thread
automatically or sth like that?

If anything, it would be the timer that uses a different thread (it
wouldn't create it, but rather get one from the thread pool). Whether it
does that depends on which timer class you're using. The Forms.Timer
signals the timer event on the same thread as the form, but the other
timers don't. Your button's click event will run on the form's thread,
which is the main UI thread (unless you've done something specific to
change that).

That should be enough information to tell you what's going on, but if you
really want to check to see whether the two techniques wind up running on
different threads, you can get the Thread.ManagedThreadId property and
compare them between the two executions (in the same process, of course).
If the number is different, then one of the methods is using a different
thread than the other.

It does sound as though you're not using the Forms.Timer class, and that
the timer-based method does execute on a different thread. I suspect that
the main thing is that COM hasn't been initialized (or hasn't been
initialized properly for your use of DirectShow) on the timer thread, but
I don't have much in the way of specifics as to how you'd do that. If
you're using a timer that's using a thread pool thread, then you never
know which thread is actually going to be used. You are probably better
off either dedicating a thread to your timing somehow, or using Invoke()
to put the processing back on the main thread.

Pete
 
Hi Pete,

Thanks a lot for your help... it just solved my problem!!!

I was using system.timers.timer and just changing that to forms.timer solved
the problem.


Thanks a lot,
Regards
Victor


Peter Duniho said:
[...]
The question is: what is different in calling a method in a button click
routine and in a timer elapsed event (in threads meaning), is that the
"user interaction" (button click) that creates a new thread
automatically or sth like that?

If anything, it would be the timer that uses a different thread (it
wouldn't create it, but rather get one from the thread pool). Whether it
does that depends on which timer class you're using. The Forms.Timer
signals the timer event on the same thread as the form, but the other
timers don't. Your button's click event will run on the form's thread,
which is the main UI thread (unless you've done something specific to
change that).

That should be enough information to tell you what's going on, but if you
really want to check to see whether the two techniques wind up running on
different threads, you can get the Thread.ManagedThreadId property and
compare them between the two executions (in the same process, of course).
If the number is different, then one of the methods is using a different
thread than the other.

It does sound as though you're not using the Forms.Timer class, and that
the timer-based method does execute on a different thread. I suspect that
the main thing is that COM hasn't been initialized (or hasn't been
initialized properly for your use of DirectShow) on the timer thread, but
I don't have much in the way of specifics as to how you'd do that. If
you're using a timer that's using a thread pool thread, then you never
know which thread is actually going to be used. You are probably better
off either dedicating a thread to your timing somehow, or using Invoke()
to put the processing back on the main thread.

Pete
 
Back
Top