|
I a'm reading values from raspberry pi mysql database with visual studio c#
If the database is not available and I am trying to read from it, the c# app
freezes for about 15 seconds.
Is it possible to make it not to freeze when checking if the database is available?
Her is the code
<pre lang="C#">string connString = "SERVER='192.168.86.41';DATABASE='spaceinformation';UID='******';PASSWORD='******'";
private void FormMeasurement_Load(object sender, EventArgs e)
{
try
{
using (var connection = new MySqlConnection(connString))
{
connection.Open();
query = "select * from weather";
using (var command = new MySqlCommand(query, connection))
{
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
listBoxMeasurement.Items.Add(string.Format("Temperature: {0} Humidity: {1} Date: {2}", reader.GetString("temp"), reader.GetString("hum"), reader.GetString("datecreated")));
}
}
}
connection.Close();
}
}
catch
{
MessageBox.Show("Database is not available for moment!");
listBoxMeasurement.Enabled = false;
}
}
|
|
|
|
|
Having used neither raspberry pi and/or mysql EVER, I might be chasing down a pot-o-fool's gold here (Raspberry Pi? That's expired pharmacy my friend) but I see THAT connString and always say to myself "config issue/networking port" issue.
I might open up Task Manager and set the display to anything that resembles the realtime CPU use, probably where there's a network monitoring graph that one can set the sample speed FASTER, then go through the motions which cause this "freeze" ... and see if the two things align in time.
Come to think of it, VS (C#? Unlikely slow (blazes fast eh?)) ... leaves frozen footprints sometimes when running debug. Now, there's an idea!
Run debug while under VS gravitational pull.
|
|
|
|
|
That "freeze" is the connection object giving the server time to respond to the connection request. When that times out, that's when you get the exception message.
What do you do about that? Set the Connect Timeout[^] in the connection string.
|
|
|
|
|
Assuming the MySql connector supports it, you could try using an async method:
private void FormMeasurement_Load(object sender, EventArgs e)
{
listBoxMeasurement.Enabled = false;
_ = LoadFormAsync();
}
private async Task LoadFormAsync()
{
try
{
using (var connection = new MySqlConnection(connString))
{
await connection.OpenAsync();
const string query = "select * from weather";
using (var command = new MySqlCommand(query, connection))
using (var reader = await command.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
{
listBoxMeasurement.Items.Add(string.Format("Temperature: {0} Humidity: {1} Date: {2}",
reader.GetString("temp"), reader.GetString("hum"), reader.GetString("datecreated")));
}
}
}
listBoxMeasurement.Enabled = true;
}
catch (Exception ex)
{
MessageBox.Show("Database is not available for moment!\n" + ex.Message);
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
When debugging a multi-threaded process, is there any way to make Visual Studio step through only one of the threads at a time, so that when you're stepping, it's not jumping from thread to thread? The jumping from one thread to another makes following one specific thread almost impossible.
I've tried the thread flagging feature, but one must still keep choosing the desired thread from the dropdown every time it jumps.
Is there any help from VS to focus on just one thread when stepping?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Seems one would fully test with one thread; then employ multiple threads. At that point, trying to isolate a single thread would seem redundant. When in doubt "lock" critical code and / or use concurrent objects.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
Well when one is employing BackgroundWorker threads as part of the basic design, I don't see how it's possible to test that code with only one thread.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
The backgroundWorker (BGW) is one thread. I have used multiple BGW workers. Some doing the same work; some different. I often use a wrapper for each BGW I launch; so in effect they have their own scope; maybe that's the difference.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
Hi,
You can use the "Threads" window while single-stepping and right-click to Freeze/Suspend all the other threads. The "Parallel Watch" window also has this feature.
|
|
|
|
|
Bingo! I suspected there had to be a way. Thanks!
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
No problem,
Debugging is a skill that everyone should learn.
Btw, Visual Studio sucks for debugging, spend a few months (few hundred hours) using WinDbg and you'll never need to ask questions in an internet forum.
|
|
|
|
|
Student mystudent = new Student() ;
In this line why student used before mystudent.
|
|
|
|
|
Because Student is the type of object being created, so the variable mystudent needs to be declared as the correct type. Similarly:
int foo = 1;
declares the variable foo as an int object. Writing something like:
int foo = new Student();
would make no sense, as an int type cannot hold a Student object.
|
|
|
|
|
It's a variable declaration, just like:
int x = 666; Or
string userName = " vineeth s";
The type of variable it it precedes the name so the system can tell what type of variable it is:
<Variable Type> <Variable Name> <Optional initialization value>; So in your specific case "Student" is the class that "myStudent" is going to hold.
Make sense?
"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!
|
|
|
|
|
Because C# is a strongly-typed langrage which is compiled. The compiler must "know" the Type of any value, or object, created ... before compilation.
The (shortcut) use of var to hold a result, as in this example: var mystudent = new Student(); ...
Will work because the compiler can infer the Type from the right hand side of the assignment.
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
|
|
|
|
|
BillWoodruff wrote: var mystudent = new Student();
On a similar note, from C# 9 onwards, the compiler can often infer the type on the right-hand side from the declaration:
Target-typed new expressions - C# 9.0 draft specifications | Microsoft Learn[^]
Student student = new(); That works in more places than var (eg: fields), and can reduce the amount of typing even further; for example, compare:
var students = new List<Student>
{
new Student { ... },
new Student { ... },
}; to:
List<Student> students = new()
{
new() { ... },
new() { ... },
};
However, I suspect the use of var and target-typed new might currently be a few lessons further down the line for the OP.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
thanks, when i was teaching, i "forced" my students to specify Type explicitly the newer uses of thing like discards make me dizzy, use of new() makes me nauseous
ReSharper has gotten better and better at controlling use of 'var: [^]
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
|
|
|
|
|
As so many commentators at the World Cup have suggested, it is time to stop using VAR.
|
|
|
|
|
i am completely baffled world cup <=> to var ?
(on strong meds, brain fogged) bill
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
|
|
|
|
|
VAR stands for Video Assistant Referee, and lately has caused some very controversial decisions.
|
|
|
|
|
Message Closed
modified 29-Nov-22 8:40am.
|
|
|
|
|
You need to remove it the same way you removed it from the common list. Remember, after the two calls to AddRange you have three separate lists, so what you do to one list does not have any effect on the others.
|
|
|
|
|
To add to what Richard has said, the important thing here is the keyword new
When you create a new instance of any class, that is exactly what you get: a brand new, totally separate instance of the class which has nothing at all to do with any other. Think about it: if you put your mobile in the glovebox of your car, then go and buy a new vehicle, would your mobile be in it's glovebox? Of course not - you understand that the two cars are separate, and whatever you do to the first one has no affect whatsoever on the second! Indeed, life would get very odd if it was otherwise!
So when you do this:
List<int> a = new List<int>();
List<int> b = new List<int>();
List<int> c = new List<int>(); you create three independent collections: whatever you do to one has no affect on the others. When you add items to a collection a "copy" of the value is added so when you remove them, only that local copy gets removed.
If instead of integers you used a class, then a copy of the reference to that class instance is added, so if a class exists in two collections, then changing it's content via either collection effects the same instance:
public class MyClass
{
public int X { get; set; }
}
private void MyButton_Click(object sender, EventArgs e)
{
List<MyClass> a = new List<MyClass>();
List<MyClass> b = new List<MyClass>();
MyClass mc = new MyClass() { X = 1 };
a.Add(mc);
b.Add(mc);
foreach (MyClass m in a)
{
m.X += 10;
}
foreach (MyClass m in b)
{
m.X += 100;
}
Console.WriteLine(mc.X);
}
Will print "111" because the same instance is accessed from both collections.
"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!
|
|
|
|
|
Require assistance with a code example to be able to broadcast a simple message from an application to many instances of that application on the same computer. The message consists of a single string, either 1/0, On/Off, Start/Stop, High/Low or True/False broadcast from the Master application on 127.0.0.1:1234. The transmitting application (Master) shall continuously send the message until application close. The receiving application/s (Slaves) shall also receive the message continuously on a separate thread until the Slave application is closed. Timing is important, and needs to be less than 8 milliseconds, which I suspect will not be a problem.
The application is a VS 2019 WinForm type under dotNet 4.7, and the code snippet on the Master application shall be activated by a command button. The Slave/s will activate the appropriate code when either of the two conditions are meet.
As a retiree who last programmed in the 90's under Visual Studio 6, it's has been a leap to Visual Studio 2019 C#, and a lot of fun. But networking alludes me at this time. Appreciate any help on offer. With thanks.
|
|
|
|
|
Generally, client - server connections are point to point so that server can direct responses to the originating client only.
The way I'd probably do it is via Sockets: the app starts and tries to talk to a server instance. If it can, it's a client.
If it can't, it starts a server Socket and waits for a connection. When it gets it, that gets added to a Clients collection, and a new Socket waits for another connection.
When a socket closes, it's removed from the collection.
Broadcast is then simple: iterate the collection and send to each.
Make sense?
"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!
|
|
|
|