Threading Timer_Elapsed

G

Guest

Hello,

I'm a new user to Visual Basic.net and I would appreciate any help regarding
a problem I have. I have searched the posts in this newsgroup and the VB
library for threading topics, but I get confused with the code that is
displayed. Anyway, I have a System.Timers.Timer object in a form and its
interval is 50 ms. I want to create a thread around the Timer_Elapsed event
(ie. myThread = new System.Threading.Thread(AddressOf Timer_Elapsed)) but
the signature doesn't match the delegate. Could someone please tell me how
to create a thread around this event? I did look up Threaded Timers but I
got even more confused with the code. Thanks in advance.
 
L

Larry Serflaten

NewUser said:
Hello,

I'm a new user to Visual Basic.net and I would appreciate any help regarding
a problem I have.

You may want to include what you plan to do, so that advice can be given
on methods to achieve the task....
Anyway, I have a System.Timers.Timer object in a form and its
interval is 50 ms. I want to create a thread around the Timer_Elapsed event
(ie. myThread = new System.Threading.Thread(AddressOf Timer_Elapsed)) but
the signature doesn't match the delegate. Could someone please tell me how
to create a thread around this event? I did look up Threaded Timers but I
got even more confused with the code. Thanks in advance.

Which is why you should give some indication about what you want to
accomplish with your added thread. The form's timer runs on the form's
thread, where you can interact with the form. A system timer runs on a
thread from a pool of threads, where interacting with your form will be
made more complex.

What do you want the thread for?

LFS
 
G

Guest

Hello again,

I have a graphical user interface that requires a Matlab program to be run
every 50 ms or so. In my Timer_Elapsed event, I only execute the following
code:
mlapp.Execute("Runsim") where mlapp is the COM object (ie using
CreateObject("Matlab.Application")) and Runsim is the name of the Matlab
program I'm calling. I would do all of this in Matlab GUIDE, but my version
of Matlab does not support the ActiveX controls that I need. Anyway, since
the Matlab program is rather large and the interval required is small, my GUI
will freeze up. Therefore, I'm trying to make a separate thread to run this
Timer_Elapsed event so repainting isn't affected too much. I hope that helps
you to give me a better indication of what to do. Thanks in advance again.
 
L

Larry Serflaten

NewUser said:
I have a graphical user interface that requires a Matlab program to be run
every 50 ms or so. In my Timer_Elapsed event, I only execute the following
code:
mlapp.Execute("Runsim") where mlapp is the COM object (ie using
CreateObject("Matlab.Application")) and Runsim is the name of the Matlab
program I'm calling. I would do all of this in Matlab GUIDE, but my version
of Matlab does not support the ActiveX controls that I need. Anyway, since
the Matlab program is rather large and the interval required is small, my GUI
will freeze up. Therefore, I'm trying to make a separate thread to run this
Timer_Elapsed event so repainting isn't affected too much. I hope that helps
you to give me a better indication of what to do. Thanks in advance again.


Well, NewUser, do you really want to tackle advanced COM interop on your
first time out? Mixing threads for what very well may need to be single threaded
may not be the way to go, but I'll defer to anyone else who can provide more
experience, because my experience with COM interop proxies is next to nothing.

If it were me, I'd look into creating one assembly to handle the Matlab program,
and a second assembly to handle the GUI and provide methods or events to
transfer data between the two. But again, wait for other replies to perhaps find
someone with more experience with what you want to do. If you don't see any
replies by tomorrow at this time, post a new message with 'COM Interop threading'
in the subject line and repeat what you want to do, like you stated above. Maybe
that will better attract those with experience in that area....

Good luck!
LFS
 
T

Tom Shelton

Hello,

I'm a new user to Visual Basic.net and I would appreciate any help regarding
a problem I have. I have searched the posts in this newsgroup and the VB
library for threading topics, but I get confused with the code that is
displayed. Anyway, I have a System.Timers.Timer object in a form and its
interval is 50 ms. I want to create a thread around the Timer_Elapsed event
(ie. myThread = new System.Threading.Thread(AddressOf Timer_Elapsed)) but
the signature doesn't match the delegate. Could someone please tell me how
to create a thread around this event? I did look up Threaded Timers but I
got even more confused with the code. Thanks in advance.

The system.timers.timer event happens on a separate thread already,
there is nothing to do there.

Wait a minute... Your using this in a form from the ide aren't you? If
that's the case, and you aren't going to be accessing the GUI, then you
need to set the timers SynchronizingObejct property to nothing, or it
will automatically marshal the event to your forms thread...

Another thing - you may experience problems with your COM object on the
other thread if it isn't an MTA capable object. If that's the case,
you'll need to spawn the thread manually and set it's apartmentstate to
STA. Then you would have to do something like:

do until teminateflag
call yourbobject
thread.sleep (50)
loop

And remember, you can't directly access your form from any of these
threads. If you need to access the GUI, you'll need to look at the
following article:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms06112002.asp

this is part 1 of a 3 part article. You should read them all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top