1

First of all my Main is STAThread and i am not able to change this without facing problems with the rest of my code.

So, I am currently using Rapi2 To pull and push files between my Pda and Computer. Now since there is quite a bit of number crunching i would like to do this on a separate thread. First wat i do is create an RemoteDeviceManager and then make an Event Handler for when a device connects.

public void Initialize()
    {
        _deviceManager = new RemoteDeviceManager();
        _deviceManager.DeviceConnected += DeviceConnected;
    }

As you can see when my device connects it triggers DeviceConnected. This is the class that i end up pulling and pushing a database and do some number work.

 private void DeviceConnected(object sender, RemoteDeviceConnectEventArgs e)
        {
           if (e.Device == null) return;
           ... (unimportant code)
        }

Now the problem here is that i would want to run the code inside DeviceConnected in a new thread but i am unable to access e inside the new thread since it was initialized outside that thread enter image description here

So now wat i tried was make a new thread before calling Initialize.

public Watcher()
    {
        _dataThread = new Thread(Initialize);
        _dataThread.IsBackground = true;
        _dataThread.Name = "Data Thread";
        _dataThread.SetApartmentState(ApartmentState.MTA);
        _dataThread.Start();
    }

But the thread dies and thus never fires my event handler. I tried many different ways to make it work or keep my thread alive but without any success. I hope someone here is able to give me some hints.

Black Lotus
  • 2,377
  • 3
  • 14
  • 18
  • 1
    Extract all the information you need from `e` on the main thread, and marshal *only that information* to the new thread in a threadsafe manner of your choosing. **Treat a thread like a lightweight process**. If you had to do your number crunching by starting a process, how would you send the necessary information to the new process? – Eric Lippert Apr 07 '17 at 13:55
  • @EricLippert Right so i tried to pass e as a parameter. DeviceConnected: _dataThread = new Thread(RunSync) _dataThread.Start(e.Device); e being the device. Then in RunSync i would make a thread but even passing it, and assigning it inside RunSync.After making the Thread it was no longer able to use it. Right so i only check some data between 2 databases. The only thing that requires me to access e is to first pull the file to my pc, and after all the work is done push it back. I tried doing just the crunching on a new thread, but then it would push the file back to soon. – Black Lotus Apr 07 '17 at 15:04
  • What version of .NET are you in? If 4.5 or greater I would use a Task instead which will make this a far less headache. You could also create the thread in the event as an anonymous method and use the e value. However; this doesn't say the e value will remain the same. If the object invoking the event is going to modify e when the handler is complete, since e is a reference type, it will be affected on the thread also and possibly cause a cross threading issue. You may want to copy the data to a variable and work on the copied data in the thread. – Michael Puckett II Apr 08 '17 at 08:00
  • @MichaelPuckettII Sadly enough i am stuck in .net 3.5 though never looked at upgrading the project and if this would cause problems though. Also good idea about passing some stuff over to new variable's i will see if i can get that to work. – Black Lotus Apr 08 '17 at 13:08

0 Answers0