Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

  • Administrators
Posted

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 ;)

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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.

Posted

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.

  • *Experts*
Posted

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.

Posted

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

Posted

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.?

C#

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...