0

I have a fully running SMS application which performs a 2 way process of sending and receiving. I am now working on the part of Sending the Received SMS. I will just assure you that I am able to send and receive SMS.

I have a Form named Form1 which has contains 2 textboxes: txtnumber and txtmessage and a button btnSend. When btnSend is Clicked manually the message is sent to the recipients which is indicated in the txtnumber textbox.

I moved the receive functions from my module form to this Form1. When incoming SMS is read through here. It splits the message by the codes below.

'Split parts of the Message Received by "_"
Dim textmess As String() = Message.Split("_")
Dim pass As String = textmess(0)
Dim txt As String = textmess(1)
Dim recipients As String = textmess(2)

All my textboxes and buttons are all hidden. Then after many if's it goes through this line of code. But on the first part I receive an InvalidOperationException error.

Form1.txtnumber.Text = recipients 'Cross-thread Operation not valid:Control 'txtnumber' accessed from a thread other than the thread it was created on.
Form1.txtmessage.Text = txt
Form1.btnSend.PerformClick()

The error message is:

Cross-thread Operation not valid:
Control 'txtnumber' accessed from a thread other than the thread it was created on

Any help would be appreciated.

Ako Ci Divine
  • 69
  • 3
  • 13
  • This question already has an answer: http://stackoverflow.com/questions/2240702/crossthread-operation-not-valid-vb-net – Alex Apr 26 '15 at 16:46
  • @Alexfrom the link you gave me, i just understand that my textboxes should be visible. but even though you gave me a link, i cant understand what should be done to correct this error. – Ako Ci Divine Apr 26 '15 at 17:09

1 Answers1

0

Your problem is exactly what the error message states: you are trying to update the user interface (i.e. textbox controls on your Form1) from a thread, that did not create those controls. That is an illegal operation, you are not allowed to do that.

To solve this, you need to "marshal" these update operations to occur on the thread that created the UI elements, refer to How to: Make Thread-Safe Calls to Windows Forms Controls.

I don't know much about the rest of your project, but one way in which you could do that is as illustrated in the below example. In this example, I am assuming that the form that contains your textboxes is named Form1.

Add a sub procedure to your Form1 that performs the action you want to perform:

Sub UpdateForSmsSending(ByVal recipients As String, ByVal txt As String)
    txtnumber.Text = recipients
    txtmessage.Text = txt
    btnSend.PerformClick()
End Sub

Replace the three lines of code you had (in the block where the exception was thrown) with this:

If Form1.InvokeRequired Then
     ' Need to marshal updates to UI thread
    Form1.Invoke(Sub() Form1.UpdateForSmsSending(recipients, txt))
Else ' No need to marshal to UI thread
    Form1.UpdateForSmsSending(recipients, txt)
End If
Alex
  • 13,024
  • 33
  • 62