473,385 Members | 1,553 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,385 software developers and data experts.

VB.NET: RasDial + CallBacks + throwing events = frozen UI?

bhc
all-

up until just recently, i was fairly sure i'd implemented a RasDial
class in VB.NET complete with a callback to get updated status. from
this wrapper, when i throw an event back to the calling program, i can
console.writeline the state successfully. but as soon as i replace
that line with updating the UI, the program freezes, and execution
halts. no exceptions are thrown or anything (i've got the line in a
try...catch block, but nothing happens), the app just freezes. in
fact, if i just mouseover the line that causes the problem (in this
case, it's just a simple Me.Text of a form that's calling my object),
the program freezes. what's amusing is that i can successfully
observe and return Me.Left...i imagine it has something to do with the
RasDial occurring in a separate thread...but it's not working
regardless.

any help would be appreciated, thanks in advance.

relevant sample code:
---
'in a class
'The callback function for obtaining updated ras states
Private mRasCallBack As RasDialCallBack = AddressOf RasDialFunc

Public Function Dial(ByVal Parameters As RASDIALPARAMS) As IntPtr
Try
'Declare return value
Dim lReturn As Int32

'Declare handle for RAS dial attempt
Dim lHandle As IntPtr

'Reset flag that controls whether or not we're done dialing
mRasDone = False

'Start dialing
lReturn = modRASAPI.RasDial(IntPtr.Zero, Nothing,
Parameters, 0, mRasCallBack, lHandle)

'See if an error occurred
If lReturn <> 0 Then
'Get the error message
Dim lErrString As String =
modRASAPI.DecodeRASErrorNumber(lReturn)

'Throw the error
Throw New Exception(lErrString)
End If

'Return the handle
Dial = lHandle
Catch ex As Exception
Throw ex
End Try
End Function

Private Function RasDialFunc(ByVal unMsg As Integer, ByVal
rasconnstate As Integer, ByVal dwError As Integer) As Integer
Try
'Get the new state
Dim lState As RasConnState = rasconnstate

'Throw a state changed event
RaiseEvent StateChanged(lState, GetConStateSTR(lState))

'See if we're either connected or disconnected
Select Case lState
Case modRASEnums.RasConnState.Connected
'Raise a done message
ThrowDoneEvent(True, 0, "")

Case modRASEnums.RasConnState.Disconnected
'Raise a done message
ThrowDoneEvent(True, 0, "")
End Select

'See if an error occurred
If dwError <> 0 Then
'First we need to determine what the error is
Dim lErrorMessage As String
Try
'Attempt to decode the error number
lErrorMessage =
modRASAPI.DecodeRASErrorNumber(dwError)
Catch ex As Exception
'Assume the error message, if there was one
lErrorMessage = ex.Message
End Try

'Raise a done message
ThrowDoneEvent(False, dwError, lErrorMessage)
End If

Catch ex As Exception
Throw ex
End Try
End Function
---
'in the calling program

Private Sub mRAS_StateChanged(ByVal CurrentState As
pdsRAS.modRASEnums.RasConnState, ByVal CurrentStateStr As String)
Handles mRAS.StateChanged
Try
'this line works, and i can see the updated state in the
console
Console.WriteLine(CurrentState)
'this line does not work, and the app halts
Me.Text = CurrentState
Catch ex As Exception
Stop
End Try
End Sub

Jun 12 '06 #1
4 3767

bhc wrote:
all-

up until just recently, i was fairly sure i'd implemented a RasDial
class in VB.NET complete with a callback to get updated status. from
this wrapper, when i throw an event back to the calling program, i can
console.writeline the state successfully. but as soon as i replace
that line with updating the UI, the program freezes, and execution
halts. no exceptions are thrown or anything (i've got the line in a
try...catch block, but nothing happens), the app just freezes. in
fact, if i just mouseover the line that causes the problem (in this
case, it's just a simple Me.Text of a form that's calling my object),
the program freezes. what's amusing is that i can successfully
observe and return Me.Left...i imagine it has something to do with the
RasDial occurring in a separate thread...but it's not working
regardless.


More then likely you are right - it is occuring on a separate thread.
You should check the Me.InvokeRequired propertie in the callback - if
it is true, then you need to call Me.Invoke so that the call is
marshaled to the UI thread...

--
Tom Shelton [MVP]

Jun 12 '06 #2
bhc
that's what i was afraid of...perhaps you could expand a little bit on
Invoking, as it's definitely not me strong suit...

