Click here to Skip to main content
15,900,572 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: Can someone give me the code of my project? Pin
icemandark26-Sep-07 3:32
icemandark26-Sep-07 3:32 
QuestionModifying textbox control from MTA Pin
cstrader23225-Sep-07 11:18
cstrader23225-Sep-07 11:18 
AnswerRe: Modifying textbox control from MTA Pin
Dave Kreskowiak25-Sep-07 13:14
mveDave Kreskowiak25-Sep-07 13:14 
GeneralRe: Modifying textbox control from MTA Pin
cstrader23225-Sep-07 13:47
cstrader23225-Sep-07 13:47 
GeneralRe: Modifying textbox control from MTA Pin
Dave Kreskowiak25-Sep-07 15:10
mveDave Kreskowiak25-Sep-07 15:10 
GeneralRe: Modifying textbox control from MTA Pin
cstrader23226-Sep-07 0:45
cstrader23226-Sep-07 0:45 
GeneralRe: Modifying textbox control from MTA Pin
Dave Kreskowiak26-Sep-07 1:01
mveDave Kreskowiak26-Sep-07 1:01 
GeneralRe: Modifying textbox control from MTA [modified] Pin
Dave Kreskowiak26-Sep-07 9:05
mveDave Kreskowiak26-Sep-07 9:05 
Well, it does work and without anything "off the wall". This is the test case I put together under the .NET Framework 2.0. The only controls are a Form, TextBox, and Button.
Imports System.Threading
 
Public Class Form1
 
    Private Delegate Sub UpdateTextBoxDelegate(ByVal Message As String)
 
    Private Sub UpdateTextBox(ByVal Message As String)
        TextBox1.AppendText(message & Environment.NewLine)
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles Button1.Click
        For x As Integer = 1 To 20
            Dim t As New Thread(AddressOf GenerateData)
            t.SetApartmentState(ApartmentState.MTA)
            t.Start()
        Next
    End Sub
 
    Private Sub GenerateData()
        Dim callback As New UpdateTextBoxDelegate(AddressOf UpdateTextBox)
 
        For x As Integer = 1 To 10
            TextBox1.Invoke(callback, New Object() { _
                String.Format("Thread Id: {0}  Apartment: {1}  Message: {2}", _
                    Thread.CurrentThread.ManagedThreadId, _
                    Thread.CurrentThread.GetApartmentState().ToString(), _
                    x)})
        Next
    End Sub
End Class


Sorry, I just went back and found out you're using a third party component. The short version of apartments is COM components written in STA mode can only have one thread call their methods at any one time, the thread that created the component. In MTA (sometimes called "free threading", multiple calls to the component's methods from any thread(s) are possible with complete safety.

Since most of Windows Forms and the controls it supplies wrap the existing COM controls, most of which are written in STA mode, the default apartment mode for a Windows Forms app is STA. It's possible to change this, but not a good idea because the COM controls backing Windows Forms requires an STA environment to run.

Calling a method of a control in STA can be done from any apartment mode, so long as the call is marshaled to the thread that created the control. It matters more about the apartment your code is calling, than the apartment the call is comming from. I'd be willing to bet that there's something in this third party component that is not marshalling the call back to STA correctly.


A guide to posting questions on CodeProject[^]

Dave Kreskowiak
Microsoft MVP
Visual Developer - Visual Basic
     2006, 2007


GeneralRe: Modifying textbox control from MTA Pin
cstrader23226-Sep-07 15:42
cstrader23226-Sep-07 15:42 
AnswerRe: Listview Pin
Bso_Cool25-Sep-07 9:03
Bso_Cool25-Sep-07 9:03 
QuestionHow to use Performance Counter? Pin
shyne725-Sep-07 6:04
shyne725-Sep-07 6:04 
AnswerRe: How to use Performance Counter? Pin
Luc Pattyn25-Sep-07 6:46
sitebuilderLuc Pattyn25-Sep-07 6:46 
AnswerRe: How to use Performance Counter? Pin
Dave Kreskowiak25-Sep-07 6:50
mveDave Kreskowiak25-Sep-07 6:50 
AnswerRe: How to use Performance Counter? Pin
Paul Conrad26-Sep-07 18:30
professionalPaul Conrad26-Sep-07 18:30 
QuestionUse a function in CrystalreportViewer.SelectionFormula Pin
dcode2525-Sep-07 5:17
dcode2525-Sep-07 5:17 
AnswerRe: Use a function in CrystalreportViewer.SelectionFormula Pin
Dave Kreskowiak25-Sep-07 5:21
mveDave Kreskowiak25-Sep-07 5:21 
GeneralRe: Use a function in CrystalreportViewer.SelectionFormula Pin
dcode2525-Sep-07 12:09
dcode2525-Sep-07 12:09 
GeneralRe: Use a function in CrystalreportViewer.SelectionFormula Pin
Dave Kreskowiak25-Sep-07 13:02
mveDave Kreskowiak25-Sep-07 13:02 
Questionassociate events to an array (matrix) of controls Pin
diebugger25-Sep-07 4:26
diebugger25-Sep-07 4:26 
AnswerRe: associate events to an array (matrix) of controls Pin
Dave Kreskowiak25-Sep-07 4:33
mveDave Kreskowiak25-Sep-07 4:33 
QuestionRe: associate events to an array (matrix) of controls Pin
diebugger25-Sep-07 5:51
diebugger25-Sep-07 5:51 
AnswerRe: associate events to an array (matrix) of controls Pin
Dave Kreskowiak25-Sep-07 6:45
mveDave Kreskowiak25-Sep-07 6:45 
AnswerRe: associate events to an array (matrix) of controls Pin
Luc Pattyn25-Sep-07 6:48
sitebuilderLuc Pattyn25-Sep-07 6:48 
AnswerRe: associate events to an array (matrix) of controls Pin
diebugger25-Sep-07 23:35
diebugger25-Sep-07 23:35 
AnswerRe: associate events to an array (matrix) of controls Pin
parth.p25-Sep-07 11:36
parth.p25-Sep-07 11:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.