|
if I understand all that correctly, you have a single communication means, and the PC does not really have much to compute, so I would use:
- either a single thread to do it all;
- or exactly one thread for each device.
The major issue seems to be to dispatch the incoming messages, so the result can be stored, logged, counted, and followed by a new outbound message. That can be handled by a data-driven approach and does not require separate threads at all. Threads are expensive: they each need memory (including a stack), and thread switching takes time too.
If you want all that to reside in a service, you could do that, it making sense depends on what people are supposed to do or not to do to the PC in the mean time.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. [The QA section does it automatically now, I hope we soon get it on regular forums as well]
|
|
|
|
|
To explain more, I have one example. I have 3 (but it can be more) port COM. With my application the goal is to do at the same time:
Send message every 50ms to COM1 (time interval is defined by the user in a user interface)
Send message every 75ms to COM2
Send message every 50ms to COM3
For sending, I use one Timer for each port COM (= 3 thread),each thread wake up at the time defined by the user (i.e. one at 50ms, one at 75ms and the last one at 50ms).
So if you send message, you receive answer. It seems that we need a thread for each port COM to receive data (we don't know when the data is coming back). The thread of reception sleep until a data is received. When a message is received, the thread wake up, we push the message into a list and the thread sleep to the next reception.
Another thread check the list of message in reception and log the info.
I know that the thread is expensive and this is my problem. Now I use 2 thread by devices (one for sending, one for reception) and one thread to check the data and log it. This is the reason for what I want to change for a service. I think that one service by device can be used and so reduce to max 2-3 thread for each service. I think that the service have one process for him.
|
|
|
|
|
Hi,
0.
You should have provided this information and more from the start, you have been wasting time.
1.
having P identical processes with T threads each isn't any better than having 1 process with P*T threads; it is the same amount of work for the Windows kernel, and takes the same amount of stack space.
2.
one normally doesn't need a thread to output something. Output most often can be handled synchronously.
3.
one often needs some asynchronous code to receive data; it could be a thread performing a blocking read, it could also be an event running on some thread, e.g. SerialPort.DataReceived. You may want to read this[^].
4.
Windows timing isn't always very accurate; you may want to read this[^]
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. [The QA section does it automatically now, I hope we soon get it on regular forums as well]
|
|
|
|
|
Hi,
Thank you
0. Yes, sorry! I have not take into account the difficulty to understand my need.
1. Of course the memory and stack space is not reduced, it seems that is greather with more process but perhaps I have more time execution by thread. Each process have a limited time in the Windows kernel and this time is splited into the number of thread.
2. Probabily, I need to check that because I have one processor and is not possible to send at the same time at two or more differents output.
3-4 thank you for the link! I have not the time now to read that, but later I do it.
I see that you have a good knowledge about the "low level" and I am interested to clarify one more point.
Have you one link for the event in c#. I use some event to report information (e.g. device error, reception of data) and it seems that is need to be used carrefuly. When a event is generated, it is the thread where the event is generated that execute the call or a new thread (one thread by listner of an event?) ?
|
|
|
|
|
lord_laurent_r wrote: I use some event to report information
if that refers to the event keyword, then yes they are synchronous, no extra threads involved.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. [The QA section does it automatically now, I hope we soon get it on regular forums as well]
|
|
|
|
|
no, it refer to a delegate type for hooking up change notifications
|
|
|
|
|
I've got the following code running and the idea is that a row in the DB has a coloured letter in it to denote customer or supplier etc. The first row i highlight seems to vanish, the background for the row is blue and the selected index is set so that's all nice but the text disappears. If i click the row a second time the row unselects and the text comes back.
public partial class CompanyListControl : ListBox<br />
{<br />
int _previousIndex = 0;<br />
public CompanyListControl()<br />
{<br />
InitializeComponent();<br />
this.DrawMode = DrawMode.OwnerDrawFixed;<br />
}<br />
protected override void OnDrawItem(DrawItemEventArgs e)<br />
{<br />
base.OnDrawItem(e);<br />
if (!DesignMode)<br />
{<br />
e.DrawBackground();<br />
if (this.SelectedIndex == e.Index)<br />
{<br />
if (_previousIndex != e.Index)<br />
{<br />
InvalidateItem(_previousIndex);<br />
DrawSelected(e);<br />
_previousIndex = e.Index;<br />
}<br />
}<br />
else<br />
{<br />
DrawUnselected(e);<br />
}<br />
}<br />
}<br />
public void InvalidateItem(int index)<br />
{<br />
if ((index < 0) || (index >= this.Items.Count))<br />
return;<br />
object InvalidatedObject = this.Items[index];<br />
this.Items.RemoveAt(index);<br />
this.Items.Insert(index, InvalidatedObject);<br />
}<br />
private void DrawSelected(DrawItemEventArgs e)<br />
{<br />
if (e.Index == -1) return;<br />
int RowWidth = e.Bounds.Width;<br />
Font f = new Font("Arial Black", e.Font.Size);<br />
Size s = TextRenderer.MeasureText("C", f);<br />
int SalesPos = 0;<br />
float PurchasePos = s.Width * 0.7f;<br />
float TextPos = PurchasePos * 2;<br />
int VerticalPos = e.Bounds.Top;<br />
e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, Brushes.White, TextPos, e.Bounds.Top);<br />
e.Graphics.DrawString("C", f, Brushes.Red, SalesPos, VerticalPos);<br />
<br />
}<br />
private void DrawUnselected(DrawItemEventArgs e)<br />
{<br />
int RowWidth = e.Bounds.Width;<br />
Font f = new Font("Arial Black", e.Font.Size);<br />
Size s = TextRenderer.MeasureText("C", f);<br />
int SalesPos = 0;<br />
float PurchasePos = s.Width * 0.8f;<br />
float TextPos = PurchasePos * 2;<br />
int VerticalPos = e.Bounds.Top;<br />
e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, Brushes.Black, TextPos, e.Bounds.Top);<br />
e.Graphics.DrawString("C", f, Brushes.Red, SalesPos, VerticalPos);<br />
}<br />
protected override void OnSelectedIndexChanged(EventArgs e)<br />
{<br />
base.OnSelectedIndexChanged(e);<br />
this.Invalidate();<br />
}<br />
}
Does anyone have any ideas what might be wrong? I've added and removed all sorts of bits from the code and it doesn't seem to change the behaviour very much at all (to the point that I put some break points in just to check that this code was actually being run)
Thanks
Russ
|
|
|
|
|
It was wery hard to find your code problem, but i did find it. Use this:
protected override void OnDrawItem(DrawItemEventArgs e)
{
base.OnDrawItem(e);
if (!DesignMode)
{
e.DrawBackground();
if (this.SelectedIndex == e.Index)
DrawSelected(e);
else
DrawUnselected(e);
}
}
Also InvalidateItem function removed item and inserted, witch coused to be OnDrawItem called. You have created infinitive loop.
Edit: also I do not think if (!DesignMode) is nesesery, because you want to see items in designer mode if you add items using property windows. This is normal behaviour for ListView
|
|
|
|
|
thank you,
not at my desk at the moment but i'll try that fix in the morning.
The invalidate thing was added because i thought maybe it needed to redraw to fix it, adding it seemed to make no difference at all, I should have taken it out before posting
Thanks again,
Russ
|
|
|
|
|
Russell Jones wrote: i thought maybe it needed to redraw to fix it
Actualy you only draw an items. For that usaly happens in OnDraw function.
|
|
|
|
|
Please go through...
class Test
{
Console.WriteLine("1:Add\n2:Sub\n3:Mult:\n4:Div");
Console.WriteLine();
Console.WriteLine("Press 9 to Exit or Enter to Continue ");
string s=Console.ReadLine();
....
}
My query is that when I enter the "ENTER" key, it does go ahead but it also does the same for the other keys except 9, for which it exits from the Program.
Hope so I will get a solution
|
|
|
|
|
Yes it will.
I assume you have something along the lines of:
do
{
Console.WriteLine(...
...
s = Console.ReadLine();
} while (s != "9")
If so, then it will loop round for any sequence of keypresses follewed by an ENTER other than the '9' key followed by ENTER.
Remember that ReadLine will retrun with what has been pressed up to an ENTER - so if you only press enter, you will get and empty string. You will have to check for this and act accordingly.
Sorry if this sound vague, but your code fragment doesn't give us a lot to go on!
All those who believe in psycho kinesis, raise my hand.
My 's gonna unleash hell on your ass. tastic!
|
|
|
|
|
hi,
for reading a key you can use ReadKey method and a good practice for you're application is to use a switch statement like this:
Console.WriteLine("1:Add\n2:Sub\n3:Mult:\n4:Div");
Console.WriteLine();
Console.WriteLine("Press 9 to Exit or Enter to Continue ");
ConsoleKeyInfo cki = Console.ReadKey();
switch (cki)
{
case ConsoleKey.D9:
Console.WriteLine("exit");
break;
case ConsoleKey.Enter:
Console.WriteLine("continue");
break;
case ConsoleKey.D1:
Console.WriteLine("Add");
break;
case ConsoleKey.D2:
Console.WriteLine("Sub");
break;
case ConsoleKey.D3:
Console.WriteLine("Mult");
break;
case ConsoleKey.D4:
Console.WriteLine("Div");
break;
default: break;
}
Anyway I don't understand why use the ENTER key to continue? have you more option than 1,2,3 and 4 ?
Cheer's,
Alex Manolescu
|
|
|
|
|
Ok, so I have a DataGridView control with 7 columns, the column names are identical to the ones in my MySql Database and I have this code:
public partial class Form3 : Form
{
private MySqlConnection connection = new MySqlConnection();
private MySqlDataAdapter data = new MySqlDataAdapter();
public Form3()
{
InitializeComponent();
connection.ConnectionString =
"server=uhhhhh;"
+ "database=uhhhh;"
+ "uid=uhhhh;"
+ "password=uhhh;";
connection.Open();
MySqlCommand command = connection.CreateCommand();
command.CommandText = "select * from data";
data.SelectCommand = command;
DataSet dataset = new DataSet();
data.Fill(dataset, "data");
gridInfo.DataSource = dataset;
gridInfo.DataMember = "data";
gridInfo.Dock = DockStyle.Fill;
}
}
Basically what I want to do is retreive all the info from the DataBase, and show it in the DataGridView. I hope you can help me.
Regards,
Melvin
|
|
|
|
|
So did the DataGridView show anything?
|
|
|
|
|
Try removing the columns you have created for the DataGridView, then let the grid view build them itself by using the gridInfo.DataBind() method.
|
|
|
|
|
DataGridView does not have a DataBind method - GridView does (I've made that mistake myself!)
All those who believe in psycho kinesis, raise my hand.
My 's gonna unleash hell on your ass. tastic!
|
|
|
|
|
1. DataGridView and GridView are two different things.
2. OP wasn't clear enough about the what exactly he wants to show in the gridview. So, which columns are you asking to remove?
"No matter how many fish in the sea; it will be so empty without me." - From song "Without me" by Eminem
|
|
|
|
|
What do you mean by all the information from the database?
"No matter how many fish in the sea; it will be so empty without me." - From song "Without me" by Eminem
|
|
|
|
|
Ok, well its a DataGridView, and I just want every entry in the database, put in there. So the database has 7 columns, and for example it has an entry like this:
300,mark,male,homosexual,single,male,19
And I want the DataGridView to display that info, in the appropriate columns.
|
|
|
|
|
I generally use a datatable instead of a dataset, I find it easier to debug. I can't see anything to stop this code working, change the datasource to a table, debug and make sure you are getting data returned from the database.
I also never use the data member property with a table. This may be forcing the DGV to look for a TABLE in the dataset named data which does not exists of course!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
dataset DS = new Dataset
datatable DT = new DataTable
mysqlconnection connection = new mysqlconnection();
connection.ConnectionString = "blah blah blah"
connection.Open();
sqlite_comm = connection.CreateCommand();
string CommandText = "select * from data";
mysql_da = new MySQLDataAdapter(CommandText, connection);
DS.Reset();
mysql_da.Fill(DS);
DT = DS.Tables[0];
dataGrid1.DataSource = DT;
connection.Close();
|
|
|
|
|
I have a C# form users use to enter hours worked, sick time , vacation.
It is connected to access 2003 database.
The form does not have a login- each person just selects last name from combo box and any secure data is not visible on form.
is there a way I can display a current number of vacation hours, sick time left each week on form for each person.
I recieved an excel sheet with total number of vacation hours and sick hours each user has for this year at present. When someone uses time it has to calculated and updated.
I am uncertain of how of even how to start on this task. any ideas. I also have to have vacation time and sick time only available to user. no one else can view.
thank you,
|
|
|
|
|
You would force a user to select their name first and then query the data on the SelectedItemChanged event, same as you would select data from another data source on a different event, such as saving data on a button click
|
|
|
|
|
|