473,563 Members | 2,658 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(B yVal sender ...) Handles CallBtn.Click

webServiceObj = New MyDatabaseWSCla ss()|
Dim delCB As New AsyncCallback(A ddressOf MyCallBack)
aResult = webServiceObj.B eginConnectToDB (CInt(txtSecond s.Text), delCB,
Now)

End Sub

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

Private Sub MyCallBack(ByVa l result As IAsyncResult)
If webServiceObj.E ndConnectToDB(r esult) = True Then
DisplayMessage( result)
End If

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

Any ideas ?

Thank you

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

HTH
Alex
"Chris" <ch********@pan dora.be> wrote in message
news:xp******** ***********@pho bos.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(B yVal sender ...) Handles CallBtn.Click

webServiceObj = New MyDatabaseWSCla ss()|
Dim delCB As New AsyncCallback(A ddressOf MyCallBack)
aResult = webServiceObj.B eginConnectToDB (CInt(txtSecond s.Text), delCB,
Now)

End Sub

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

Private Sub MyCallBack(ByVa l result As IAsyncResult)
If webServiceObj.E ndConnectToDB(r esult) = True Then
DisplayMessage( result)
End If

' following is NOT allowed although it works fine ?????
OutputLabel.Tex t = "Async: Web service call complete."
Dim mi As New MethodInvoker(A ddressOf 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.Tex t = "Async: Web service call complete."

use

OutputLabel.Inv oke(New UpdateLabelDele gate(AddressOf LabelUpdate), New
Object() {OutputLabel})

******

Declare the following:

Delegate Sub UpdateLabelDele gate(ByVal lbl As Label)

******

and add the following sub:

Private Sub UpdateLabel(ByV al lbl As Label)
OutputLabel.Tex t = "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(B yVal sender ...) Handles CallBtn.Click

webServiceObj = New MyDatabaseWSCla ss()|
Dim delCB As New AsyncCallback(A ddressOf MyCallBack)
aResult = webServiceObj.B eginConnectToDB (CInt(txtSecond s.Text), delCB,
Now)

End Sub

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

Private Sub MyCallBack(ByVa l result As IAsyncResult)
If webServiceObj.E ndConnectToDB(r esult) = True Then
DisplayMessage( result)
End If

' following is NOT allowed although it works fine ?????
OutputLabel.Tex t = "Async: Web service call complete."
Dim mi As New MethodInvoker(A ddressOf 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
3474
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 appreciate any recommendation.
5
5636
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 CancelIo(HANDLE hFile) method (declared in Winbase.h) in C++. Thanks for help. Olek
4
1558
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 MethodInvoker instead as in following example :
5
2081
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 this - is Asynchronous Programming in C# sufficient for production grade quality? If so - does anyone know where to find out how many concurrent...
9
8659
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 trying to decide if I should use remoting vs. writing a server that uses networkstreams. I have read that networkstreams\tcp programming should...
4
348
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: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpovrasynchronousprogrammingoverview.asp Now, I read that 'Always call EndInvoke after your asynchronous call completes'.
1
1543
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 asynchronous programming and multithreaded programming two different pictures? As I read the help topic above it is not clear to me if the design...
4
4385
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). In this medium sized (30 or so forms) application, our users have requested a more visual notification of when the client (this app) is...
1
3163
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 common from the mobile and the windows client. But this didn't work: The new .NET framework supports asynchronous web services with the...
3
2086
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 different stages of execution. However I was wondering if there was any information available regarding the limitations of the asynchronous model, in...
0
7659
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7882
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6244
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5208
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3618
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2079
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.