הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Saturday, June 22, 2013 11:14 AM
Hi,
I am trying to access the members(functions and variable) of one thread to another thread. But I am not able to access it.
Please help me with a sample program.
Regards Syed
All replies (3)
Tuesday, June 25, 2013 5:37 AM ✅Answered | 1 vote
If you would access members in UI thread, you may have to use Invoke method. As for non-UI thread, you could access via this instance.
Here's an example form MSDN library: How to: Make Thread-Safe Calls to Windows Forms Controls.
Regards,
Shanks Zen
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Tuesday, June 25, 2013 10:22 PM ✅Answered
That's not much of an explanation to go on.
Option Strict On
Public Class Form1
' http://tech.xster.net/tips/invoke-ui-changes-across-threads-on-vb-net/
Dim ColorNames As New List(Of String)
Dim Thread1 As New System.Threading.Thread(AddressOf ThreadNumber1)
Dim Thread2 As New System.Threading.Thread(AddressOf ThreadNumber2)
Dim Thread3 As New System.Threading.Thread(AddressOf ThreadNumber3)
Dim i As Integer = 0
Dim Thread2Info As String = ""
Dim Counter As Integer = 0
Dim ColorName As System.Type = GetType(System.Drawing.Color)
Dim ColorPropInfo As System.Reflection.PropertyInfo() = ColorName.GetProperties()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each CPI As System.Reflection.PropertyInfo In ColorPropInfo
If CPI.PropertyType.Name = "Color" And CPI.Name <> "Transparent" Then
ColorNames.Add(CPI.Name)
End If
Next
Me.Text = "Threading test using " & ColorNames.Count.ToString & " system colors"
Thread1.Start()
Thread2.Start()
Thread3.Start()
Timer1.Interval = 50
Timer1.Start()
End Sub
Private Delegate Sub ThreadNumber1Delegate()
Private Sub ThreadNumber1()
i += 1
If InvokeRequired Then
Invoke(New ThreadNumber1Delegate(AddressOf ThreadNumber1))
Else
Label1.Text = "Thread1 is safe and was called " & i.ToString & " times" & vbCrLf & "by Timer1"
Thread2Info = "I am Thread2 and Thread1 has called"
End If
End Sub
Private Delegate Sub ThreadNumber2Delegate()
Private Sub ThreadNumber2()
If InvokeRequired Then
Invoke(New ThreadNumber2Delegate(AddressOf ThreadNumber2))
Else
Label2.Text = Thread2Info
Label2.Text += vbCrLf & "across threads " & i.ToString & " times."
End If
End Sub
Private Delegate Sub ThreadNumber3Delegate()
Private Sub ThreadNumber3()
If InvokeRequired Then
Invoke(New ThreadNumber3Delegate(AddressOf ThreadNumber3))
Else
Label3.Text = "I am Thread3 and I don't like Thread1 or" & vbCrLf & "Thread2 because they have all of the fun."
Dim Rand As New Random
Label3.BackColor = Color.FromName(ColorNames(Rand.Next(0, 140)))
End If
End Sub
Private Sub Form1_FormClosing(sender As Object, e As EventArgs) Handles Me.FormClosing
Thread1.Abort()
Thread2.Abort()
Thread3.Abort()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If Counter = 4 Then Counter = 0
If Counter = 0 Then ThreadNumber1()
If Counter = 1 Then ThreadNumber2()
If Counter = 2 Then ThreadNumber3()
Counter += 1
End Sub
End Class
You've taught me everything I know but not everything you know. _________________________________________________________________________________________________________________ This search engine is for MSDN Library and has many features. http://social.msdn.microsoft.com/Search/en-US?query=search%20msdn%20library&beta=0&ac=8
Wednesday, June 26, 2013 1:42 AM ✅Answered
It is very difficult to provide any kind of generic sample because the actual design and functionality requirements of your program will likely dictate the way in which you implement multithreading. Much depends on the how this object whose members you want to access is designed. Safely accessing an object member across threads is only the first step; you then have to watch out for race conditions or dead locks when you actually use those members on different threads.
I'd suggest reading the link already provided, as well as the references at the end of that article; particularly: http://msdn.microsoft.com/en-us/library/1c9txz50.aspx
If you still require assistance to get started, please describe or provide a short pseudo code example of the process you are trying to achieve.
-EDIT-
You should also probably take a look at the documentation for System.Threading.Tasks.Task. The other links are a bit older; the Task object is the preferred way to implement a background thread (versus using a Thread object).
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"