I need help with my program. So I have com port connection ( gps device ) and when the program starts I want to be able to show Form2 until gps eventhandler gets triggerd.
And same thing when device starts to send me data, there is some time when device is not available to give me data. How can I know in that time that I need to show again Form2?
This is a bit complicated to explain.
Here is what I have done so far. So when Form1 loads I am showing a Form2 screen which basically says that I am waiting for signal to shown up.
Now how can I put this in some sort of loop or something like that which will be always checking for eventhandler to be triggerd and in mean time I am showing this form2.
If you need more explanation let me know.
So here is the code:
private void Form1_Load(object sender, EventArgs e)
{
//open the com port when loading form
comport.Open();
Form2 my_form2 = new Form2();
my_form2.ShowDialog();
}
This is the event handler for GPS signal:
GPS.PositionReceived += new NmeaInterpreter.PositionReceivedEventHandler(GPS_PositionReceived);
And this is GPS function:
private void GPS_PositionReceived(string Lat, string Lon)
{
arrLon = Lon.Split(new char[] { '°', '"' }, StringSplitOptions.RemoveEmptyEntries);
dblLon = double.Parse(arrLon[0]) + double.Parse(arrLon[1], new System.Globalization.NumberFormatInfo()) / 60;
deciLon = arrLon[2] == "E" ? dblLon : -dblLon;
//some more code
}
So why not just close the form when the event handler triggers? that is their whole purpose.
They run code when something triggers.
Having an indefinate loop that waits for a state change is a bad idea.
private void GPS_PositionReceived(string Lat, string Lon)
{
arrLon = Lon.Split(new char[] { '°', '"' }, StringSplitOptions.RemoveEmptyEntries);
dblLon = double.Parse(arrLon[0]) + double.Parse(arrLon[1], new System.Globalization.NumberFormatInfo()) / 60;
deciLon = arrLon[2] == "E" ? dblLon : -dblLon;
//some more code
// LOAD FORM 1
// CLOSE THIS FORM (FORM 2)
}
Related
My first question to this forum, so please be kind :D
I'm building a desktop application in C# that should read data from a Raspberry Pi and show it (values, charts...). The MainForm.cs has a menu strip where I can call a child form, ConnectionSetupForm.cs where I can enter the database connection values (db user, password, table, host, port, etc.) which are stored as Properties.Settings.Default.xxx (System.Configuration).
When I'm closing this child form with a save button, I want the MainForm to get this information and refresh. This works, but only after I restart the application, but it should be when the FormClosing or FormClosed event of the child form happens (in my example the MessageBox.Show() never occurs).
What am I missing? I have no errors and no warnings.
MainForm calls the child form ConnectionSetupForm:
private void showConnectionForm(object sender)
{
connectionSetupForm = new ConnectionSetupForm();
connectionSetupForm.MdiParent = this.ParentForm;
connectionSetupForm.StartPosition = FormStartPosition.CenterParent;
connectionSetupForm.ShowDialog();
connectionSetupForm.FormClosed += new FormClosedEventHandler(ConnectionForm_FormClosed);
}
private void ConnectionForm_FormClosed(object sender, FormClosedEventArgs e)
{
Properties.Settings.Default.Save();
Properties.Settings.Default.Reload();
MessageBox.Show("Closed"); // is never called, so this method is never called...
// actually, I want to do some stuff here, e.g.:
this.Text = Properties.Settings.Default.ApplicationName; // name is changed after restart only
connectionSetupForm = null;
}
This looks to be a simple mis-ordering of a couple lines. The connectionSetupForm.ShowDialog(); line is actually a call to a blocking function. This means nothing past this line will execute until AFTER your dialog closes. Because you add the FormClosed event handler after the form has already closed, the ConnectionForm_FormClosed() function never gets called.
Try simply putting the FormClosed event handler before the connectionSetupForm.ShowDialog(); line.
private void showConnectionForm(object sender)
{
connectionSetupForm = new ConnectionSetupForm();
connectionSetupForm.MdiParent = this.ParentForm;
connectionSetupForm.StartPosition = FormStartPosition.CenterParent;
connectionSetupForm.FormClosed += new FormClosedEventHandler(ConnectionForm_FormClosed);
connectionSetupForm.ShowDialog();
}
Hello, I've got problem to write simple string to TextBox and ListBox. I do not know what is wrong.
After click on button3 is in class Listen running method to open communication and receiving packets. In this method (Listen.StartListen) is reference to PrintReceivedPackets. Is it mistake in Task part?
Is it better to use Thread instead of Task?
2.
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private Listen lis, start;
public Form1()
{
InitializeComponent();
}
private void button2_Click_1(object sender, EventArgs e)
{
string deviceNum = comboBox2.Text;
char dN = deviceNum[0];
start = new Listen();
Task.Factory.StartNew(() => start.StartListen(dN));
}
private void button3_Click(object sender, EventArgs e)
{
lis = new Listen();
var devices = lis.GetDevices();
comboBox2.DataSource = devices;
}
public void PrintReceivedPackets(string packetInfo)
{
// Do not work
Console.WriteLine(">>>" + packetInfo);
listBox1.Items.Add(packetInfo);
textBox1.AppendText(packetInfo);
}
}
Methods in Class Listen
public void StartListen(char deviceNum)
{
int deviceNumber = (int)Char.GetNumericValue(deviceNum);
// Take the selected adapter
PacketDevice selectedDevice = allDevices[deviceNumber - 1];
// Open the device
using (PacketCommunicator communicator =
selectedDevice.Open(65536, // portion of the packet to capture
// 65536 guarantees that the whole packet will be captured on all the link layers
PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode
1000)) // read timeout
{
Console.WriteLine("Listening on " + selectedDevice.Description + "...");
// start the capture
communicator.ReceivePackets(0, PacketHandler);
}
}
// Callback function invoked by Pcap.Net for every incoming packet
public void PacketHandler(Packet packet)
{
Console.WriteLine(packet.Timestamp.ToString("dd-MM-yyyy ") + " length:" + packet.Length + " " + packet.DataLink);
string packetInfo = packet.Timestamp.ToString("dd-MM-yyyy ") + " length:" + packet.Length + " " + packet.DataLink;
Form1 f = new Form1();
f.PrintReceivedPackets(packetInfo);
}
Edit: added comments on how to make the main form visible to the Listener
The easiest way would be to add a reference to the main form to your Listen class. A'la:
public class Listen
{
Form1 mainForm;
public Listen(Form1 mainForm)
{
this.mainForm = mainForm;
...
}
}
Then, in button2_Click_1 you can create the start object like this:
start = new Listen(this);
And then, in PacketHandler, you can do:
mainForm.Invoke((Action)(() => mainForm.PrintReceivedPackets(packetInfo)));
And remove Form1 f = new Form1() from the PacketHandler, as you dont actually want a new form for every packet.
In the PacketHandler you are creating a new Form1 instance and then calling PrintReceivedPackets method of that form. That form, judging by the code, is never actually opened - for that you would need to call f.Show() or f.ShowSialog() at some point.
If your intent is to display the packet notification of the actual main form, then you need to do two things:
Make your main form visible to the StartListen object, by either assigning the main form object to a global variable, or pass it into StartListen as parameter.
In PacketHandler, call the PrintReceivedPackets method of the main form. It's not quite apparent, in which thread the PacketHandler is executed. If you get a "cross-thread" exception, then you need to pass the update into main thread with Invoke, something like this:
mainForm.Invoke((Action)(() => mainForm.PrintReceivedPackets(packetInfo)));
All these comes from the idea that i want to use the SerialPort class in .Net , but the only way is by calling dll . Because i can only get interfaces from the program calling this dll. mycode is below.
i wrote a class about serialport,
public class CommClass
{
public SerialPort _port;
private string _receivedText;
public string receivedText
{
get { return _receivedText; }
set
{
_receivedText = value;
}
}
public CommClass(string _pname)
{
portList = SerialPort.GetPortNames();
_port = new SerialPort(portList[0]);
if (portList.Length < 1)
_port= null;
else
{
if(portList.Contains(_pname.ToUpper()))
{
_port = new SerialPort(_pname);
_port.DataReceived += new SerialDataReceivedEventHandler(com_DataReceived);
}
}
}
private void com_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string indata = _port.ReadExisting();
receivedText = indata;
}
}
from Bytestoread i can see there r data coming in and i can get data from port.ReadExisting(), but receivedText did not change ,it did not hit the SerialDataReceived event . Is my way wrong?any suggestion?thanks
i created a dll from CommClass ,then i call it in my winform program which has a button and a textbox . Clicking the button , then i initialize the port
public Form1()
{
InitializeComponent();
}
public CommClass mycom;
private void button1_Click(object sender, EventArgs e)
{
mycom = new CommClass("com3");
mycom._port.Open();
textbox.Text=mycom.receivedText;//i add a breakpoint at this line ,
}
when hitting it , i check mycom._port.PortName is "com3", its IsOpen() is "Open" , i use virtual port to send data . i send "1111",then check the mycom._port.BytestoRead is 4, and mycom._port.ReadExisting() is "1111", but mycom.receivedText is null. My puzzle is that i have no idea when the data is coming . How to use the DataReceived event in my winform without code "using System.Io.Ports",just with reference CommClass.dll. Did i make it clear? Thanks for help.
mycom._port.Open();
textbox.Text=mycom.receivedText;//i add a breakpoint at this line ,
That code cannot work, it is a threading race bug. The DataReceived event does not fire instantly after you open the port. It will take a microsecond or so, give or take. A threadpool thread has to get started to fire the event. And of course the device actually has to send something, they usually only do so when you transmit something first.
Which clearly did not happen, your DataReceived event handler has a bug as well. It is not allowed to update the Text property of a control in that event since it runs on a worker thread. Your program will bomb with an InvalidOperationException.
You'll have to write something like this instead:
private void com_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string indata = _port.ReadExisting();
this.BeginInvoke(new Action(() => {
textbox.AppendText(indata);
}));
}
With the additional stipulation that you must not leave it this way, updating the Text property of a TextBox and making it visible on the screen is an expensive operation that's going to turn your user interface catatonic when the device starts transmitting data at a high rate.
I have the following piece of code:
class NotepadCloneNoMenu : Form
{
protected TextBox txtbox;
public NotepadCloneNoMenu(string a)
{
Text = "Notepad Clone No Menu";
txtbox = new TextBox();
txtbox.Parent = this;
txtbox.Dock = DockStyle.Fill;
txtbox.BorderStyle = BorderStyle.None;
txtbox.Multiline = true;
txtbox.ScrollBars = ScrollBars.Both;
txtbox.AcceptsTab = true;
txtbox.AppendText(a);
txtbox.AppendText("\n");
}
}
class program1
{
public static void Main()
{
string result = "abc";
while(true)
{
Application.Run(new NotepadCloneNoMenu(result));
}
}
}
I want to continuously appending the string result to the textbox so it looks like this:
abc
abc
abc
so on and so forth. However, every time I called this:
Application.Run(new NotepadCloneNoMenu(result));
It will reset the textbox. Is there anyway I can update the textbox continuously? I am fairly new to C# so this is quite confusing to me.
thanks,
Phuc Pham
First of all, you're continuously closing and opening an application. That's why it resets. If you want to run an infinite loop, you probably want to run it inside your application proper.
In your application code, use some event (maybe a timer would suit you) to append text to the textBox. Like this:
public someEventOnTheForm (object sender, EventArgs e)
{
txtBox.Text += "Notepad Clone to Menu";
}
There's two more things to take into account: first, if you don't have a stoping condition, this will just keep filling memory until you run out of it.
Second, windows forms run on only one thread by default. You'll be using that thread to update the textbox, so while it's appending text, the form itself will be unusable. It'll probably blank out during the event if it starts taking long. You'll need a second thread to handle the event if you want your form to be usable.
I have an API (dll) that collects stock ticks via an event mechanism. Such as below:
...
using MT4API;
public partial class Blue : Form
{
...
public Blue()
{
...
string symbol = "GBPUSD";
MT4DDE dde = new MT4DDE("");
dde.OnQuote += new System.EventHandler<QuoteEventArgs>(MT_OnQuote);
dde.Connect();
dde.Subscribe(symbol);
....
The idea is that on each chart tick I get an event. here is the event handler code:
private static void MT_OnQuote(object sender, QuoteEventArgs args)
{
GlobalClass.Ask = args.Ask;
GlobalClass.Bid = args.Bid;
// I have back ground worker code that updatestables from the global class
}
This all works fine. So long as I do not touch any other buttons on the form UI. As soon as I click a button on the form of the UI... I no longer receive events from my API, the UI application functions normally, but with no data from the API.
Why do events from the UI stop any further events coming from the API event?
Any idea whats going on here? Or suggestions how to design this?
Does the same problem occur if you comment out your code that updates the tables from the global object? and if you comment out the background worker?
It would be a good idea to distinguish if the event stops being fired just after you press some button on the UI, or if it stops being fired only after some line of code you wrote is being executed.
In order to be able to help you, we would need to know how the event on the MT4DDE class is triggered.
If you have the code for this class, posting it would help.
If you don't you may want to use a tool such as Reflector to decompile the assembly into C# and see what the MT4DDE class is doing that might cause it to stop invoking the event.
In addition, if you are doing anything related to background threads, or if you're doing anything unusual with your application's main message loop, it would be a good idea to mention it here.
I have tried to use the invoke command, it works, but after a few events it stops...here is the code isolated:
using MT4API;
namespace WindowsFormsApplication1
{
public delegate void UpdateTextCallback(double ask, double bid);
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string symbol = "GBPUSD";
MT4DDE dde = new MT4DDE("");
dde.OnQuote += new EventHandler<QuoteEventArgs>(MT_OnQuote);
dde.Connect();
dde.Subscribe(symbol);
}
private void updateTickDisplay(double ask, double bid)
{
textBox1.Text = ask.ToString();
textBox2.Text = bid.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void MT_OnQuote(object sender, QuoteEventArgs args)
{
BeginInvoke(new UpdateTextCallback(this.updateTickDisplay),
new object[] { args.Ask, args.Bid });
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox3.Text = textBox1.Text;
}
}
}
The only difference from the real code is that I am using a data grid....as opposed to a text field. But it is clear that the UI blocks somehow the new events. It is strange that I get about 5 to 10 events and then it just stops. Strange. Any ideas on a differnet design?