|
If you don't show us any relevant code that you have written, you are just wasting our time and yours. You might as well just google what triggers that exception.
This space for rent
|
|
|
|
|
this is my code
int port = 2195;
String hostname = "gateway.push.apple.com";
try
{
TcpClient client = new TcpClient(hostname, port);
SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
try
{
sslStream.AuthenticateAsClient(hostname, certificatesCollection, System.Security.Authentication.SslProtocols.Tls, false);
for (int i = 0; i < Token.Length; i++)
{
HttpContext.Current.Trace.Warn("inside sendPushNotificationApple, dt=" + Token[i]);
MemoryStream memoryStream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(memoryStream);
writer.Write((byte)0);
writer.Write((byte)0);
writer.Write((byte)32);
writer.Write(HexStringToByteArray(Token[i]));
String payload = "{ \"aps\" : { \"alert\" : {\"title\" : \"" + szAppName + "\",\"body\" : \"" + message + "\",\"Type\" : \"" + szType + "\"} }}";
HttpContext.Current.Trace.Warn("inside sendPushNotificationApple, payload=" + payload.ToString());
writer.Write((byte)0);
byte[] b1 = System.Text.Encoding.UTF8.GetBytes(payload);
writer.Write((byte)b1.Length);
writer.Write(b1);
writer.Flush();
byte[] array = memoryStream.ToArray();
sslStream.Write(array);
sslStream.Flush();
}
client.Close();
}
catch (System.Security.Authentication.AuthenticationException ex)
{
HttpContext.Current.Trace.Warn("inside sendPushNotificationApple, AuthenticationException=" + ex.ToString());
client.Close();
}
catch (Exception exp)
{
HttpContext.Current.Trace.Warn("inside sendPushNotificationApple, Exception=" + exp.ToString());
client.Close();
}
}
catch (Exception ex)
{
HttpContext.Current.Trace.Warn("inside sendPushNotificationApple, Exception1=" + ex.ToString());
}
|
|
|
|
|
You're using a "TcpClient" (Socket), but I do not see you actually "opening" (and testing) the "connection".
i.e. You need to "connect" at some point.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
New to C# and want to know the best way to use the TaskFactory class and how to make sure I am populating my dataGridView rows correctly. Right now, I am only seeing a value in the last dataGridView row. Example: (If I have 3 dataGridView rows I am trying to populate, I only see the output in the 4th unpopulated row.) So, my question is: Am I using TaskFactory correctly and in the most efficient way and how do I make sure I am populating my dataGridView rows correctly. I am using .NET 4.5.51209 and VS 2010
if (dataGridView1.Rows.Count > 1)
{
for (int index = 0; index < dataGridView1.Rows.Count-1; index++)
{
string gridDevice = dataGridView1.Rows[index].Cells[0].Value.ToString();
string service = comboBox1.Text;
WebClient web = new WebClient();
web.UseDefaultCredentials = true;
List<Task> tasks = new List<Task>();
tasks.Add(Task.Factory.StartNew(() =>
{
System.IO.Stream stream = web.OpenRead("http://<some website>");
StreamReader reader = new StreamReader(stream);
String holdHTML = reader.ReadToEnd();
Match mTitle = Regex.Match(holdHTML, ">" + service + "(.*?)<");
if (mTitle.Success)
{
string package = mTitle.Groups[0].Value;
string[] package1 = package.Split('>', '<');
dataGridView1.Rows[index].Cells[5].Value = package1[1];
}
else
{
dataGridView1.Rows[index].Cells[5].Value = "N/A";
}
}));
}
}
|
|
|
|
|
There are a few issues with your code that you need to address. First, you are creating a list of tasks that you end up doing nothing with other than adding a single task. Why not just use that single task instead? Second issue, the StartNew is, essentially, fire and forget so you're calling it and letting the code fall back to the calling method immediately. You have nothing in there that's handling cases where the calling code errors out (for instance); to fix this, add something called a continuation that gets called when your task completes - you can handle your error cases in here for instance. Third, have you tested the core logic of the method inside the task to make sure that adds the data you would expect? Before moving your code into a task, it's generally worth seeing if the code runs successfully and only move it once you are sure it's rock solid.
What version of .NET are you using? It might be worth investigating using async/await and Task.Run if you can.
This space for rent
|
|
|
|
|
Thanks Pete for responding! Yes, my code was working before trying the TaskFactory. I have removed the List of tasks and the code below is working. But, my issue is, I am not sure how to use the task in the for loop. Currently, this is taking about 5 seconds for 3 rows whether I use the Task or not. I am trying to figure out where to place it so I can do all three calls at one time and reduce the time from 5 seconds to 1.75 seconds, which is about what it takes for 1 row.
<pre>
if (dataGridView1.Rows.Count > 1)
{
for (int index3 = 0; index3 < dataGridView1.Rows.Count-1; index3++)
{
string gridDevice = dataGridView1.Rows[index3].Cells[0].Value.ToString();
string service = comboBox1.Text;
WebClient web = new WebClient();
web.UseDefaultCredentials = true;
Task tasks = Task.Factory.StartNew( () =>
{
System.IO.Stream stream = web.OpenRead("http://<website>");
StreamReader reader = new StreamReader(stream);
String holdHTML = reader.ReadToEnd();
Match mTitle = Regex.Match(holdHTML, ">" + service + "(.*?)<");
if (mTitle.Success)
{
string package = mTitle.Groups[0].Value;
string[] package1 = package.Split('>', '<');
dataGridView1.Rows[index3].Cells[5].Value = package1[1];
}
else
{
dataGridView1.Rows[index3].Cells[5].Value = "N/A";
}
});
tasks.Wait();
}
}</pre>
|
|
|
|
|
You should rethink your whole design; too much "overlap".
WebClient supports asynchronous "call backs" (i.e. events); so no need to complicate things with "Tasks".
Since you're "updating the UI" directly, your approach will probably result in "UI thread" conflicts.
"Data Grids" are "views" of an underlying data source; updating grids directly is "hacking".
Better your "web interface" updates an "observeable-type" of collection that the data grid consumes as its data source.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Can anyone help with this? This code is working using Parallel.ForEach - I understand since I am doing I/O, I should be using Async/Await but need some help to incorporate this for my piece of code. I will comment about how/why/what I am doing below within my code. Thanks
// Piece of code to find out if a particular device has a specified piece of software entitled. Will have up to 10 devices in the grid.
// Someone has told me I am not using the Grid in the correct way, so I will eventually change the grid to a table.
if (dataGridView1.Rows.Count > 1)
{
string service = comboBox1.Text;
// Is it best to use a list or IEnumerable here? If IEnumerable, please help!
// populating internal url with device id from the grid, accessing same page,
// with different search parameter for each url
var urls = new List<string> { };
for (int index1 = 0; index1 < dataGridView1.Rows.Count - 1; index1++)
{
string gridDevice = dataGridView1.Rows[index1].Cells[0].Value.ToString();
urls.Add("http://<internal webite>.asp?type=search&OU=" + service + "<internal>&devicename=" + gridDevice + <internal>");
}
// Not sure how to use async/await - or any other suggestions and help would be appreciated.
Parallel.ForEach(urls, (url, state, index) =>
{
using (var client = new WebClient())
{
client.UseDefaultCredentials = true;
var contents = client.DownloadString(url);
// When I put Stopwatch ending here, I am seeing 1st time around 2 seconds, then increasing thru next 9 up to around 5 seconds.
// Total time I see to populate all 10 rows is around 15 seconds. Is it possible to get all 10 rows populated in approximately 3 seconds.
int vOut;
Match mTitle = Regex.Match(contents, ">" + service + "(.*?)<");
if (mTitle.Success)
{
string package = mTitle.Groups[0].Value;
string[] package1 = package.Split('>', '<');
vOut = Convert.ToInt32(index);
dataGridView1.Rows[vOut].Cells[5].Value = package1[1];
}
else
{
vOut = Convert.ToInt32(index);
dataGridView1.Rows[vOut].Cells[5].Value = "N/A";
}
}
});
}
c#
|
|
|
|
|
Hi all,
In my current project it is needed to work with CAN dbc files in order to extract the CAN messages and relevant data.
Is there a standard method for going about this?
So far my searches havent revealed anything helpful..
Thanks for any advice!
|
|
|
|
|
|
Thanks Maciej,
I have seen those but unfortunately they dont help with c#..
I am looking into reading the dbc as a text file now and then searching for the relevant data according to the message.
thanks
|
|
|
|
|
hi guys,
how don't textbox inputs get deleted once going 1 form to another?
I created 2 form. Once I click next button to go 2nd form and click previous button to come back , I see all inputs at textboxes are gone. How can we save inputs at switching forms?
thanks.
modified 13-Feb-19 21:02pm.
|
|
|
|
|
This type of problem usually occurs because the first Form is re-created, and the previous instance of the Form is thus, "lost."
I'll be happy to help you, but, first, you need to make an effort: post your code. Describe what is the "Main form," and where and how the second Form is created. State clearly if this is a Windows Forms project.
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
hello.first of all thank you. as a beginner of c#, I try to make a windows forms application which contain 2 forms and some labels,textboxes,comboboxes. program will make all entries a pdf file at the end. that's pretty much what I want to build.
below is code of form1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
namespace lastcheck
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label4_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void label7_Click(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
}
private void label11_Click(object sender, EventArgs e)
{
}
private void label13_Click(object sender, EventArgs e)
{
}
private void label26_Click(object sender, EventArgs e)
{
}
private void label12_Click(object sender, EventArgs e)
{
}
private void comboBox6_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2sec = new Form2();
form2sec.Show();
Hide();
}
private void button2_Click(object sender, EventArgs e)
{
Close();
}
}
}
and form2 codes:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
namespace lastcheck
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void label17_Click(object sender, EventArgs e)
{
}
private void label4_Click(object sender, EventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
Form1 form1sec = new Form1();
form1sec.Show();
Hide();
}
private void button3_Click(object sender, EventArgs e)
{
Close();
}
}
}
modified 13-Feb-19 21:02pm.
|
|
|
|
|
And there is your problem:
Form 1 does this:
private void button1_Click(object sender, EventArgs e)
{
Form2 form2sec = new Form2();
form2sec.Show();
Hide();
}
And Form2 does this:
private void button5_Click(object sender, EventArgs e)
{
Form1 form1sec = new Form1();
form1sec.Show();
Hide();
}
Which means that each time you click a button, you create a new instance of the form. That's not the same as previous one!
Think of it like this:
You get into your car, and put your phone in the glove box. Then you buy a new car. Would you expect your phone to be in the new car's glove box? Of course not! You know the difference between "this car" and "that car", "old car" and "new car" - and you know that whatever is in the glove box of one car is not in the glove box of any other.
Computer classes work the same way: Each time you use the new keyword you are "buying a new car" - only it's a form instead of a car!
What you need to do probably most easily done like this:
Form1:
private void button1_Click(object sender, EventArgs e)
{
Form2 form2sec = new Form2();
form2sec.ShowDialog();
string valueFromForm2 = form2sec.getTheValueProperty;
} Form2:
private void button5_Click(object sender, EventArgs e)
{
Close();
}
public string getTheValueProperty
{
get { return myTextBox.Text; }
}
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
OriginalGriff wrote: You get into your car, and put your phone in the glove box. Then you buy a new car. Would you expect your phone to be in the new car's glove box? That is wonderful
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
If you can afford a new car, why not buy a new phone as well? with a hands-free kit, and matching colors...
|
|
|
|
|
Because my old one works? Or at least it did until I sold the old car.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
OriginalGriff wrote: You get into your car, and put your phone in the glove box. Then you buy a new car. Would you expect your phone to be in the new car's glove box? Of course I would. If it wasn't, I would expect Jeeves to deliver a damn good thrashing to the salesman for being so remiss. It's all about the service old chap.
This space for rent
|
|
|
|
|
Why would you put your phone in the glove box? Surely that is your chauffeur's part of the automobile?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Good grief, you don't let the chauffeur drive the Jag old bean.
This space for rent
|
|
|
|
|
Hi:
How to define a recursive lambda function using C#?
By example, I was using the following sentence to get the even numbers in a Real group:
double[] doubles = Odd(a, (double x) => x + 2.0);
Any ideas?
Ieshua Carroll
Systems Engineer
|
|
|
|
|
|
It is an useful link. Thanks !
|
|
|
|
|
yw
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|