|
Yes. Thanks
|
|
|
|
|
Or, as you have your numbers nicely formatted, you could read them from a file and set in a couple of for loops...
using System.IO;
StreamReader sr = new StreamReader("inputfile.txt");
int[,] Grid = new int[20,20];
for(int i = 0; i < 20; i++)
{
string line = sr.ReadLine();
string[] vals = line.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries);
for(int j = 0; j < 20; j++)
Grid[i, j] = int.Parse(vals[j]);
}
sr.Close();
...A bit messy, but you get the idea. Also you will need to do some testing on the input file to make sure it contains correct data to avoid crash
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
Why don't you want to store them one by one in the array you have defined?
|
|
|
|
|
I'm having an issue determining the size of an array of structures, missingTrans in the code below, that reside within another structure, REGISTER_TOTALS. When I use the MarshalAs attribute shown in the code it sees missingTrans as a 4 byte reference to the array. When I do a, Marshal.SizeOf(typeof(MISSING_TRANS)) , the result is that missingTrans is calculated as being 24 bytes instead of 48. I've tried google and searched numerous message boards but have been unable to come up with anything that works. I would appreciate any help.
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public class MISSING_TRANS
{
public UInt32 FirstTrans;
public UInt32 LastTrans;
public void byteSwap()
{
ISPData.swap4Bytes(ref FirstTrans);
ISPData.swap4Bytes(ref LastTrans);
}
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public class REGISTER_TOTALS
{
public RECORD_ID recID;
public UInt16 RecordVersion;
public UInt16 LocationNum;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 52)]
public byte[] Filler1;
public UInt32 RegRdsTotal;
public UInt32 CalcRdsTotal;
public UInt16 RegRdsCount;
public UInt16 CalcRdsCount;
...
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.Struct,
SizeConst = 6)]
public MISSING_TRANS[] missingTrans;
public UInt16 RegisterForceClosed;
public UInt16 RegisterModel;
...
}
|
|
|
|
|
Hi,
it is not entirely clear to me, however there seem to be some inconsistencies in your post:
1.
why should MISSING_TRANS take either 24 or 48 bytes? all I see is two integers.
2.
why is MISSING_TRANS declared a class, but you are using "ArraySubType = UnmanagedType.Struct" ?
FWIW: I use P/Invoke quite a lot, however I try and keep the data stucture as simple as possible,
which one can do when in charge of both sides. In particular I avoid passing a class, a struct holding other than basic types, and arrays holding other than basic types.
That may take some adjusting, however there are several benefits: it works almost right away, and most of the time the marshaling is free, i.e. no data gets ever copied, it is just passing pointers back and forth.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Hello,
Thanks for the reply. Let me address your questions in order:
1. The reason that I say the size for MISSING_TRANS is 24 bytes is that there are 6 occurences of the array and SizeOf sees MISSING_TRANS as a 4 byte reference instead of an 8 byte structure. What I was hoping with the MarshalAs attribute was to get the data marshalled so that SizeOf would see MISSING_TRANS as 48 bytes, what it should be.
2. Yes MISSING_TRANS is declared as a class but I believe it is really being used as a structure thus allowing me to use the [StructLayout(LayoutKind.Sequential, Pack = 1)] attribute.
I see your point about keeping the structure as simple as possible. I haven't used P/Invoke before and will have to look into it. I was just wondering if anyone had successfully used MarshalAs on an array of structures. Again thanks for the reply.
|
|
|
|
|
Hi,
some comments:
0.
I now see you were referring to the MISSING_TRANS[] array inside REGISTER_TOTALS, not MISSING_TRANS itself.
1.
as I've told you before your MISSING_TRANS was defined as a class, not a struct; that is where you went wrong. Make it a struct, and its data will sit right in the array of fixed size inside REGISTER_TOTALS. As long as MISSING_TRANS is a class all REGISTER_TOTALS can do is hold pointers to it's instances.
2.
I'm not sure whether REGISTER_TOTALS will Marshal fine as that is a class too; consider making it a struct!
3.
FYI: references/pointers would take 4 bytes each on Win32 and 8 on Win64. IntPtr takes care of that, however a lot of P/Invoke examples are plain wrong in this respect.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
That did the trick. I forgot that structures are passed by value and classes by reference. REGISTER_TOTALS does marshal okay. Thanks for all of your help.
|
|
|
|
|
you're welcome.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Hi
Im building a windows application that needs to interact with a database.
For this I created a database worker thread.
I send sql queries to the "database worker thread" and when ready it will put them in a Qeueue that I read from the "Main" thread.
But when I try to for example put the result in a listview I get a "Cross thread Exception".
Why?
If you look at the image below you will understand what Im trying to do.
http://i25.tinypic.com/2saie05.png[^]
Very much thankful for any ideas!
|
|
|
|
|
seems like OnNewResult is running on the thread that caused it to happen, I would guess your BackgroundWorker; hence it can't touch any GUI Controls.
You need to:
- either use Control.InvokeRequired/Control.Invoke
- or let BGW.ReportProgress do the job (it Invokes the GUI thread for you)
While your picture is a work of art, "putting classes inside threads" as it suggests, is misleading;
code does not sit inside a thread, code sits in memory, and one or more threads can/could execute it.
Here enqueueWithEvent runs on something other than the main thread.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
@Luc Pattyn
yeah, isnt the picture great
Unfortuntly I dont have the code avaiible otherwise I would have posted it.
Anyway, as I see it the two Queues are in thread "neutral" lands. So if "Main" thread assigns a Eventlistener to the resultQueue it should be the "Main" thread that gets the event.
bases on the error it must be the Database worker thread that somehow manages to execute the OnnewResult() function!?
|
|
|
|
|
raising an event is just like calling a method, it happens on the thread that executes the code; here the thread that calls enqueueWithEvent, and that is clearly the same one doing the DoWork() stuff, which is bound to be a threadpool thread, as BGWs run on them, not on the main thread.
You can hope and dream all you want, but you have to make it happen. As I said, use either Controle.Invoke or BGW.ReportProgress; for a general-purpose queue, it has to be Control.Invoke
There is one alternative, rather than telling the queue about your Controls, give the queue an event, and let your Form wire a delegate to it, where the delegate itself does the necessary Invoke stuff (after all, to be a proper queue the queue does not have to know about a GUI, all it needs is a way to signal its customers, and that is events+delegates).
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
First off, Im really greatful for your answers!
The thing is that Im not really understanding my own code, im quite a noob when it comes to threading. Thats why i "push" you to explain my design errors.
I will have a look at the "delegates" thing and have another go at this.
Thanks again!
|
|
|
|
|
David8001C wrote: Im not really understanding my own code
That is unacceptable. It is hard enough when you think you do understand your code and it sometimes doesn't do what you expect.
Here is some great advice[^] for you
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Hi David,
I just finished a little article on the cross-thread problem, so here[^] is a read for you. It does assume you are familiar with delegates though. I might create an article on delegates and events at some other time.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Read the documentation.
You'll need to check InvokeRequired and Invoke as needed.
|
|
|
|
|
Hi
Is there any way to copy assmeblies that registered in GAC to local drive ?
thanks
|
|
|
|
|
Yes through the command prompt.
|
|
|
|
|
yes, command prompt or free commander (like windows explorer)
|
|
|
|
|
hdv212 wrote: Is there any way to copy assmeblies that registered in GAC to local drive ?
Lets you want to copy System.Data.dll from GAC [ Just for Example ]
Open Command Prompt.
Move to Directory
C:\WINDOWS\assembly\GAC_32\System.Data\2.0.0.0__b77a5c561934e089>
Use DIR
07/04/2009 04:12 PM <DIR> .
07/04/2009 04:12 PM <DIR> ..
07/04/2009 04:12 PM x,xxx,xxx System.Data.dll
1 File(s) x,xxx,xxx bytes
2 Dir(s) xx,xxx,xxx,xxx bytes free
Now use Copy command to copy the System.Data.Dll
cheers,
Abhijit
CodeProject MVP
Web Site:abhijitjana.net
When you ask a question, remember to click "Good Answer", If the Answer is helps you.
|
|
|
|
|
Hello everyone,
2 days ago i bought a relays card wich is controlled by rs232 ComPort.
It works with the suplied software, that's written in visual basic.
But i code in visual c#, so i downloaded visual basic but i have som problem understanding how to convert sertan things from visual basic to c#.
i will post some code and hope someone could help me out, the problem is in a byte buffer, wich wil sent the commands to my serial port.
Send_buffer(0) = 2 ' Get Ports
Send_buffer(1) = 0 ' Broadcast
Send_buffer(2) = 0
Send_buffer(3) = Send_buffer(0) Xor Send_buffer(1) Xor Send_buffer(2)
SerialPort1.Write(Send_buffer, 0, 4)
i know a little bit what is happening here, but i dont know how to use it in c# thanks so far;)
|
|
|
|
|
Okay, the first four lines are arrays. That mean that you can refer to them like you do in VB, but the brackets need to be square [] instead of curly (). XOR is a logical operator, the equivalent in C# is the ^ symbol. The method call is the same. Don't forget the semicolons at the end of every statement
As to what happens on a high level, you simply fill a small array with numbers and send it over a serial port
Between the idea
And the reality
Between the motion
And the act
Falls the Shadow
|
|
|
|
|
Computafreak wrote: instead of curly ().
Parentheses () aren't curly, braces {} are.
|
|
|
|
|
Ah, my terminology is a bit off. To me, () are curly, [] are square, {} are squiggly. Unsurprisingly, I feel rather silly when I talk about programming syntax
Between the idea
And the reality
Between the motion
And the act
Falls the Shadow
|
|
|
|
|