How to solve"Cross thread operation not valid" error when programming with C#.net
-
Hi im devloping a client server program using C#. What it basically does is the Server can connect 3,4 clients and they can send messags. To do this i didnt use threads. My problem is when client send a message to server it is not receved to the server. Then it indicate a error "Cross thread operation not valid." This is the solution i found form this site. --------------------------------------------------------- Private Strt As System.Threading.Thread ; Strt = New System.Threading.Thread(AddressOf MyThread1) Strt.Start(); Sub MyThread1 ' Working code ' Working code ' Working code ' Working code ' Working code ' Working code AccessControl() End Sub Private Sub AccessControl() If Me.InvokeRequired Then Me.Invoke(New MethodInvoker(AddressOf AccessControl)) Else ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub Button2.Visible = True Button3.Visible = True Opacity = 1 ShowInTaskbar = True End If End Sub --------------------------------------------------------- but the problem is hence i do not use threads do i need to create threads to solve this problem? Im developing the program using C#, so do i need to implement it in a same way of this code? Thank you so much.. sweenySL
-
Hi im devloping a client server program using C#. What it basically does is the Server can connect 3,4 clients and they can send messags. To do this i didnt use threads. My problem is when client send a message to server it is not receved to the server. Then it indicate a error "Cross thread operation not valid." This is the solution i found form this site. --------------------------------------------------------- Private Strt As System.Threading.Thread ; Strt = New System.Threading.Thread(AddressOf MyThread1) Strt.Start(); Sub MyThread1 ' Working code ' Working code ' Working code ' Working code ' Working code ' Working code AccessControl() End Sub Private Sub AccessControl() If Me.InvokeRequired Then Me.Invoke(New MethodInvoker(AddressOf AccessControl)) Else ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub Button2.Visible = True Button3.Visible = True Opacity = 1 ShowInTaskbar = True End If End Sub --------------------------------------------------------- but the problem is hence i do not use threads do i need to create threads to solve this problem? Im developing the program using C#, so do i need to implement it in a same way of this code? Thank you so much.. sweenySL
If you are not using Threads then are you using Asyncronous sockets? It would help a lot if you post your code.
-
Hi im devloping a client server program using C#. What it basically does is the Server can connect 3,4 clients and they can send messags. To do this i didnt use threads. My problem is when client send a message to server it is not receved to the server. Then it indicate a error "Cross thread operation not valid." This is the solution i found form this site. --------------------------------------------------------- Private Strt As System.Threading.Thread ; Strt = New System.Threading.Thread(AddressOf MyThread1) Strt.Start(); Sub MyThread1 ' Working code ' Working code ' Working code ' Working code ' Working code ' Working code AccessControl() End Sub Private Sub AccessControl() If Me.InvokeRequired Then Me.Invoke(New MethodInvoker(AddressOf AccessControl)) Else ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub Button2.Visible = True Button3.Visible = True Opacity = 1 ShowInTaskbar = True End If End Sub --------------------------------------------------------- but the problem is hence i do not use threads do i need to create threads to solve this problem? Im developing the program using C#, so do i need to implement it in a same way of this code? Thank you so much.. sweenySL
Try form1.CheckForIllegalCrossThreadCalls=false;:-D
-
Hi im devloping a client server program using C#. What it basically does is the Server can connect 3,4 clients and they can send messags. To do this i didnt use threads. My problem is when client send a message to server it is not receved to the server. Then it indicate a error "Cross thread operation not valid." This is the solution i found form this site. --------------------------------------------------------- Private Strt As System.Threading.Thread ; Strt = New System.Threading.Thread(AddressOf MyThread1) Strt.Start(); Sub MyThread1 ' Working code ' Working code ' Working code ' Working code ' Working code ' Working code AccessControl() End Sub Private Sub AccessControl() If Me.InvokeRequired Then Me.Invoke(New MethodInvoker(AddressOf AccessControl)) Else ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub ' Code wasn't working in the threading sub Button2.Visible = True Button3.Visible = True Opacity = 1 ShowInTaskbar = True End If End Sub --------------------------------------------------------- but the problem is hence i do not use threads do i need to create threads to solve this problem? Im developing the program using C#, so do i need to implement it in a same way of this code? Thank you so much.. sweenySL
Hi, you create threads when executing asynchronous stuff, e.g. with the timers (except for Windows.Forms.Timer). setting CheckForIllegalCrossThreadCalls false only disables the checking, it does not avoid the problem that may result (UI freeze) immediately or after a while... If InvokeRequired is true, you really need the Invoke (as you did), and then the code you invoke should execute; check it with debugger or with logging ! PS: is this a C# question ? :)
Luc Pattyn [My Articles]