C
C#17mo ago
Gibbo

✅ Can you lock or freeze a thread while using another form in a new thread??

Part of my application requires i detect a USB device, and grab the DeviceID, I've got this and works fine, how ever im trying to get it to open a new form when it detects that the device is connected. again i got this working. the issue i got is i want it open a form and pass in a serial number from the USB. again i got this working but had to open the form by spawning a new thread. however dispite the form being open as Showdialog(), i can still access the main form. im assuming this is because they are on 2 separate threads. am i able to lock the previous thread while i have the new form open ??? and when it closes unlock it? this is my code for opening the form currently
private void DeviceInsertedEvent(object sender, EventArrivedEventArgs e)
{
// Update Interface to recognise USB inserted
lblUSBDetection.Text = "-- USB Inserted";

string serialNumber;

// Retrieve Serial Number from USB Device
ManagementBaseObject instance = (ManagementBaseObject)e.NewEvent["TargetInstance"];

foreach (var s in instance.Properties)
{
//Output all properties and values to debug console window for testing
//Debug.WriteLine(s.Name + " = " + s.Value);

if (s.Name == "PNPDeviceID")
{
string[] strings = s.Value.ToString().Split('\\');
serialNumber = strings[2];
Debug.WriteLine(serialNumber);

if (serialNumber != null)
{
//TODO if staement to show form if Serial matches serial stored in the data base, for now open form for all USB devices
new Thread(() => new USBPopup(serialNumber).ShowDialog()).Start();
}
break;
}
}
}
private void DeviceInsertedEvent(object sender, EventArrivedEventArgs e)
{
// Update Interface to recognise USB inserted
lblUSBDetection.Text = "-- USB Inserted";

string serialNumber;

// Retrieve Serial Number from USB Device
ManagementBaseObject instance = (ManagementBaseObject)e.NewEvent["TargetInstance"];

foreach (var s in instance.Properties)
{
//Output all properties and values to debug console window for testing
//Debug.WriteLine(s.Name + " = " + s.Value);

if (s.Name == "PNPDeviceID")
{
string[] strings = s.Value.ToString().Split('\\');
serialNumber = strings[2];
Debug.WriteLine(serialNumber);

if (serialNumber != null)
{
//TODO if staement to show form if Serial matches serial stored in the data base, for now open form for all USB devices
new Thread(() => new USBPopup(serialNumber).ShowDialog()).Start();
}
break;
}
}
}
19 Replies
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Gibbo
GibboOP17mo ago
ok ty, il go have a read up on these
phaseshift
phaseshift17mo ago
new Thread(() => new USBPopup(serialNumber).ShowDialog()).Start();
aren't you meant to use dispatcher to run/queue thinhgs on the ui thread? https://learn.microsoft.com/en-us/dotnet/api/system.windows.threading.dispatcher.invoke?view=windowsdesktop-7.0
Dispatcher.Invoke Method (System.Windows.Threading)
Executes the specified delegate synchronously on the thread the Dispatcher is associated with.
Gibbo
GibboOP17mo ago
no idea as I'm a novice, and just learning as i go, I've worked out a way to do this now though with out too much effort
if (InvokeRequired == true)
{
Invoke(new Action(() => { this.Close(); }));
}
else
{
this.Close();
}
if (InvokeRequired == true)
{
Invoke(new Action(() => { this.Close(); }));
}
else
{
this.Close();
}
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Gibbo
GibboOP17mo ago
im using winforms i am having another issue if you can help thow
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Gibbo
GibboOP17mo ago
ive got a usb device but it reports 2 PNPdevice IDs, , i got my code opening and closing a form based on detection and removal but on removal the usb with 2 ids loads a 2nd form been playing for hours no trying to sort it lol
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Gibbo
GibboOP17mo ago
yea this is a unique device has 2 device IDs il get a video asec
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Gibbo
GibboOP17mo ago
wrong veideo
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Gibbo
GibboOP17mo ago
so one usb stick has 2 PNPDeviceID's
Gibbo
GibboOP17mo ago
Availability =
Caption = USB Mass Storage Device
ClassCode =
ConfigManagerErrorCode = 0
ConfigManagerUserConfig = False
CreationClassName = Win32_USBHub
CurrentAlternateSettings =
CurrentConfigValue =
Description = USB Mass Storage Device
DeviceID = USB\VID_16D0&PID_0D71&MI_01\8&1D5C76F4&2&0001
ErrorCleared =
ErrorDescription =
GangSwitched =
InstallDate =
LastErrorCode =
Name = USB Mass Storage Device
NumberOfConfigs =
NumberOfPorts =
PNPDeviceID = USB\VID_16D0&PID_0D71&MI_01\8&1D5C76F4&2&0001
PowerManagementCapabilities =
PowerManagementSupported =
ProtocolCode =
Status = OK
StatusInfo =
SubclassCode =
SystemCreationClassName = Win32_ComputerSystem
SystemName = UK-CH-OR-TS9-L
USBVersion =
Availability =
Caption = USB Composite Device
ClassCode =
ConfigManagerErrorCode = 0
ConfigManagerUserConfig = False
CreationClassName = Win32_USBHub
CurrentAlternateSettings =
CurrentConfigValue =
Description = USB Composite Device
DeviceID = USB\VID_16D0&PID_0D71\00012547
ErrorCleared =
ErrorDescription =
GangSwitched =
InstallDate =
LastErrorCode =
Name = USB Composite Device
NumberOfConfigs =
NumberOfPorts =
PNPDeviceID = USB\VID_16D0&PID_0D71\00012547
PowerManagementCapabilities =
PowerManagementSupported =
ProtocolCode =
Status = OK
StatusInfo =
SubclassCode =
SystemCreationClassName = Win32_ComputerSystem
SystemName = UK-CH-OR-TS9-L
USBVersion =
Availability =
Caption = USB Mass Storage Device
ClassCode =
ConfigManagerErrorCode = 0
ConfigManagerUserConfig = False
CreationClassName = Win32_USBHub
CurrentAlternateSettings =
CurrentConfigValue =
Description = USB Mass Storage Device
DeviceID = USB\VID_16D0&PID_0D71&MI_01\8&1D5C76F4&2&0001
ErrorCleared =
ErrorDescription =
GangSwitched =
InstallDate =
LastErrorCode =
Name = USB Mass Storage Device
NumberOfConfigs =
NumberOfPorts =
PNPDeviceID = USB\VID_16D0&PID_0D71&MI_01\8&1D5C76F4&2&0001
PowerManagementCapabilities =
PowerManagementSupported =
ProtocolCode =
Status = OK
StatusInfo =
SubclassCode =
SystemCreationClassName = Win32_ComputerSystem
SystemName = UK-CH-OR-TS9-L
USBVersion =
Availability =
Caption = USB Composite Device
ClassCode =
ConfigManagerErrorCode = 0
ConfigManagerUserConfig = False
CreationClassName = Win32_USBHub
CurrentAlternateSettings =
CurrentConfigValue =
Description = USB Composite Device
DeviceID = USB\VID_16D0&PID_0D71\00012547
ErrorCleared =
ErrorDescription =
GangSwitched =
InstallDate =
LastErrorCode =
Name = USB Composite Device
NumberOfConfigs =
NumberOfPorts =
PNPDeviceID = USB\VID_16D0&PID_0D71\00012547
PowerManagementCapabilities =
PowerManagementSupported =
ProtocolCode =
Status = OK
StatusInfo =
SubclassCode =
SystemCreationClassName = Win32_ComputerSystem
SystemName = UK-CH-OR-TS9-L
USBVersion =
`
Unknown User
Unknown User17mo ago
Message Not Public
Sign In & Join Server To View
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Gibbo
GibboOP17mo ago
what I'm seeing is correct, as i suspect this this is 2 usbs rolled into one. how ever my code should break out of the loop when it finds the first PNPDeviceID, but it doesn't, i get one prompt which im expecting and is successful, then when i remove the usb a 2nd prompt pop s up for the 2nd pnp device
private void DeviceInsertedEvent(object sender, EventArrivedEventArgs e)
{
// Update Interface to recognise USB inserted and get serial number from the device
//lblUSBDetection.Text = "-- USB Inserted";
string serialNumber = "";
string[] fullDeviceID;
string[] shortDeviceID;

List<string> list = new List<string>();
ManagementBaseObject instance = (ManagementBaseObject)e.NewEvent["TargetInstance"];


if (list.Count != 1)
{
foreach (var s in instance.Properties)
{
if (s.Name == "PNPDeviceID")
{
list.Add(s.Value.ToString());
break;
}
}
}

fullDeviceID = list.ToArray();
shortDeviceID = fullDeviceID[0].Split('\\');
serialNumber = shortDeviceID[2];

if (serialNumber != null)
{
list.Clear();
Debug.WriteLine("Full Device ID: " + fullDeviceID[0] + "\nShort Device ID: " + serialNumber);
Form USBPopup = new USBPopup(serialNumber);
USBPopup.ShowDialog();
}
}
private void DeviceInsertedEvent(object sender, EventArrivedEventArgs e)
{
// Update Interface to recognise USB inserted and get serial number from the device
//lblUSBDetection.Text = "-- USB Inserted";
string serialNumber = "";
string[] fullDeviceID;
string[] shortDeviceID;

List<string> list = new List<string>();
ManagementBaseObject instance = (ManagementBaseObject)e.NewEvent["TargetInstance"];


if (list.Count != 1)
{
foreach (var s in instance.Properties)
{
if (s.Name == "PNPDeviceID")
{
list.Add(s.Value.ToString());
break;
}
}
}

fullDeviceID = list.ToArray();
shortDeviceID = fullDeviceID[0].Split('\\');
serialNumber = shortDeviceID[2];

if (serialNumber != null)
{
list.Clear();
Debug.WriteLine("Full Device ID: " + fullDeviceID[0] + "\nShort Device ID: " + serialNumber);
Form USBPopup = new USBPopup(serialNumber);
USBPopup.ShowDialog();
}
}
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server