what i gather, is that i need to have a Sub that updates the form text,
declare a Delegate that has the same signature, and a variable of the
Delegate type that has an AddressOf pointing to the Sub. what i have
right now....

Private Delegate Sub DelUpdateTextCallBack(ByVal NewText As String)
Private x As DelUpdateTextCallBack = AddressOf UpdateText

Private Sub UpdateText(ByVal NewText As String)
Me.Text = NewText
Application.DoEvents()
End Sub

then in the mRAS_StateChanged above...

If Me.InvokeRequired Then
Me.Invoke(x, New Object() {CurrentState})
End If

i imagine it's not right, as i'm experiencing the same problems already
on the Me.Invoke line, but that was the best i could come up with...

More then likely you are right - it is occuring on a separate thread.
You should check the Me.InvokeRequired propertie in the callback - if
it is true, then you need to call Me.Invoke so that the call is
marshaled to the UI thread...

--
Tom Shelton [MVP]


Jun 13 '06 #3

bhc wrote:
that's what i was afraid of...perhaps you could expand a little bit on
Invoking, as it's definitely not me strong suit...

what i gather, is that i need to have a Sub that updates the form text,
declare a Delegate that has the same signature, and a variable of the
Delegate type that has an AddressOf pointing to the Sub. what i have
right now....

Private Delegate Sub DelUpdateTextCallBack(ByVal NewText As String)
Private x As DelUpdateTextCallBack = AddressOf UpdateText

Private Sub UpdateText(ByVal NewText As String)
Me.Text = NewText
Application.DoEvents()
End Sub

then in the mRAS_StateChanged above...

If Me.InvokeRequired Then
Me.Invoke(x, New Object() {CurrentState})
End If

i imagine it's not right, as i'm experiencing the same problems already
on the Me.Invoke line, but that was the best i could come up with...


bhc - did you ever get this sorted out? I've been sort of out for the
last couple of days... I got your email, and I came back here to check
if any other answers were offered - apparently not. I can try and do a
little bit of playing with this at home tonight, if you still need the
help....

--
Tom Shelton [MVP]

Jun 16 '06 #4
bhc
hi tom - thanks for getting back to me.

turns out this was my fault - although it still doesn't make much sense
to me. here's the deal...in the calling thread, i started dialing,
then entered a loop where i slept 100 miliseconds at a time. when the
event came bubbling up from the RasCallBack, any screen update would
cause the program to freeze. well...all i have to do was throw in a
DoEvents, and it worked. i imagine the screen wasn't able to update
for whatever reason, and the DoEvents was needed to flush out the
request. just a guess - like i said, i really don't understand why it
didn't work to begin with...

Jun 21 '06 #5

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

Similar topics

5
by: Christopher Jastram | last post by:
I'm a self-taught programmer, so this might be a pretty dumb question. If it is, please point me in the right direction and I shall apologize profusely. I have a question regarding C++ and...
3
by: jean | last post by:
I am very new to .Net and Visual Basic in general. My goal is to write a Visual Basic .Net DLL that can have its functions and subroutines called from another .Net application. My DLL will not...
10
by: Alfonso Morra | last post by:
This may be considered as OT since the C++ Standard says not one word about threads. Nevertheless, C++ is routinely and widely used to write solid multithreaded code. I wondered if anyone has...
8
by: Michael McDowell | last post by:
I'm confused: "why do we need to assign a method to a delegate then assign the delegate to an event why not just assign the method to the events event handler" and "how does making a...
8
by: Brian Corbett | last post by:
I'm am trying to write a RAS Component that lists the Available RAS Connections on the current machine, I have got as far as getting the Connections using the RasEnumEntry API Getting the Entry...
6
by: Les Hughes | last post by:
Im working on some RAS stuff, and am after callbacks for when the connection is disconnected. According to http://msdn.microsoft.com/library/default.asp?url=/library/en-us/rras/rras/rasdial.asp...
1
by: bob95226 | last post by:
Hi All, I create a .net compact framework Window CE class library with a few events using .net 2003, the application bundled with this library doesn't work on .net 2005 platform (it works well...
18
by: hq4000 | last post by:
I have a 32-bit application installed on x64 (AMD64) Vista and Windows XP. The rasdial returns 632 - ERROR_INVALID_SIZE on x64 machine; but the rasdial behaves properly installing the same 32-bit...
1
by: Florence Tissot | last post by:
We are seeing some kind of resource leak in our performance lab running an ASP.NET (2.0) application that sends and receives messages from 2 public MSMQ queues. Here's a brief summary of what are...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.