|
I suggest you read the forum guidelines!
|
|
|
|
|
Go here [^]
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997
|
|
|
|
|
priyashankaran wrote: send this project to my id
Sorry this kind of facility is not available here
better to learn C# languages and try to make it by yourself.
|
|
|
|
|
Hi all,
Hope my title explains the all.
I want to control the plug-unplug USB devices programatically, rather manually doing this. Found lots of articles on how to unplug, not other way round. Anybody of you have an idea about it.
Thanks
I appreciate your help all the time...
CodingLover
|
|
|
|
|
I believe that the only way to plug back a device after it has been programatically unplugged is to physically remove the USB device and plug it back again.
|
|
|
|
|
You can use WMI queries to check USB device is plugged or unplugged.
void StartEventWatcher()
{
try
{
WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent");
watcher = new ManagementEventWatcher(query);
Console.WriteLine("Waiting for an event...");
watcher.EventArrived += new EventArrivedEventHandler(HandleDeviceChangeEvent);
// Start listening for events
watcher.Start();
}
catch (ManagementException err)
{
MessageBox.Show("An error occurred while trying to receive an event: " + err.Message);
}
}
private void HandleDeviceChangeEvent(object sender, EventArrivedEventArgs e)
{
IsDeviceConnected()
}
Boolean IsDeviceConnected()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_USBDevice");
foreach (ManagementObject queryObj in searcher.Get())
{
if (queryObj["Description"].ToString() == "Write the name of USB device";)
{
return true;
}
}
return false;
}
WMI is use full , It we can directly write query to OS , to get required information form OS.
Regards,
Somnath Avhad.
|
|
|
|
|
Member 8064748 wrote: WMI is use full , It we can directly write query to OS , to get required information form OS.
True, I can get all the plug-unplug sate. But I cannot unplug a device with WMI.
I appreciate your help all the time...
CodingLover
|
|
|
|
|
Always wrap your code in Pre tag.
|
|
|
|
|
What does a device do when it is programmatically unplugged?
Some devices switch off - how could you plug it programmatically?
Some devices connect in a different mode to receive electricity via the USB port for charging their internal batteries (e.g. connect a "HID keyboard"). In such cases, the SDK of the manufacturer could provide a function to switch the device to its "normal" connected mode.
|
|
|
|
|
|
We have a set (~50) of local mode reports hosted in forms as plugins (dlls) in our app.
They were initially developed in VS2005 and later ported to VS2008.
We are now attempting to get them working in VS2010.
The reports use lists to iterate over a table, invoking a subreport with the ID (primary key) as a variable/parameter so that the subreport could select details from related tables and display them.
Some of the subreports used lists and sub-subreports, on and on.
However, as of VS2010, even once we get the reports building and running they don't work properly.
When we attempt to assign parameters to a subreport in the parent reports' design[er] using
Right-click ->
Subreport Properties ->
Parameters ->
Fields
Then either...
Add to create a new one or
f[unction] to edit one that's broken...
We get "Report item not linked to a dataset."
This happens even if the Field (name) appears in the "Value" combobox and we select it.
Does anyone know how to fix this or get past it...
|
|
|
|
|
hey guys!
does anybody have any experience
in C# ? i would need to read data (numbers) from
*.txt file and plot them in ZedGraph in Csharp
it is easy to make a plot of equestion
but i am suppose to plot bar graph like that:
v1 v2 v3 v4 v5
0122 4466 2847 2640 82800
0122 4466 2847 2640 82801
0122 4466 2847 2640 82802
0122 4466 2847 2640 82803
0122 4468 2847 2640 82804
0122 4468 2847 2640 82805
0122 4466 2847 2640 82806
0122 4466 2847 2640 82807
the task is easy, plot every
column seperately like bar
histogram. i have no idea
how to read data from .txt
i am a beginner and it´s
really important for me.
thx!
|
|
|
|
|
This sounds a lot like homework, so I won't give you full code!
Having said that, it's not a complex task, if you just break it down into the various parts:
1) Read data from text file
2) Convert data to numbers
3) Plot data
It's never that simple, though, because 2 and 3 will have some relationship, because ZedGraph will define what it want's in the form of data.
But the principles are the same.
1) Reading from a text file: easy!
string[] lines = File.ReadAllLines(pathToFile); That's it - all done.
2) Convert to numbers: Not too difficult.
First you need to repeat some action for each line, and convert a line into the various values to be plotted. Since your data appears to be separated by spaces, it would make sense to split teh line up on those and then convert the relevant numbers only.
Set up a foreach loop to work on each line of lines above (the info will be in your notes).
For each line, split iot on a space:
string[] parts = line.Split(' '); This gives you an array of separated values, as strings. It looks like you want to plot the second and fifth values since they are the only ones changing. So the strigns you will need to convert to numbers are parts[1] and parts[4] since C# always starts numbering at zero, not one.
To convert a numeric string to a numeber, use int.Parse:
int value = int.Parse(numericString); For example:
int value = int.Parse("123"); Would give you the number one hundred and twenty-three in the variable value
3) Put the values together, and pass them to ZedGraph.
This bit is up to you - I don't use ZedGraph and don't want to look up for you how it expects it's data - you probably have some notes giving you a short cut on that anyway!
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
i really appreciate
your advice, and
i am going to try
applying you have
just written me.
Thank you very much!
|
|
|
|
|
You're welcome - good luck!
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
Hello!
I have a question regarding an unsafe code blocks in C#. As is well known, the following code:
int[] x = new int[10];
int* ptr;
fixed (int* fix = x)
{
ptr= fix;
}
*(ptr+1) = 123;
Is bad, because ptr is outside the fixed block and the array might be moved by GC. However, is the following code bad too?
int[] x = new int[10];
GCHandle xH = GCHandle.Alloc(x);
int* ptr;
fixed (int* fix = x)
{
ptr= fix;
}
*(ptr+1) = 123;
xH.Free();
If it is wrong, what is an alternative?
Greetings - Jacek
|
|
|
|
|
You should use the overload of Alloc that takes a GCHandleType, and pass GCHandleType.Pinned.
And then using fixed as well is confusing, but there is AddrOfPinnedObject which you can cast to a pointer.
|
|
|
|
|
Thank you!
What I actually wanted to do was to take fixed statements outside a loop. It's because I have discovered, that these statements bring an overhead and, therefore, a performance drop. So, I had an idea to "cache" pinned pointers but I couldn't use a fixed statement when outside a loop. Finally, here is a result:
string[] oldValues,
int oldCount = oldValues.Length;
GCHandle[] oldHandles = new GCHandle[oldCount];
for (int i = 0; i < oldCount; i++)
{
oldHandles[i] = GCHandle.Alloc(oldValues[i], GCHandleType.Pinned);
}
char* oldFix = oldHandles[i].AddrOfPinnedObject();
fixed (char* oldFix = oldValues[i])
This brought about 1.5x performance increase, so it was worth. But still I'm not sure if it is correct (and is hard to detect experimentally due to unpredictability of GC).
Edit: fixed fixed code
Greetings - Jacek
modified 27-Dec-11 13:15pm.
|
|
|
|
|
I don't know, I'm not 100% sure of this, but it looks to be fixing the strings temporarily and not long enough.
Jacek Gajek wrote: but I couldn't use a fixed statement when outside a loop Why is this? I've written plenty of loops spanned by a fixed.
Not that many, granted, but I would say that usually when I use fixed, there is P/Invoke or a loop inside it.
edit: I would only use the GCHandle way to pin if something has to remain pinned for a long time or the pinning must survive returning from the function that pins it in a way that can not be refactored in a sane way (typically these things happen at the same time). Or in the rare case that a GCHandle is required anyway.
modified 27-Dec-11 12:49pm.
|
|
|
|
|
harold aptroot wrote: Why is this? I've written plenty of loops spanned by a fixed.
If you have a single pointer then it is possible. But if you had an array of strings A, then you cannot pull the fixed statement outside the loop since you do not have an indexer:
while(...)
for(int i = 0; i<len; i++)
{
fixed(char* str = A[i])
}
Solution:
GCHandle[] handles= new GCHandle[len];
for (int i = 0; i < len; i++)
{
handles[i] = GCHandle.Alloc(A[i], GCHandleType.Pinned);
}
while(...)
for(int i = 0; i<len; i++)
{
char* str = (char*)handles[i].AddrOfPinnedObject()
}
for (int i = 0; i < len; i++)
{
handles[i].Free();
}
Note: I use memcpy and memcmp inside, so yes there is some P/Invoke shift involved.
Greetings - Jacek
modified 27-Dec-11 13:15pm.
|
|
|
|
|
Oh ok, makes more sense now.
Why is the array pinned too though?
|
|
|
|
|
No special reason. It also caused a fatal exception. Now removed.
Greetings - Jacek
|
|
|
|
|
Sometimes I get "handle is unpinned" exception on AddrOfPinnedObject method call. Any ideas how is it possible? As I see in dotPeek, the exception is thrown because a condition ((int) this.m_handle & 1) != 0; is not satisfied. Since I do not call Free until the method's exit point, I really don't get how was it possible to happen .
Greetings - Jacek
|
|
|
|
|
I don't know, that makes no sense to me.. I would almost say it's a bug in the CLR, but those are rare so I'm probably missing something
|
|
|
|
|
Jacek Gajek wrote: 1.5x performance increase
Compared to what?
And what, exactly, are you trying to accomplish?
|
|
|
|