|
use a debug build, use try-catch blocks, look at each Exception.ToString() that gets thrown, watch for line numbers, increase the observability by adding log statements, that should be sufficient to solve any problem.
Others will tell you to set breakpoints, and/or run in single-step. I'm not saying that.
One more thing: don't write hundreds of lines of code at once, start small and make sure it works perfectly before you expand on it. Once you have several bugs coming together it may be very hard to figure out what is wrong. When you have something that works pretty well, expand on it by adding a dozen lines, then make sure those work fine too. Etc.
|
|
|
|
|
memorexr wrote: initGridView();
This is probably where you are getting the index error.
You will need to post the code for this method.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
My latest tip/trick
Visit the Hindi forum here.
|
|
|
|
|
Abhinav S wrote: This is probably where you are getting the error
No need to guess or gamble.
People should learn to read the information an exception provides. Starting with line numbers (I am assuming a debug build of course).
|
|
|
|
|
hi everyone, What I'm trying to do with the below code is; I want to run the doUpdate() method on a seperate thread in order to keep the responsiveness of the main form. It doesn't work. Could you tell me where I stuck? please
private void Form1_Load(object sender, EventArgs e)
{ Thread thd = new Thread(new ThreadStart(bilgiGuncelle));
thd.Start();
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (InvokeRequired) this.Invoke(new dlgUpdate(doUpdate));
else doUpdate();
}
private void doUpdate()
{
}
|
|
|
|
|
I have not yet read your code and so you may be lucky enough to get an answer anyway but IMO you will be more likely to get help if you edit your question to expand on what 'It doesn't work.' means.
Do you get an exception? Does it give unexpected results? Does the UI become unresponsive?
In what way does it not work?
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Henry Minute wrote: In what way does it not work?
When I call the doUpdate() code from Form's load event or anywhere else, update process is done. When I call the same method within a thread, it doesn't.
|
|
|
|
|
A lot of information is missing to understand what is going on here, such as:
- what timer you use
- what dlgUpdate might be
- what exactly is happening in doUpdate().
If your timer is a System.Windows.Forms.Timer (as your interval comment may suggest), then you should know it always "ticks" on the main thread, so the InvokeRequired line doesn't make sense, and the GUI behavior will be hampered by whatever doUpdate() actually does (such as waiting for a DB response).
If you were to switch to another kind of timer, it would not make sense to have its tick handler do an Invoke the way you did it, as that too would cause everything to run on the main thread. What you probably should do is:
- start a thread that loops a combination of doUpdate and a Thread.Sleep
- have doUpdate gather information and not touch the GUI
- then Invoke a method to update the GUI (that must run on the main thread, see here[^].
[ADDED]
You should have a look at BackgroundWorker, as that is pretty much tailored to what you probably want: put the loop (fetchData+sleep) in DoWork, and use ReportProgress to update the GUI.
[/ADDED]
|
|
|
|
|
hi Luc, once again thanks for understanding me.I've read the article you pointed me to and got what was going on under the hood.Later you added "BackgroundWorker" to your reply and I will research that too.
Regarding this:
Luc Pattyn wrote: - start a thread that loops a combination of doUpdate and a Thread.Sleep
I couldn't figure out starting a thread that loops even though I know how to start a thread.Would you mind giving me a sample?
|
|
|
|
|
just create a little "eternal loop" inside the method that your thread will execute. And probably give it an exit flag, say:
private void threadRunner() {
while(!stopThreadRunner) {
doIt();
Thread.Sleep(3000);
}
}
When you want to terminate, just set the class member stopThreadRunner true.
The advantage of a thread over a timer is you guarantee an idle period in between two activities; when you use a timer, it would launch the activity periodically, even when the previous one hadn't even finished, possibly causing lots of trouble.
With a BackgroundWorker, it would look like this:
private void bgw_DoWork(...) {
while(!stopThreadRunner) {
calculate();
reportProgress(...);
Thread.Sleep(3000);
}
}
|
|
|
|
|
Luc, thanks for the samples. It helped a lot
|
|
|
|
|
You're welcome.
|
|
|
|
|
I would like to write an application that performs a visual diff of 2
DataTables. Let's call them source and destination. There are several
components to this that I would like to solicit opinions on.
I have written something that kind of works but I'm sure there are better
ways to do this. My application is a form that contains 2 DataGridViews, one tied to the source DataTable and the other tied to the destination DataTable.
My end goal is to have records that appear in destination but not in source show with a red background color, records that appear in source but not in destination show with a green background color and rows that have a difference in a field show up yellow (only the changed cell).
The user should then be able to select which source rows then want to propagate to the destination and which rows in the destination that should be deleted so that they match. Those changes will be applied by generating a .SQL file containing the necessary statements.
1) Performing the actual diff of the 2 DataTable objects -> I have read
several posts about table datatable1 and merging it in to datatable2 and
then using the GetChanges method to find out what changed. I never got this to work. Currently what I do is simply compare manually. For each record in the source I do a select against the destination using the primary key and then loop over each field. If the record doesn't appear in the destination, the background color on the source record is green. If the record appears, I color the cells which are different yellow. I then loop over the destination table checking the source table for the associated record. If it's not found I paint the background color of the row red.
My question is there a better way to perform the diff. The datasets that I'll be working with aren't that large but I'm pretty sure this isn't the most efficient way. The end result that I need are the changes in a way that I can generate the SQL to apply the changes to the destination database.
2) The way I'm currently setting the background color of the DGV is using the CellFormatting event in the DGV. Those event are responsible for performing the actual diff described above and setting the background color on the cell. This has several issues:
- I'm doing too many queries. Since this event runs against each cell, I effectively query for the presence of the row in the other DGV for each cell in the row. Not very efficient.
- The CellFormatting event is called on mouse over. I don't want this.
I've tried several different ways to set the background color but the only way that worked was usig the CellFormatting event. Simply trying to set the color doing dgv[0,0].Style.BackColor doesn't appear to do anything. Any suggestion on this is greatly appreciated.
I think I have the right concept just not executing it correctly. Any help is greatly appreciated.
Thanks.
|
|
|
|
|
I don't have the answer, I do have one warning:
setting background color on an empty DGV cell seems to cause havoc when the DGV needs repainting, e.g. when scrolling or changing the sort order. AFAICT empty cells don't get repainted, so the previous content remains visible. I'd call that a bug.
|
|
|
|
|
I did something similar a few weeks back, except comparing text entered in a TextBox to rows in a DGV ( ).
Likewise, I'm setting cell backgrounds to red, green, or yellow as appropriate.
A quick search showed me the CellFormatting event technique, but it didn't perform as required.
Eventually I resorted to not setting the DataSource, but adding the rows one at a time and setting the cell's background color.
Here's the code; the source rows are in a DataTable, the last column contains DataGridViewButtonCells that will perform an Insert, Update, or Delete as appropriate and has the desired background color.
public void
PopulateGrid
(
)
{
foreach
(
System.Data.DataRow src
in
this.TableDef.DataTable.Rows
)
{
System.Windows.Forms.DataGridViewButtonCell action =
(System.Windows.Forms.DataGridViewButtonCell) src [ this.dgvData.Columns.Count - 1 ] ;
System.Windows.Forms.DataGridViewRow dst =
new System.Windows.Forms.DataGridViewRow() ;
for ( int c = 0 ; c < this.dgvData.Columns.Count - 1 ; c++ )
{
System.Windows.Forms.DataGridViewTextBoxCell cell =
new System.Windows.Forms.DataGridViewTextBoxCell() ;
cell.Value = src [ c ] ;
cell.Style.BackColor = action.Style.BackColor ;
dst.Cells.Add ( cell ) ;
}
dst.Cells.Add ( action ) ;
this.AddGridRow ( dst ) ;
}
return ;
}
P.S. If you're wondering about AddGridRow... it's an anonymous method to add the row to the DGV because I'm running on a thread. You are too, right? Right?
this.AddGridRow = delegate
(
System.Windows.Forms.DataGridViewRow Row
)
{
if ( this.InvokeRequired )
{
this.Invoke ( this.AddGridRow , Row ) ;
}
else
{
this.dgvData.Rows.Add ( Row ) ;
}
return ;
} ;
|
|
|
|
|
Does the .NET framework have something equivalent to the Win32 DebugBReak() ?
Is there any way to force a breakpoint in a .NET program?
|
|
|
|
|
System.Diagnostics.Debugger.Break();
or
using System.Diagnostics;
...
if(Debugger.IsAttached)
{
Debugger.Break();
}
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
|
You're welcome!
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
MSDN thread here[^].
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
My latest tip/trick
Visit the Hindi forum here.
|
|
|
|
|
Hi everyone,
I've been struggling with this for a while now and was wondering if anyone could help me out..
I have an RFID reader connected to a serial port, and I am reading the EPC(ID) from it in my C# program using the .NET SerialPorts class.
Reading the tag EPC from my C# program.
string data = serialPort.ReadExisting();
I convert the data to a Char array using data.ToCharArray();
Then using a foreach loop read each letter of the array and add it to a string which comes out like this:
¥I.¦.+ÂX.#Âu.âa¥«ø
On the reader itself it displays the correct hexadecimal value:
E2003411B802011207098357
Can anyone tell me how I can decode the value I am getting in my program this to a hexadecimal value:confused :
The value I am expecting from the reader is a 12 byte value.
I appreciate any help.
Regards,
Chris
|
|
|
|
|
what makes you think the data is actually text? why would you use text-oriented methods?
I suggest you read up on the subject. Read the EPC specification.
And have a look at UHF RFID Reader Program[^]; it shows the E2 starting byte in your hex data is a marker, not a character.
I don't know the details of EPC, it isn't in my area of interest, however your hex data contains hardly any regular character. To me it looks like binary data, not text. So I suggest you keep away from strings, characters, and any SerialPort method that regards data as text. You need SerialPort.Read(byte[]) .
|
|
|
|
|
Hi Luc,
I will check that out thanks
|
|
|
|
|
You're welcome.
|
|
|
|
|
Hei every one,
I want to open cash drawer(cash bases) with programming C#. I've cash drawer [^]
do u have any idea?
Thanks
Syed Shahid Hussain
|
|
|
|
|
There are two options:
1) Contact the manufacturer (Cash bases?)
or
2) Design hardware to locate the key, move it to the correct position, insert it in the lock, turn in the correct direction with the correct degree of force and rotation, and then pull the draw open. Program it in C#
On balance, (1) may be the simpler option...
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|