Thread Problem
-
I am working on file based Messenger application (using VB.NET) in which i m using a Web Service where all my application functions are written. I have a list of users which i m desplaying in a treeview & refreshing this list every 5 seconds. I call a function from web service to get the latest list of online users. But i want to run this process in background using threads. But i m not able to do this. Code is: ---------------------------------------------------------------------------- Private populate_userlist_thread As Threading.Thread Private Sub RefreshUserList() 'This is my main function, here i m calling another function using thread. populate_userlist_thread = New Threading.Thread(AddressOf PopulateUsers) populate_userlist_thread.Start() End Sub 'Here is the delegate Delegate Sub GeneralDelegate() Private invoker As GeneralDelegate Private Sub PopulateUsers() Dim msngr_serv_obj As MessengerService.MessengerFunctions Dim users_dataset As DataSet Dim i As Int32 Dim curr_node As TreeNode Dim tree_node_collection As TreeNodeCollection msngr_serv_obj = New MessengerService.MessengerFunctions users_dataset = msngr_serv_obj.GetOnlineUsers UsersTreeView.Nodes.Clear() For i = 0 To users_dataset.Tables(0).Rows.Count - 1 If UsersTreeView.InvokeRequired = True Then invoker = New GeneralDelegate(AddressOf RefreshUserList) UsersTreeView.Invoke(invoker) End If curr_node = UsersTreeView.Nodes.Add(users_dataset.Tables(0).Rows(i).Item(0).ToString) curr_node.ForeColor = Color.Green Next End Sub ---------------------------------------------------------------------------- But this code is not working fine. Can anybody plz help me. Or do u have any other solution to this problem. Thanks Sumit Domyan Software Developer ANALEC INFOTECH
-
I am working on file based Messenger application (using VB.NET) in which i m using a Web Service where all my application functions are written. I have a list of users which i m desplaying in a treeview & refreshing this list every 5 seconds. I call a function from web service to get the latest list of online users. But i want to run this process in background using threads. But i m not able to do this. Code is: ---------------------------------------------------------------------------- Private populate_userlist_thread As Threading.Thread Private Sub RefreshUserList() 'This is my main function, here i m calling another function using thread. populate_userlist_thread = New Threading.Thread(AddressOf PopulateUsers) populate_userlist_thread.Start() End Sub 'Here is the delegate Delegate Sub GeneralDelegate() Private invoker As GeneralDelegate Private Sub PopulateUsers() Dim msngr_serv_obj As MessengerService.MessengerFunctions Dim users_dataset As DataSet Dim i As Int32 Dim curr_node As TreeNode Dim tree_node_collection As TreeNodeCollection msngr_serv_obj = New MessengerService.MessengerFunctions users_dataset = msngr_serv_obj.GetOnlineUsers UsersTreeView.Nodes.Clear() For i = 0 To users_dataset.Tables(0).Rows.Count - 1 If UsersTreeView.InvokeRequired = True Then invoker = New GeneralDelegate(AddressOf RefreshUserList) UsersTreeView.Invoke(invoker) End If curr_node = UsersTreeView.Nodes.Add(users_dataset.Tables(0).Rows(i).Item(0).ToString) curr_node.ForeColor = Color.Green Next End Sub ---------------------------------------------------------------------------- But this code is not working fine. Can anybody plz help me. Or do u have any other solution to this problem. Thanks Sumit Domyan Software Developer ANALEC INFOTECH
you are invoking 'RefreshUserList' from within the 'PopulateUsers' method :rolleyes: ? that can't be good... AND you should invoke ALL the GUI related methods by delegates as you did in usersTreeView.invoke(Invoker) otherwise strange things happen. Fade (Amit BS)
-
you are invoking 'RefreshUserList' from within the 'PopulateUsers' method :rolleyes: ? that can't be good... AND you should invoke ALL the GUI related methods by delegates as you did in usersTreeView.invoke(Invoker) otherwise strange things happen. Fade (Amit BS)
Thanks for reply amit. Actually the whole problem is i m calling Refresh userlist function every 5 seconds. should i destroy the previous thread before calling the function or not. Sumit Domyan Software Developer ANALEC INFOTECH
-
Thanks for reply amit. Actually the whole problem is i m calling Refresh userlist function every 5 seconds. should i destroy the previous thread before calling the function or not. Sumit Domyan Software Developer ANALEC INFOTECH
invoker = New GeneralDelegate(AddressOf RefreshUserList) UsersTreeView.Invoke(invoker)
that's a code from thePopulateUsers()
sub... unless i'm missing something this kind of action will create threads until something bad will happen , am i missing something :confused: ? i'll assume that either i am, or you have modified the code to invoke the RefreshUserList sub every 5. now i think that it might be best not the destroy the previously created thread, but to monitor it's existence, possible by using a global or shared boolean member probably named 'blRefreshingUserList', each time you enter your timer event, before you create a new thread check and see whether the previous one has finished. Although i don't see a scenario in which you won't finish refreshing in 5 secs. in 5 secs you can add A LOT or info, i think that something else is wrong, if you wold like to send me the code and tell me what you want it to do, i'll check it and see why it is not working. if not, you may also present more info so i can help you better, your choice (i won't steal your code anyway though) Fade (Amit BS)