|
For the time being, I am using a timer, storing the data until the timer is elapsed, and resetting the timer if new data comes before it has elapsed.
in DataReceived :
try
{
while (myPort.BytesToRead > 0)
logText += myPort.ReadExisting();
}
catch (Exception ex)
in elapsed timer event handler:
It seems to be operating as expected, with the acceptable artificial latency discussed in comments while it waits for the timer to expire, capturing and storing subsequent data bursts received within the timer interval in a single chunk.
|
|
|
|
|
I call COM object method, it generate exception. If call from VB project or from Excel VBA I get detail exception information (original text exception message from COM object method). But if call from C# project I receive only NullReferenceException. InnerException field is empty. What can do to get detail exception info from c#?
|
|
|
|
|
Member 12632601 wrote: I receive only NullReferenceException There is not much more that the exception handler can tell you. You need to use your debugger to find out which reference item is null, and why.
|
|
|
|
|
My code similar:
dynamic result = comobject.ExecuteQuery();
On this line debug stoped at exception NullReferenceexception.
Known what method ExcecuteQuery throw exception in message contain error description
But i no can get this message in c#
|
|
|
|
|
Use the debugger to examine all the reference items. One of them is null when it should not be.
|
|
|
|
|
ComObject is not null. What in this line can by null?
In VB same line
throw exception TargetInvocationException what have not null InnerException field. What contain detail error message.
|
|
|
|
|
I have no idea because I cannot examine your running program.
|
|
|
|
|
At a guess, some of the information on which the ExecuteQuery request depends.
Use the debugger: look at the ComObject and it's properties - are there any there that are null, that it might be using?
We can't do that for you: we have no idea what yoru object is, what it is talking to, what data it might require - and no way to find out as we can't run your code under the same conditions you do!
So break out the debugger, and start looking at exactly what is going on!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi all, the code below broadcasts on a lan for Logitech media servers listening on UDP port 3483 - it works perfectly unless I run it more than once in the same session which results in the error Only one usage of each socket address is normally permitted. Now I understand that you can't bind to a socket twice with the same ip/port pairing but I'm not trying to do that - I'm simply resending the broadcast. I don't have servers running on the same machine as this code is run on ( that's bitten me before ) Any ideas ?
public async Task<LMSResponseParser> ScanForServer()
{
try
{
this.listener = new UdpClient(UDPPort);
this.ReceiveIP = new IPEndPoint(IPAddress.Broadcast, UDPPort);
this.Result = await listener.ReceiveAsync();
this.RequestData = this.Result.Buffer;
this.ReceiveIP = this.Result.RemoteEndPoint;
this.BytesSent = await this.listener.SendAsync(this.RequestData, this.RequestData.Length, this.ReceiveIP);
this.Result = await this.listener.ReceiveAsync();
this.BytesReceived = this.Result.Buffer;
if (this.BytesReceived != null && this.BytesReceived.Length > 0)
this.RetVal = Encoding.UTF8.GetString(this.BytesReceived);
this.parseLMSResponse = new LMSResponseParser(BytesReceived);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (this.listener != null && this.listener.Client.Connected)
this.listener.Close();
}
return this.parseLMSResponse;
}
"I didn't mention the bats - he'd see them soon enough" - Hunter S Thompson - RIP
modified 5-Oct-20 5:30am.
|
|
|
|
|
The UspClient class implements IDisposable , so you could try wrapping it in a using block.
I'd also be inclined to use local variables for objects which aren't meant to live beyond the current method, rather than storing everything in class-level fields.
public async Task<LMSResponseParser> ScanForServer()
{
using (var listener = new UdpClient(UDPPort))
{
this.ReceiveIP = new IPEndPoint(IPAddress.Broadcast, UDPPort);
var result = await listener.ReceiveAsync();
var requestData = result.Buffer;
this.ReceiveIP = result.RemoteEndPoint;
this.BytesSent = await listener.SendAsync(requestData, requestData.Length, this.ReceiveIP);
result = await listener.ReceiveAsync();
var bytesReceived = result.Buffer;
if (bytesReceived != null && bytesReceived.Length != 0)
{
this.RetVal = Encoding.UTF8.GetString(bytesReceived);
}
return new LMSResponseParser(bytesReceived);
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks Richard I'll give it a shot
"I didn't mention the bats - he'd see them soon enough" - Hunter S Thompson - RIP
|
|
|
|
|
Hi Richard, I tried your code and it worked fine, I think it's the using statement that's key here - I modded my code to call Dispose() on the listener object and that also worked fine so I think you nailed it noticing UDPClient implements IDispose - a lesson learnt - thanks for your help
"I didn't mention the bats - he'd see them soon enough" - Hunter S Thompson - RIP
|
|
|
|
|
Visual studio 2019 16.7.5
I'm getting this warning":
Version mismatch between the analyzer package '3.3.1' and Microsoft.CodeAnalysis '2.1.0.0'
I have the latest version of Microsoft.CodeAnalysis and FxCopAnalyser installed.
I don't understand what causes the system to look at Microsoft.CodeAnalysis '2.1.0.0'.
|
|
|
|
|
|
I have installed MicroCodeAnalysis v3.70 and FxCopAnalyzers v3.3.0. I don't have version 3.3.1
|
|
|
|
|
Member 14924607 wrote: I don't have version 3.3.1
Member 14924607 wrote: Version mismatch between the analyzer package '3.3.1' ...
The error message suggests you do. Unless there's a different analyzer package installed which you haven't mentioned?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi,
I am using the printDocument function to print out my datagridview data. However, I am using the debugger to trace my program execution and I have found out that the "printDocument1_BeginPrint" and "printDocument1_PrintPage" never get executed during the program execution.
Can anyone tell me what I have done wrong? Why the printDocument1_PrinPage and printDoucument1_BeginPrint never got executed?
PS. I am pasting my code below of the printing logic.
Thanks,
John
#region Print Button Click Event
private void btnPrint_Click(object sender, EventArgs e)
{
PrintDialog printDialog = new PrintDialog();
PrintDocument printDocument1 = new PrintDocument();
printDialog.Document = printDocument1;
printDialog.UseEXDialog = true;
if (DialogResult.OK == printDialog.ShowDialog())
{
printDocument1.DocumentName = "Lottery Tickets Priniting";
printDocument1.Print();
}
}
#endregion
#region Begin Print Event Handler
private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
try
{
strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Near;
strFormat.LineAlignment = StringAlignment.Center;
strFormat.Trimming = StringTrimming.EllipsisCharacter;
arrColumnLefts.Clear();
arrColumnWidths.Clear();
iCellHeight = 0;
iRow = 0;
bFirstPage = true;
bNewPage = true;
iTotalWidth = 0;
foreach (DataGridViewColumn dgvGridCol in dataGridView1.Columns)
{
iTotalWidth += dgvGridCol.Width;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion
#region Print Page Event
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
try
{
int iLeftMargin = e.MarginBounds.Left;
int iTopMargin = e.MarginBounds.Top;
bool bMorePagesToPrint = false;
int iTmpWidth = 0;
if (bFirstPage)
{
foreach (DataGridViewColumn GridCol in dataGridView1.Columns)
{
iTmpWidth = (int)(Math.Floor((double)((double)GridCol.Width /
(double)iTotalWidth * (double)iTotalWidth *
((double)e.MarginBounds.Width / (double)iTotalWidth))));
iHeaderHeight = (int)(e.Graphics.MeasureString(GridCol.HeaderText,
GridCol.InheritedStyle.Font, iTmpWidth).Height) + 11;
arrColumnLefts.Add(iLeftMargin);
arrColumnWidths.Add(iTmpWidth);
iLeftMargin += iTmpWidth;
}
}
while (iRow <= dataGridView1.Rows.Count - 1)
{
DataGridViewRow GridRow = dataGridView1.Rows[iRow];
iCellHeight = GridRow.Height + 5;
int iCount = 0;
if (iTopMargin + iCellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
{
bNewPage = true;
bFirstPage = false;
bMorePagesToPrint = true;
break;
}
else
{
if (bNewPage)
{
e.Graphics.DrawString("Lottery Tickets Listing", new Font(dataGridView1.Font, FontStyle.Bold),
Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top -
e.Graphics.MeasureString("Lottery Tickets Listing", new Font(dataGridView1.Font,
FontStyle.Bold), e.MarginBounds.Width).Height - 13);
String strDate = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString();
e.Graphics.DrawString(strDate, new Font(dataGridView1.Font, FontStyle.Bold),
Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width -
e.Graphics.MeasureString(strDate, new Font(dataGridView1.Font,
FontStyle.Bold), e.MarginBounds.Width).Width), e.MarginBounds.Top -
e.Graphics.MeasureString("Lottery Tickets Listing", new Font(new Font(dataGridView1.Font,
FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height - 13);
iTopMargin = e.MarginBounds.Top;
foreach (DataGridViewColumn GridCol in dataGridView1.Columns)
{
e.Graphics.FillRectangle(new SolidBrush(Color.LightGray),
new Rectangle((int)arrColumnLefts[iCount], iTopMargin,
(int)arrColumnWidths[iCount], iHeaderHeight));
e.Graphics.DrawRectangle(Pens.Black,
new Rectangle((int)arrColumnLefts[iCount], iTopMargin,
(int)arrColumnWidths[iCount], iHeaderHeight));
e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font,
new SolidBrush(GridCol.InheritedStyle.ForeColor),
new RectangleF((int)arrColumnLefts[iCount], iTopMargin,
(int)arrColumnWidths[iCount], iHeaderHeight), strFormat);
iCount++;
}
bNewPage = false;
iTopMargin += iHeaderHeight;
}
iCount = 0;
foreach (DataGridViewCell Cel in GridRow.Cells)
{
if (Cel.Value != null)
{
e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font,
new SolidBrush(Cel.InheritedStyle.ForeColor),
new RectangleF((int)arrColumnLefts[iCount], (float)iTopMargin,
(int)arrColumnWidths[iCount], (float)iCellHeight), strFormat);
}
e.Graphics.DrawRectangle(Pens.Black, new Rectangle((int)arrColumnLefts[iCount],
iTopMargin, (int)arrColumnWidths[iCount], iCellHeight));
iCount++;
}
}
iRow++;
iTopMargin += iCellHeight;
}
if (bMorePagesToPrint)
e.HasMorePages = true;
else
e.HasMorePages = false;
}
catch (Exception exc)
{
MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion
modified 2-Oct-20 1:56am.
|
|
|
|
|
You didn't wire up your printing event handlers.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Works great now. Thanks for the help.
|
|
|
|
|
As Gerry has said, you need to add the handlers:
PrintDocument printDocument1 = new PrintDocument();
printDialog.Document = printDocument1;
printDocument1.BeginPrint += printDocument1_BeginPrint;
printDocument1_PrintPage += printDocument1_PrintPage;
But when you post a large chunk of code, please use the code widget to insert the pre tags - it preserves formatting and engages the appropriate syntax highlighter. I added them to your post, but without the you get this:
if (bMorePagesToPrint)
e.HasMorePages = true;
else
e.HasMorePages = false;
With them you get this:
if (bMorePagesToPrint)
e.HasMorePages = true;
else
e.HasMorePages = false; which is a whole load more readable for everyone!
All the tags look like in a post is this:
<pre lang="cs"> if (bMorePagesToPrint)
e.HasMorePages = true;
else
e.HasMorePages = false;</pre>
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Thanks for the tips. I am new to this. Will do it next time when I post codes in here. Thanks again.
|
|
|
|
|
You're welcome!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I want tout compare templates between them stored in my database.
Can you help me please
This is an example:
if (template1 == template2)
{
MessageBox.Show("Fingerprint matched");
}
|
|
|
|
|
What's the template made out of? Ink? A finger print template?
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
A fingerprint template in c#
I want to compare fingerprints between them. The templates are already stored in MySQL database.
streamCompare = new MemoryStream((Byte[])lignemysql["FingerprintRight"]);
TemplateCompare = new DPFP.Template(streamCompare);
ImmatriculationCompare = (String)lignemysql["Immatriculation"];
foreach (DataRow lignemysqlTemp in tablemysqlTemp.Rows)
{
if (lignemysqlTemp["FingerprintRight"] != DBNull.Value)
{
streamTemp = new MemoryStream((Byte[])lignemysqlTemp["FingerprintRight"]);
TemplateTemp = new DPFP.Template(streamTemp);
if (TemplateCompare == TemplateTemp)
{
MessageBox.Show("Fingerprints matched"]);
}
}
}
I want you to help me please
|
|
|
|