.NET Components for Mobility

32feet.bt v1.6 + vb.netcf 2005 = Weird Behavior !!!

Last post 08-17-2006 3:31 PM by Andy Hume. 4 replies.
Page 1 of 1 (5 items)
Sort Posts: Previous Next
  • 08-14-2006 12:17 PM

    32feet.bt v1.6 + vb.netcf 2005 = Weird Behavior !!!

    I'm trying to call certain functions from within a bluetooth listening thread that was created via vb.net 2005 for MS compact framework v2.0. Now everytime a file is recieved and a certain function or procedure is called from within the thread, the whole program crashes with the following errors are shown: 1. An error message cannot be displayed because an optional resource assembly containing it cannot be found... or either 2. Use p/invoke instead. This seems to happen when I set the labels' texts on my form to the file name, date & time, and the sender's name. It also occures when I want to control my form either show it or hide it. Even happens when I want to control objects on my form such as showing a picturebox. Why does this happen... Am I doing something wrong ?

    Here's a sample of the code:

    Public Sub DealWithRequest()

    While ol.IsListening = True

    Dim olc As ObexListenerContext

    olc = ol.GetContext

    Dim olr As ObexListenerRequest

    olr = olc.Request

    Dim filename As String

    Dim path As String

    Dim specchar As Char

    specchar = "/"

    filename = ObexUri.UnescapeDataString(olr.RawUrl.TrimStart(specchar))

    path = "\My Documents"

    Dim bep As BluetoothEndPoint = CType(olr.RemoteEndPoint, BluetoothEndPoint)

    Dim bdi As New Sockets.BluetoothDeviceInfo(bep.Address)

    Dim name As String = bdi.DeviceName

    olr.WriteFile(path + "\\" + " " + filename)

    Me.Label2.Text = filename

    Me.Label3.Text = DateTime.Now.ToString("20yy/MM/dd HH:mm:ss")

    Me.Label4.Text = name

    End While

    End Sub

    Everytime it fails, it stops at the "Me.Label2.Text = filename" or anything that involves the form or objects on the form.

    I seriously need your urgent help on this matter. I would highly appreciate your quick responce.

    TANworks™ 2006
  • 08-14-2006 2:23 PM In reply to

    Re: 32feet.bt v1.6 + vb.netcf 2005 = Weird Behavior !!!

    tansoftware:

    ...snipped...

    Everytime it fails, it stops at the "Me.Label2.Text = filename" or anything that involves the form or objects on the form.

    I seriously need your urgent help on this matter. I would highly appreciate your quick responce.


    Assuming that you're running the method on a separate thread...  That isn't allowed! :-)  Window's windows only allow the creating thread to access and control them.  If a different thread tries to modify a window bad things will happen. :-(  I don't think there's anything 32feet.NET specific about your problem.

    I've included a few references below.  In summary there's a couple of methods on Control that help deal with this.  Firstly InvokeRequired returns true if on a different thread for the form, and BeginInvoke and Invoke which cause the given method to be run on the UI thread.  If you weren't using the NETCF, the best solution would be to use the BackgroundWorker component, sadly it appears not to be present in the CF. :-(

    I'd probably create a method that updates your three labels, taking the three strings as arguments, and have it Invoke/BeginInvoke itself as Figure 3 in the BasicInstincts columns shows.

    http://msdn2.microsoft.com/en-us/library/ms171728.asp
    http://msdn.microsoft.com/library/en-us/dnnetcomp/html/BackgroundProcess.asp?frame=true
    http://msdn.microsoft.com/msdnmag/issues/04/05/BasicInstincts/
  • 08-16-2006 11:13 AM In reply to

    Re: 32feet.bt v1.6 + vb.netcf 2005 = Weird Behavior !!!

    Dear Andy,

    I know that I could be asking for too much here, but I'm having a hard time apply the solution you've mentioned above... I could understand how the method works, but I'm facing a huge difficulty applying that solution onto my example code.

    If it's not too much to ask, can you kindly apply the method to my code ? or come up with a much simpler example where I could apply the same concept myself ? I would be highly appreciative of your assistance and help. Thanks again for your time and effort.

    Derek.

    TANworks™ 2006
  • 08-16-2006 8:32 PM In reply to

    Re: 32feet.bt v1.6 + vb.netcf 2005 = Weird Behavior !!!

    I'll help Andy out...  Without the full program I've not been able to test it, but the following should work.  Let me know if it doesn't. :-)

    As to an easier way, I'm not sure of one.  On the full framework, I'd recommend the BackgroundWorker and call ReportProgress(name, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), name) to update the UI, and it would handle the thread switching.  But it's not included in the CF, an odd decision to my mind...

        Public Sub DealWithRequest_WithThreadCompatibleUiUpdating()
            While ol.IsListening = True
                Dim olc As ObexListenerContext
                olc = ol.GetContext
                Dim olr As ObexListenerRequest
                olr = olc.Request
                Dim filename As String
                Dim path As String
                Dim specchar As Char
                specchar = "/"
                filename = ObexUri.UnescapeDataString(olr.RawUrl.TrimStart(specchar))
                path = "\My Documents"
                Dim bep As BluetoothEndPoint = CType(olr.RemoteEndPoint, BluetoothEndPoint)
                Dim bdi As New Sockets.BluetoothDeviceInfo(bep.Address)
                Dim name As String = bdi.DeviceName
                olr.WriteFile(path + "\\" + " " + filename)
                UpdateTheUi(filename, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), name)
            End While
        End Sub
       
        ' This delegate enables asynchronous calls for setting
        ' Control properties on the Form.
        Delegate Sub UpdateTheUiCallback(filename As String, time As String, name As String)
           
        ' This method demonstrates a pattern for making thread-safe
        ' calls on a Windows Forms control.
        Sub UpdateTheUi(filename As String, time As String, name As String)
            If Me.Label2.InvokeRequired Then
                ' We've found that we're attempting to update the Form from
                ' a different thread to the one that created it.  So use
                ' Control's BeginInvoke method to request a callback of ourselves
                ' with the same arguments, but on the UI thread! :-)
                Dim callback As New UpdateTheUiCallback(AddressOf UpdateTheUi)
                Me.BeginInvoke(callback, New Object() { filename, time, name })
                Exit Sub
            End If
            ' Now we're on the UI thread, access the Controls' properties as normal :-)
            Me.Label2.Text = filename
            Me.Label3.Text = time
            Me.Label4.Text = name
        End Sub

    Alan J. McFarlane
    http://www.alanjmcf.me.uk/
    Please follow-up in the newsgroup for the benefit of all.
    Have I helped? Consider visiting my Amazon wishlist, see my homepage.
  • 08-17-2006 3:31 PM In reply to

    Re: 32feet.bt v1.6 + vb.netcf 2005 = Weird Behavior !!!

    Thanks Alan, a fine answer.  Feeling short of questions to answer were we! :-,)

    One could also suggest doing using the GetFolderPath method to generalise the folder location and Path.Combine to do the, err, combining, see the example below.

            Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
            Console.WriteLine(path)
            Dim filename As String = "hugh.txt"
            path = System.IO.Path.Combine(path, filename)
            Console.WriteLine(path)

    Andy
Page 1 of 1 (5 items)
Copyright © 2001-2010 In The Hand Ltd. All rights reserved. Terms of Use and Privacy Policy.