Hi,

Does anyone give me the suggestion about "InvokeRequired"?
I can't find "Controls.InvokeRequired" in vb.net2003. I want to write a simple data updating from "Mulitple Thread", but I would like to check whether "Controls" are requied "Invoke" or not.

Thanks and regards

Normally (with just the main thread) you can access a control's properties directly as in

lblStatus.Text = "online"

however, if you are trying to access the control from another thread you must use a delegate. So what I do is wrap the code in a sub such as

Private Sub UpdateStatus (text As String)

    If lblStatus.InvokeRequired Then
        Dim dlg As New dlgUpdateStatus(AddressOf UpdateStatus)
        Me.Invoke(dlg, text)
    Else
        lblStatus.Text = TimeOfDay & " " & txtAddress.Text & " is " & text
    End If

End Sub

The InvokeRequired method returns True if you are trying to access the control from another thread. In that case you use the delegate which I define at the Class level as

Private Delegate Sub dlgUpdateStatus(text As String)

I do not know if InvokeRequired is available in VB 2003.

Hi Reverend,

Yes, VB2003 doesn't support "InvokeRequired". Is there any method to check whether "Controls" is used by other thread?

Regards

Hi Reverend,

If I don't use any protection and update "Controls" many times from other thread by using "Delegate Method", will it cause other problem like overwrite or overflow?

Regards

I don't know what happens when you try to modify the control from another thread. I'm without my dev system at the moment, again (back in the shop for the second time in two months) so I can't play around with it. Also, because I don't have your version of VB I really can't try anything out here.

Hi Reverend,

In VB2003, system allows to update "UI Controls" from other thread. System doesn't give any error message. Actually, I get a problem "UI Freezing" in my project. It can be seen often and UI will freeze about 3secs. I can't figure out the error.
In system, I use one "MultiThread", "Thread Timer". "MultiThread" will update "UI Controls". When I remove "MultiThread", the system is working well. The following is my threading code.

Private Sub ThreadMonitor()
Do
    UpdateUI()
    Application.DoEvents()
    Thread.Sleep(200)
Loop
End Sub

I suspect updating UI many times can cause "message queue overflows". Do you see any unsual things in my coding?

Regards

I don't, however, I've never used VB 2003 and I am not familiar with UpdateUI. Is that a system call, or is it a call to a Sub that you have written? I'm not a fan of loops like this. It goes against the intent of event driven programming.

Hi Reverend,

Yes, "UpdateUI" is a sub that will update GUI controls. In our application, we need to update some datam every milli second. I suspect that fuction may cause messaging queuing and make UI freeze.
Is it possible to clear message queue?

Regards

I'm not sure why you need to call UpdateUI at all. What do you mean by

we need to update some datam every milli second

even if the UI needs to be refreshed (and I'm not convinced it does), five times a second is overkill.

Hi Reverend,

I know where the problem is. Because "Multiple Threads" and "Main Threads" access same function at the same and then cause the deadlock condition. So, UI is freeze.
Is there a way to make the syncronization between "Threads"? I use 2 threads with different priority. If I use "Thread.Join", thread with low priority will not have a chance to do this task.

Regards

I can't make a blanket statement without seeing the code. And, unfortunately, my main computer (with all the dev tools) is in the shop for a few days. All I have at the moment is a browser on an old laptop. I'd like to try a test with something similar to your code before making a suggestion so unless someone else steps in I hope you can wait a few days.

I can't find "Controls.InvokeRequired" in vb.net2003. I want to write a simple data updating from "Mulitple Thread", but I would like to check whether "Controls" are requied "Invoke" or not.

I can't say for certain as I never worked with Visual Basic .Net 2003 or the 1.1 .Net Framework, but the documentation indicates that the InvokeRequired property did exist in that version.

Control.InvokeRequired Property

Just because .Net 1.1 did not enforce cross-thread checks, does not mean its safe practice.

You mention updating every millisecond. This is not very probable as the paint events alone take longer than that. Also, you have shown a DoEvents (very time consuming and not very safe to throw around haphazardly Click Here) followed by a 250 millisecond thread sleep.

I think you need to get out of panic mode and relax and think about what you need to do and draft a plan of action.

Hi Reverend and TnTinMN,

Thanks for giving me a lot of information. I have already solved the problem. Freezing is not caused by updating. When "Multi Thread" and "Main Thread" access same function at same time, system will not response any more. We can see it in "Task Manager", CPU usage is zero at this situation. So, I have to do a protection for "Function Procedure" or make the synchronization between threads.

Regrads

You could create a class level variable to use as a semaphore. Any thread could use it as follows:

wait until semaphore is available
set semaphore to this thread ID
call shared function
clear semaphore

Hi Reverend,

Thanks for your information.

Regards

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.