Error Msg within class

Jay1b

Contributor
Joined
Aug 3, 2003
Messages
640
Location
Kent, Uk.
I am using a tick event within a form to trigger a timer (see other thread). The tick code for this timer is:

Visual Basic:
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

Visual Basic:
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:
 
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.
 
I did read somewhere about putting thism before my sub main bit.

Visual Basic:
 <STAThread()> ........

    <STAThread()> Public Shared Sub Main()
        Dim initForm As New frmStartup
        Application.Run()
    End Sub

But it doesnt seem to work.
 
A timer from the Windows.Forms namespace doesnt need a form to operate.
Visual Basic:
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.
 
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
 
:) No need to apologise, you and PlausiblyDamp have both been a fantastic help to me on these forums, its truthfully appreciated.
 
You mentioned something like this:
C#:
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.?
 
Back
Top