Event handling

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi all,

I am currently writing an app for a mobile phone. (WM6) with Compact
Framework, though this probably has no bearing on my question.

I am building the app as a layered app. I have my main screen, but behind
this, I have for example, a class that talks to the bluetooth device.

So, for example...

Form1.cs

Here I have...

Bluetooth bt = new Bluetooth();
bt.Connect(); // Asks the bluetooth connection to start and be ready to
handle incoming files.


classes\Bluetooth.cs

public void Connect()
{
// Do my bluetooth actions.
// This also starts a thread.
}

private void BTThreadHandler()
{
// This is the thread handler that was started in connect.
// This saves a file.
}

What I need to do is to allow the BTThreadHandler to notify Form1.cs when a
file has arrived. I actually need to pass the filename. I could store the
filename in a property but if another file comes in, the property will no
doubt change, so a property is not really safe in this instance.


For the event handler, in the past, I have done something like this, but I
don't know if this would be normal... is it?

Form1.cs
bt.EventUpdateData += new EventHandler(CollectDataHandler);

private void CollectDataHandler(object sender, EventArgs e)
{
// handle the event
}


classes\Bluetooth.cs

public event EventHandler EventUpdateData;

private void BTThreadHandler()
{
// This is the thread handler that was started in connect.
// This saves a file.

// Create an event
if (EventUpdateData != null)
EventUpdateData(sender, e); // Note, this is copied, so
sender and e are not valid here.
}


Is this how it is done?

I guess that to pass the filename string, instead of using sender and e, I
just send the string.


Your advice is appreciated. If the above is the wrong way, I would
appreciate the details of how it should be done.

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available


.



Relevant Pages

  • Threading woes
    ... and so I wrote a small test app to make sure ... private void startButton_Click(object sender, EventArgs e) ...
    (microsoft.public.dotnet.framework)
  • Re: How do I intercept a close event?
    ... even when shutting down WindowsXP prof. ... I'm at a loss with how to differentiate between the User closing the app ... > private void MainForm_Closing(object sender, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Serialport issue?
    ... I had trouble with a delay after opening the serial BlueTooth port, ... My simple test program uses one SerialPort class with settings:BaudRate ... private void pbClose_Click(object sender, EventArgs e) ...
    (microsoft.public.dotnet.framework.compactframework)
  • Problems with BackgroundWorker
    ... now i successfully added a BackgroundWorker to my App on a panel with: ... private void bgWorker_DoWork(object sender, DoWorkEventArgs e) ... work a little bit with the app, ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: multiple forms problem
    ... private void button1_Click(object sender, System.EventArgs e) ... Of Course now you have to shutdown your app by yourself. ... could assign a Closed event to Form2 ...
    (microsoft.public.dotnet.framework.windowsforms)