HI!
I have a big problem with event handling in VS 2008. I'm writing a GPS program which determine the coordinates, attitude, speed etc. This is a part of a compass program. I'm using the Microsoft's GPS Intermediate Driver. I also use thread to the GPS reading from serial port to not interferre with the UI and the compass because the serial port reading is very slow and stops the compass' spinning for a second at every reading. I was ran through tons of forums and examples about the threads and event handling, but whenever I start the program and the event rises I get this error message:
NotSupportedException
Microsoft.AGL.Common.MISC.HandleArr(PAL_ERROR ar)
System.Windows.Forms.Control.get_Text()
System.Windows.Forms.Control.set_Text(String value)
O2Compass_VB.Form1._Lambda$__1(Object a0, LocationChangeEventArgs a1)
Microsoft.WindowsMobile.Samples.Location.GPS.WaitForGpsEvents()
The code is the next:
Code:
Imports Microsoft.WindowsMobile.Samples.Location
Imports System.Threading
Dim LatRef, LonRef As String
Dim WithEvents GPS As New Gps
'Start GPS readings
GPS.Open()
'Start thread
Dim GPSThread = New Thread(AddressOf GPSDataWriteOut)
GPSThread.Start()
Private Sub GPSDataWriteOut() Handles GPS.LocationChanged
If GPS.GetPosition.Latitude < 0 Then LatRef = "S" Else LatRef = "N"
If GPS.GetPosition.Longitude < 0 Then LonRef = "W" Else LonRef = "E"
If GPS.GetPosition.LatitudeValid Then LatTXT.Text = "Lattitude: " + GPS.GetPosition.Latitude.ToString("#0.0000000") + " " + LatRef
If GPS.GetPosition.LongitudeValid Then LongTXT.Text = "Longitude: " + GPS.GetPosition.Longitude.ToString("#0.0000000") + " " + LonRef
If GPS.GetPosition.SeaLevelAltitudeValid Then AltTXT.Text = "Altitude: " + GPS.GetPosition.SeaLevelAltitude.ToString + " m"
If GPS.GetPosition.SpeedValid Then SpeedTXT.Text = "Speed: " + GPS.GetPosition.Speed.ToString
If (GPS.GetPosition.SatelliteCountValid) And (GPS.GetPosition.SatellitesInViewCountValid) Then SatsTXT.Text = "Sattelites: " + GPS.GetPosition.SatelliteCount.ToString + " / " + GPS.GetPosition.SatellitesInViewCount.ToString
End Sub
And all of this works on the device emulator, but on the phone it's crash. When I'm put the writeouts in a timer it works but then I can't use thread.
Anyone can help me to fix this error? I though the code was right, but always came the same error. What am I doing wrong?
I managed to solve the problem.
The event handling is a different thread, no need to create one to it. And because it's a new working thread, it can't update the label's text of the main thread. So I just pass through the received datas to a variable in the main thread and write it out with a timer if the datas received.
Related
I'm trying to develop a simple application to my Omnia 2 in DirectX in VB.NET, but i stuck, really hard. I want to load a simple texture from a file, but the TextureLoader.FromFile gave me an InvalidCallException error. I don't know what to do, because the code is working on the emulator, but throw this error on the phone. I googled it, a lot, but found nothing.
I hope someone can help me in this.
Here's some code fragment. Please someone help me.
Code:
Function InitializeGraphics() As Boolean
Dim Parameters As PresentParameters = New PresentParameters()
Parameters.Windowed = True
Parameters.SwapEffect = SwapEffect.Discard
DeviceForm = New Device(0, DeviceType.Default, Me, CreateFlags.None, Parameters)
Me.OnCreateDevice(DeviceForm, Nothing)
Return True
End Function
Private Sub OnCreateDevice(ByVal sender As Object, _
ByVal e As EventArgs)
Dim dev As Device = CType(sender, Device)
spriteTexture = TextureLoader.FromFile(dev, "\Storage card\Picture.png")
Using s As Surface = spriteTexture.GetSurfaceLevel(0)
Dim desc As SurfaceDescription = s.Description
textureSize = New Rectangle(0, 0, desc.Width, desc.Height)
End Using
sprite = New Sprite(DeviceForm)
End Sub
Here's some picture from the exceptions details.
Nevermind. I solved it.
Lack of time has not to schedule time for WM. Right now I have begun to make an application in WM 5.0 but I have the following problem. I intend to retrieve a list of system services to fail to load the phone by the usb. For that I have something like this:
Imports System.ServiceProcess
[...]
Public Function ActivarCargaUSB(ByVal param As Integer) As Integer
Dim services() As ServiceController
services = ServiceController.GetServices()
End Function
But Visual Studio tells me that: We need a reference to assembly 'System, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089' containing the base class 'System.ComponentModel.Component'. Add one to your project.
Someone tells me how I can add that library? or some method to stop charging the battery by usb?
thanks
please help! As ServiceController declare the type correctly?
I need help please ...
amocu said:
Lack of time has not to schedule time for WM. Right now I have begun to make an application in WM 5.0 but I have the following problem. I intend to retrieve a list of system services to fail to load the phone by the usb. For that I have something like this:
Imports System.ServiceProcess
[...]
Public Function ActivarCargaUSB(ByVal param As Integer) As Integer
Dim services() As ServiceController
services = ServiceController.GetServices()
End Function
But Visual Studio tells me that: We need a reference to assembly 'System, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089' containing the base class 'System.ComponentModel.Component'. Add one to your project.
Someone tells me how I can add that library? or some method to stop charging the battery by usb?
thanks
Click to expand...
Click to collapse
You can use registry key for control battery charge usb. If you need i post example code.
Hi there,
Does anyone out there how to preserve/restore the transient state of a CheckBox and/or Radio button?
So far, I'm using the following code, working for textbox
Code:
Public Sub PreserveState_TextBox(ByVal TB As TextBox)
Dim buffer As String = String.Empty
If True = PhoneApplicationService.Current.State.ContainsKey(TB.Name) Then
buffer = TryCast(PhoneApplicationService.Current.State(TB.Name), String)
If Not String.IsNullOrEmpty(buffer) Then
TB.Text = buffer
End If
End If
End Sub
Public Sub RestoreState_TextBox(ByVal TB As TextBox)
If True = PhoneApplicationService.Current.State.ContainsKey(TB.Name) Then
PhoneApplicationService.Current.State.Remove(TB.Name)
End If
PhoneApplicationService.Current.State.Add(TB.Name, TB.Text)
End Sub
Is it possible to modify the above code to work for Checkbox and/or Radiobutton?
If not, any ideas?
So far, I've been trying the sample "Tombstoning" sample code from Microsoft without any luck...
Thanks in advance!
I have a little problem with converting a binary or hex (it's hard to tell which but it's listed as reg_binary type) registry value to a string so i can output it to a file. That's about it really other than giving you the code i developed. It generates an InvalidCastException Error.
Code:
[SIZE=2][COLOR=#0000ff]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Alarm1Days [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Byte[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = Registry.GetValue([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"HKEY_LOCAL_MACHINE\Software\Microsoft\Clock\0"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"AlarmDays"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], (0))[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sw [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] IO.StreamWriter = IO.File.CreateText(DirPath1)[/SIZE]
[SIZE=2]sw.WriteLine(RegHead)[/SIZE]
[SIZE=2]sw.WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"[HKEY_LOCAL_MACHINE\Software\Microsoft\Clock\0]"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]sw.WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"""AlarmDays""=Hex:"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + Alarm1Days.ToString.PadLeft([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"0"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]CChar[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]CStr[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](8))))[/SIZE]
[SIZE=2][SIZE=2]sw.Flush()[/SIZE][/SIZE]
[SIZE=2]sw.Close()[/SIZE]
I'd be very grateful if you guys would help with this. It's given me a nasty headache.
Hey M3PH,
Maybe this is not what you wanted but the below functions could be useful. I remember that I've used them in converting binary<->hex values in the past in one of my PC apps.
Code:
#Region "Decode/Encode Functions"
Function DecodeStr(ByVal str As String) As String
Dim dec_temp As String
Dim StrBuilder As New StringBuilder()
Dim bt64 As Byte() = Convert.FromBase64String(str)
For i = 0 To UBound(bt64) - 1
dec_temp = Convert.ToString(CInt(bt64(i)), 16)
If dec_temp.Length = 1 Then
dec_temp = 0 & dec_temp
End If
StrBuilder.Append(dec_temp & ",")
Next
dec_temp = Convert.ToString(CInt(bt64(UBound(bt64))), 16)
If dec_temp.Length = 1 Then
dec_temp = 0 & dec_temp
End If
Return StrBuilder.ToString & dec_temp
End Function
Function EncodeStr(ByVal str As String) As String
Dim enc_temp As String
Dim bt64 As Byte() = System.Text.Encoding.UTF8.GetBytes(str)
enc_temp = Convert.ToBase64String(bt64)
Return enc_temp
End Function
#End Region
Code:
'Using DecodeStr function in your example...
[SIZE=2][COLOR=#0000ff]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Alarm1Days [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE] [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = Registry.GetValue([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"HKEY_LOCAL_MACHINE\Software\Microsoft\Clock\0"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"AlarmDays"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], (0))[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sw [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] IO.StreamWriter = IO.File.CreateText(DirPath1)[/SIZE]
[SIZE=2]sw.WriteLine(RegHead)[/SIZE]
[SIZE=2]sw.WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"[HKEY_LOCAL_MACHINE\Software\Microsoft\Clock\0]"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]sw.WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"""AlarmDays""=Hex:"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] & DecodeStr(Alarm1Days))[/SIZE]
[SIZE=2][SIZE=2]sw.Flush()[/SIZE][/SIZE]
[SIZE=2]sw.Close()[/SIZE]
Regards!
When calling the Registry.Getvalue() method against binary values, it returns an Array of 'Byte' values. In this case, the value for 'AlarmDays' is only one byte long, but it is actually a one element 'Byte' array. The compiler cannot implicitly convert from 'Byte()' to 'Byte' so it throws the invalid cast error.
Here's the fixed code:
Code:
Dim Alarm1Days As Byte() = Registry.GetValue("HKEY_LOCAL_MACHINE\Software\Microsoft\Clock\0", "AlarmDays", (0))
Dim sw As IO.StreamWriter = IO.File.CreateText(DirPath1)
sw.WriteLine(RegHead)
sw.WriteLine("[HKEY_LOCAL_MACHINE\Software\Microsoft\Clock\0]")
sw.Write("""AlarmDays""=Hex:")
For Each ByteValue As Byte In Alarm1Days
sw.Write(ByteValue.ToString("X2"))
Next
sw.WriteLine()
sw.Flush()
sw.Close()
The For Each - Next code section is able to deal with any length of binary value returned. In the case of a one byte value above, you could actually get away with:
Code:
sw.Write(Alarm1Days(0).ToString("X2"))
The For Each method is safer.
thanks steph. i'm on holiday atm so i'll try it out when i get home and let you know
So i tired out your code, steph and it works pretty well but there is one thing i'm having trouble with at the moment. The for each statement seems to never end the line so you end up with one really long single line instead of lots of short ones. In an attempt to correct this i did the following:
Code:
[SIZE=2]
sw.WriteLine(RegHead)
sw.WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"[HKEY_LOCAL_MACHINE\Software\Microsoft\Clock\0]"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])
sw.WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"""AlarmDays""=Hex:"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ByteValue [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Byte[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Alarm1Days
sw.Write(ByteValue.ToString([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"X2"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]))
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]sw.Write(vbCrLf)
[/SIZE]
But that makes the for each output appear on a new line on it's own and this also is not what i need. I need the value name and then the data one the same line in the output file. Any ideas on how to achieve this?
WriteLine() automatically adds a carriage return & line feed pair to the end of whatever input it is given.
Write() doesn't.
Change
sw.WriteLine("""AlarmDays""=Hex:")
to
sw.Write("""AlarmDays""=Hex:")
P.S. WriteLine() and Write(vbCrLf) are identical.
stephj said:
WriteLine() automatically adds a carriage return & line feed pair to the end of whatever input it is given.
Write() doesn't.
Change
sw.WriteLine("""AlarmDays""=Hex:")
to
sw.Write("""AlarmDays""=Hex:")
P.S. WriteLine() and Write(vbCrLf) are identical.
Click to expand...
Click to collapse
I should have noticed that. Thanks for giving me a poke
Hi, i want to read the signal stretch value; i have this code but it doesen't works. Any idea?
Dim regKey As RegistryKey = Registry.LocalMachine
regKey = regKey.OpenSubKey("HKLM\System\State\Phone\Signal Strength")
Dim val As Object = regKey.GetValue("")
MsgBox(val.ToString)
Thaanks!
A key generated using Registry.LocalMachine already has the 'HKLM' root, implied within it. You do not need to respecify it.
Try this, and while we are at it, let's do what the optimiser would do, during a 'release' build of the above code:
Code:
Dim regKey As RegistryKey = Registry.LocalMachine.OpenSubKey("System\State\Phone")
MessageBox.Show(regKey.GetValue("Signal Strength").ToString())
If you don't need to save the value returned for later use, and just want to output the value in the message box, this actually works!
Code:
MessageBox.Show(Registry.LocalMachine.OpenSubKey("System\State\Phone").GetValue("Signal Strength").ToString())
One line of code to bring them all, and in the darkness bind them!
Note that there is no error checking at all, in the above examples.