Jay1b Posted September 15, 2003 Posted September 15, 2003 I am using a tick event within a form to trigger a timer (see other thread). The tick code for this timer is: Dim c As CommClass Dim t As New System.Threading.Timer(AddressOf c.Tick, Nothing, 0, 5000) The class listens for this and then triggers the code below Public Class CommClass 'Dim t As New System.Threading.Timer(AddressOf Tick, Nothing, 0, 5000) Public Shared Sub Tick(ByVal state As Object) Dim comm2 As New AxMSCommLib.AxMSComm comm2.InputLen = 5000 comm2.OutBufferSize = 5000 comm2.InBufferSize = 5000 comm2.CommPort = 1 comm2.InputMode = MSCommLib.InputModeConstants.comInputModeText comm2.Handshaking = MSCommLib.HandshakeConstants.comNone comm2.RThreshold = 1 comm2.SThreshold = 0 comm2.Settings = "9600,n,8,1" comm2.PortOpen = True 'Opens port comm2.DTREnable = True 'Prepares port to receive data 'comm2.RTSEnable = True 'This prepares the connection to send data MessageBox.Show("test") strNewRecFromBuffer = comm2.Input() ReadBuffer() End Sub End Class However when i run it i get the following error message. "An unhandled exception of type 'System.Threading.ThreadStateException' occurred in system.windows.forms.dll Additional information: Could not instantiate ActiveX control '648a5600-2c6e-101b-82b6-000000000014' because the current thread is not in a single-threaded apartment." As you can imagine, i wouldnt have a clue what this means! Anybody got any ideas? :confused: Quote
Administrators PlausiblyDamp Posted September 15, 2003 Administrators Posted September 15, 2003 IIRC ActiveX controls have problems running on threads other than the applications main thread. You may be better of having a quick search of these archives as I'm sure somebody mentioned a RS232 wrapper class not too long ago. I also think http://www.mentalis.org have a serial port wrapper which may be useful. http://www.mentalis.org/classlib/class.php?id=15 edit: bothered to find the url ;) Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Jay1b Posted September 16, 2003 Author Posted September 16, 2003 I saw that article previously. Although its rather indept, its actually its depth which makes it rather unhelpful for me. In other words, as i am still rather crap at .NET i need a simple example for me to understand - where as that is way beyond my present capabilities to understand. Quote
Jay1b Posted September 16, 2003 Author Posted September 16, 2003 I did read somewhere about putting thism before my sub main bit. <STAThread()> ........ <STAThread()> Public Shared Sub Main() Dim initForm As New frmStartup Application.Run() End Sub But it doesnt seem to work. Quote
aewarnick Posted September 16, 2003 Posted September 16, 2003 Why don't you use a System.Windows.Forms.Timer instead? Quote C#
Jay1b Posted September 16, 2003 Author Posted September 16, 2003 It wouldnt work, if the form is hidden. Quote
*Experts* mutant Posted September 16, 2003 *Experts* Posted September 16, 2003 A timer from the Windows.Forms namespace doesnt need a form to operate. Public Shared Sub main() Dim t As New Windows.Forms.Timer 'create the timer object AddHandler t.Tick, AddressOf Tick 'add an event handler t.Interval = 500 'set the interval t.Start() 'start the timer Do Application.DoEvents() 'let the app process messages Loop End Sub 'the tick event handler Private Shared Sub Tick(ByVal sender As Object, ByVal e As System.EventArgs) MessageBox.Show("Hello") End Sub This just an example, it will show you that the message box will be shown. Quote
Jay1b Posted September 16, 2003 Author Posted September 16, 2003 Mutant that very similar to what i am using. The timer is working fine, its the comm control which i am having trouble with. Its having problem, running on multi threads. or something like that. Thanks Quote
*Experts* mutant Posted September 16, 2003 *Experts* Posted September 16, 2003 Im sorry, I thought you had a problem with the timer because aewarnick mentioned it and you said it didnt work without a form. :) Quote
Jay1b Posted September 16, 2003 Author Posted September 16, 2003 :) No need to apologise, you and PlausiblyDamp have both been a fantastic help to me on these forums, its truthfully appreciated. Quote
aewarnick Posted September 16, 2003 Posted September 16, 2003 You mentioned something like this: public class Startup { [sTAThread] static void Main() { Form1 f=new Form1(); Application.Run(); } } above. Do not put it in the Form you are hiding, (here it is Form1), make sure it is in a totally separate class OUTSIDE of the Form1 class. I THINK that should work for you.? Quote C#
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.