1

I am trying to display a msgbox and for the user to select yes or no. I am being passed a DataTableMessage where I use the 3 row values in the msgbox. I get the error "The calling thread must be STA, because many UI components require this."

I am using vb.net and WFP. My code where this error is happening is as follows:

 If MsgBox("Message Text Here"
, MsgBoxStyle.YesNo, "Caption Here") = MsgBoxResult.No 

Then 'Do something

            Else
'Do Something
            End If
6dev6il6
  • 715
  • 1
  • 14
  • 33
  • http://stackoverflow.com/questions/2657212/the-calling-thread-must-be-sta-because-many-ui-components-require-this-error – StepUp Feb 15 '16 at 09:42
  • Not for VB.NET @StepUp – 6dev6il6 Feb 15 '16 at 09:45
  • 1
    @StepUp not related. The OP is using VB's MsgBox function, a wrapper over the WinForms message box. – Panagiotis Kanavos Feb 15 '16 at 09:45
  • @ShaunMorehammeredDenovan you are using the wrong method. In WPF, use the WPF [MessageBox class](https://msdn.microsoft.com/en-us/library/ms598711.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2). Also, are you showing the message box from the main thread or another thread, eg inside a callback, or in response to a Thread.Timer event? – Panagiotis Kanavos Feb 15 '16 at 09:46
  • Possible duplicate of [Is there a MessageBox equivalent in WPF?](http://stackoverflow.com/questions/3830228/is-there-a-messagebox-equivalent-in-wpf) – Panagiotis Kanavos Feb 15 '16 at 09:49
  • Please post the entire exception, including the call stack. You can get this with Exception.ToString. This will show us *how* the call to MsgBox was made and where exactly the error was thrown. In any case, the error is caused for either (or both) reasoins: you mixed WPF and Windows Forms code, 2) you tried to display a message box from a background thread – Panagiotis Kanavos Feb 15 '16 at 10:18
  • 1
    MsgBox works just fine, that's why he can see the error message. Don't update UI from a worker thread. – Hans Passant Feb 15 '16 at 10:26

1 Answers1

2

You must be doing showing this MessageBox In a different thread than UI thread.

Use below to do this:

1.

 Application.Current.Dispatcher.Invoke(Sub()

   ' Message Box CODE 

  End Sub)

Use Invoke not BeginInvoke as this is a MessageBox and might be decision factor. A a delay inn visibility might not be affordable. :)

Also this is c# code as I'm a c# dev. But the concept is same in VB.

2. If don't want to use Dispatcher then you can also use(create Thread in UI Thread not in another Thread):

Task.Factory.StartNew(Sub()

                          End Sub,
                          CancellationToken.None,
                          TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext()
                          )

But don't do unnecessary code in the thread. your UI might get blocked.

Kylo Ren
  • 7,647
  • 4
  • 36
  • 57
  • Or using the *wrong* message box class - the OP is using the Windows Forms method. The error message for UI thread violations is different – Panagiotis Kanavos Feb 15 '16 at 09:48
  • No, AFAIK it's the MessageBox.show() that will cause this error. if I do Window.Height=200 from a different thread then I'll get some resource owner type exception. @PanagiotisKanavos – Kylo Ren Feb 15 '16 at 09:51
  • @PanagiotisKanavos And I think cause in WPF only UI thread can generate the UI or a thread that is of same Apartment state. that's why this error displayed. – Kylo Ren Feb 15 '16 at 09:52
  • As I said, the error is *different* in this case. Even then, it's a lot easier to use `async/await` that Invoke. – Panagiotis Kanavos Feb 15 '16 at 09:53
  • 1
    Please post answers in VB.NET when that is the only language tag. This is not accepted the other way round, so the same rule applies here. Lambda syntax is quite different between the two languages and this can confuse people who are new to the language – Matt Wilko Feb 15 '16 at 10:02
  • @PanagiotisKanavos If I use MessageBox class from any thread it will not give any kind of error unlike any other user control. for ex. try of open a Window from a thread. so MessageBox of WPF must have handled that kind of situations. – Kylo Ren Feb 15 '16 at 10:02
  • @PanagiotisKanavos it's the same situation here. User is trying to view a Dialogue from another thread. This is exactly the error. – Kylo Ren Feb 15 '16 at 10:04
  • @KyloRen no it isn't and .NET does throw in cross-thread operations. I think this was added in .NET 2. There's [a similar question](http://stackoverflow.com/questions/2240702/crossthread-operation-not-valid-vb-net). The actual error in this case is "Cross-thread operation not valid" – Panagiotis Kanavos Feb 15 '16 at 10:06
  • @PanagiotisKanavos I think this is when you try to change/access already rendered View from another thread. that's not the situation here. – Kylo Ren Feb 15 '16 at 10:10
  • PS. not my downvote but the cross-thread exception is a very well known exception. *Maybe* the `MsgBox` function does some extra checks and throws a different error than what is expected. – Panagiotis Kanavos Feb 15 '16 at 10:10
  • @PanagiotisKanavos No Problem. Are you a VB dev. Can you edit my answer to VB? – Kylo Ren Feb 15 '16 at 10:11
  • 1
    I'm not a VB dev. All this holds in C# as well. I'm simply waiting for the OP to give more details before answering or closing the answer as duplicate though. There *are* a lot of duplicate questions, and having multiple answers only makes it harder for the next guy that searches for an answer – Panagiotis Kanavos Feb 15 '16 at 10:17
  • @PanagiotisKanavos k. Thanks anyway. – Kylo Ren Feb 15 '16 at 10:20
  • 2
    @MattWilko ok. Done. – Kylo Ren Feb 15 '16 at 10:38
  • Thanks @KyloRen option 1 solved the problem and it works in VB. – 6dev6il6 Feb 17 '16 at 12:35