473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

asynchronous programming question

Hello,

With asynchronous programming :

Why does the callback-function (running in a worker thread) may not update
the state of a control on the main-form ?

The docs say that you must use a MethodInvoker instead as in following
example :

Private Sub CallBtn_Click(ByVal sender ...) Handles CallBtn.Click

webServiceObj = New MyDatabaseWSClass()|
Dim delCB As New AsyncCallback(AddressOf MyCallBack)
aResult = webServiceObj.BeginConnectToDB(CInt(txtSeconds.Tex t), delCB,
Now)

End Sub

Private Sub UpdateUI()
OutputLabel.Text = "Async: Web service call complete."
End If

Private Sub MyCallBack(ByVal result As IAsyncResult)
If webServiceObj.EndConnectToDB(result) = True Then
DisplayMessage(result)
End If

' following is NOT allowed although it works fine ?????
OutputLabel.Text = "Async: Web service call complete."
Dim mi As New MethodInvoker(AddressOf Me.UpdateUI)
Me.BeginInvoke(mi)
End Sub

Any ideas ?

Thank you

Chris
Nov 20 '05 #1
2 1353
Because most if not all of controls on Windows forms are not thread-safe.
It's component / controls design issue.

HTH
Alex
"Chris" <ch********@pandora.be> wrote in message
news:xp*******************@phobos.telenet-ops.be...
Hello,

With asynchronous programming :

Why does the callback-function (running in a worker thread) may not update
the state of a control on the main-form ?

The docs say that you must use a MethodInvoker instead as in following
example :

Private Sub CallBtn_Click(ByVal sender ...) Handles CallBtn.Click

webServiceObj = New MyDatabaseWSClass()|
Dim delCB As New AsyncCallback(AddressOf MyCallBack)
aResult = webServiceObj.BeginConnectToDB(CInt(txtSeconds.Tex t), delCB,
Now)

End Sub

Private Sub UpdateUI()
OutputLabel.Text = "Async: Web service call complete."
End If

Private Sub MyCallBack(ByVal result As IAsyncResult)
If webServiceObj.EndConnectToDB(result) = True Then
DisplayMessage(result)
End If

' following is NOT allowed although it works fine ?????
OutputLabel.Text = "Async: Web service call complete."
Dim mi As New MethodInvoker(AddressOf Me.UpdateUI)
Me.BeginInvoke(mi)
End Sub

Any ideas ?

Thank you

Chris

Nov 20 '05 #2
"Controls in Windows Forms are bound to a specific thread and are not
thread safe. Therefore, if you are calling a control's method from a
different thread, you must use one of the control's invoke methods to
marshal the call to the proper thread. This property can be used to
determine if you must call an invoke method, which can be useful if you
do not know what thread owns a control. There are four methods on a
control that are safe to call from any thread: Invoke, BeginInvoke,
EndInvoke and CreateGraphics. For all other method calls, you should use
one of these invoke methods when calling from a different thread."
******
instead of
OutputLabel.Text = "Async: Web service call complete."

use

OutputLabel.Invoke(New UpdateLabelDelegate(AddressOf LabelUpdate), New
Object() {OutputLabel})

******

Declare the following:

Delegate Sub UpdateLabelDelegate(ByVal lbl As Label)

******

and add the following sub:

Private Sub UpdateLabel(ByVal lbl As Label)
OutputLabel.Text = "Async: Web service call complete."
End Sub

*************
Chris wrote:
Hello,

With asynchronous programming :

Why does the callback-function (running in a worker thread) may not update
the state of a control on the main-form ?

The docs say that you must use a MethodInvoker instead as in following
example :

Private Sub CallBtn_Click(ByVal sender ...) Handles CallBtn.Click

webServiceObj = New MyDatabaseWSClass()|
Dim delCB As New AsyncCallback(AddressOf MyCallBack)
aResult = webServiceObj.BeginConnectToDB(CInt(txtSeconds.Tex t), delCB,
Now)

End Sub

Private Sub UpdateUI()
OutputLabel.Text = "Async: Web service call complete."
End If

Private Sub MyCallBack(ByVal result As IAsyncResult)
If webServiceObj.EndConnectToDB(result) = True Then
DisplayMessage(result)
End If

' following is NOT allowed although it works fine ?????
OutputLabel.Text = "Async: Web service call complete."
Dim mi As New MethodInvoker(AddressOf Me.UpdateUI)
Me.BeginInvoke(mi)
End Sub

Any ideas ?

Thank you

Chris


Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: ... | last post by:
Does anyone know a good tutorial on asynchronous programming in .net AsyncCallback And IASyncResult are driving me crazy. And the msdn documentation is not really helpful on this topic I...
5
by: a.kostrzewa | last post by:
I have a question about C#. How can I stop asynchronous read/write operation ( BeginReceive() / BeginSend() ) if timeout occurs? The Socket class doesn't make any cancel method available. I used...
4
by: Chris | last post by:
Hello, With asynchronous programming : Why does the callback-function (running in a worker thread) may not update the state of a control on the main-form ? The docs say that you must use a...
5
by: Daniel J Rodriguez | last post by:
Greetings everyone! This is a fairly broard question.. But should be enough to suffice for an answer. Currently we are making an entreprise database server. We will be using C# - my question is...
9
by: Michael Lindsey | last post by:
I need to write a server app to send images to client GUIs that are outside of the server's domain. The client will have the file system path to the image but can not access the file system. I am...
4
by: bernardpace | last post by:
Hi, I am trying to get more familiar with asynchronous programming. I was reading through the document found on the page: ...
1
by: Julian Hershel | last post by:
Reading about asynchronous programming (ms-help://MS.NETFrameworkSDK/cpguidenf/html/cpconasynchronousdesignpatterno verview.htm) I could not clarify some doubts. Hope you can help me. 1) Are...
4
by: Dennis Sjogren | last post by:
Greetings! First, I'm not 100% sure where to post this question. I use VB.NET for this project, but it's really a design question (a question on which method to use when solving this problem). ...
1
by: org | last post by:
Hi, I'm developing a web service with should be used by an .NET CF2 client and an .NET 2.0 Windows client. I've tried to put all the connection logic into one class, which could be used in...
3
by: =?Utf-8?B?bWs=?= | last post by:
Hi everyone, I need to refactor some of our processes using the asynchronous programming model. I have defined a couple of arrays of delegates to pipline the asynchronous invocations through...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.