I've done some googling, but haven't turned up any answers to my questions - can anyone help?
1) My application reads comma delimited records from a file. The read and split is very fast indeed, but after they've been read I need to create objects out of the records, do some validation etc. etc. This can take a while to complete for large record sets. Aha, says I, multiple threads can speed this up.
My plan is to have one thread read the file and then place the returned string array in to a queue. Then as many worker threads as needed can dequeue an individual array in the queue and go off to do its work.
First up - is this a sensible approach, or is it a real stinker of an idea and are there better ways to achieve the same?
2) Is dequeuing thread safe, or could two threads attempt to dequeue the same item in the queue?
3) Are there any guidlines on the number of threads one should spawn for a given processor type? My PC has a Quad Core in it, which I assume means that things really can happen concurrently. My users, however, may well be limited to single core processors which I think will mean that the threads will share processor time - could too many threads in this situation lead to much slower performance as they all vie for time?
Me: Can you see the "up" arrow?
Me: Can you see an arrow that points upwards?
User: Oh yes, I see it now!
-Excerpt from a support call taken by me, 08/31/2007
Even under conditions where it would it might be insignificant.
could too many threads in this situation lead to much slower performance as they all vie for time?
There are many issues that come into play like overhead for context switching and cache misses etc. Actually two threads could cause that to happen and unless yours is the only process running on a multi processor/core computer (very unlikely it is the only one) it may not get access to more than one core and could also result in reduced performance.
Multi-threading in a single user application is mostly (not always) applicable to keeping the UI from freezing during a long running process like graphics manipulation or interprocess communications, etc. Otherwise threads are mostly useful in a multi-user environment.
I've written a program where part of it will print a selected file to a printer. This file has tabs in certain places that are crucial to the output not looking wonky.
The output looks wonky. However, if I print it up using something like Notepad, it looks just fine. I'm using a generic filestream/printer handler to do the job. Is there a way for the tabs to be preserved?
Thanks for your time,
//print the report<br />
OpenFileDialog fdlg = new OpenFileDialog();<br />
fdlg.Title = "C# Corner Open File Dialog";<br />
fdlg.InitialDirectory = @"C:\ ";<br />
fdlg.Filter = "Text files (*.txt | .txt | All files (*.*) | *.*";<br />
fdlg.FilterIndex = 2;<br />
fdlg.RestoreDirectory = true;<br />
if (fdlg.ShowDialog() == DialogResult.OK)<br />
textBox1.Text = fdlg.FileName;<br />
string filename = textBox1.Text.ToString();<br />
//Create a StreamReader object<br />
reader = new StreamReader(filename);<br />
//Create a Verdana font with size 10<br />
verdana10Font = new Font("Arial", 10);<br />
//Create a PrintDocument object<br />
PrintDocument pd = new PrintDocument();<br />
//Add PrintPage event handler<br />
pd.PrintPage += new PrintPageEventHandler(this.PrintTextFileHandler);<br />
//set up for landscape<br />
pd.DefaultPageSettings.Landscape = true;<br />
//Call Print Method<br />
//Close the reader<br />if (reader != null)<br />
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
I'm not sure if that's going to help, because I'm not wanting to even use Notepad to print up these files. The problem is whenever the file is printed, the tabs that would normally keep things spaced out evenly are replaced with whitespaces.
The printer handler I'm getting ready to show basically takes each line from a file and turns it into something that the printer can understand. It does it line by line but makes no provision for tabs. Can you show me how to modify this?
Thanks for your time,
privatevoid PrintTextFileHandler(object sender, PrintPageEventArgs ppeArgs)<br />
//Get the Graphics object<br />
Graphics g = ppeArgs.Graphics;<br />
float linesPerPage = 0;<br />
float yPos = 0;<br />
int count = 0;<br />
//Read margins from PrintPageEventArgs<br />float leftMargin = ppeArgs.MarginBounds.Left;<br />
float topMargin = ppeArgs.MarginBounds.Top;<br />
string line = null;<br />
//Calculate the lines per page on the basis of the height of the page and the height of the font<br />
linesPerPage = ppeArgs.MarginBounds.Height / verdana10Font.GetHeight(g);<br />
//Now read lines one by one, using StreamReader<br />while (count < linesPerPage &&<br />
((line = reader.ReadLine()) != null))<br />
//Calculate the starting position<br />
yPos = topMargin + (count *<br />
//Draw text<br />
g.DrawString(line, verdana10Font, Brushes.Black,<br />
leftMargin, yPos, new StringFormat());<br />
//Move to next line<br />
//If PrintPageEventArgs has more pages to print<br />if (line != null)<br />
ppeArgs.HasMorePages = true;<br />
ppeArgs.HasMorePages = false;<br />
I have two applications where appA relies on appB. AppB, I do not have the ability to change the source code on appB. What I need to do is make sure appA terminates before appB when the user shuts down the computer. Is there anyway to make sure this happens?
A shutdown message is broadcast to the top level window of all applications at the same time. It does not shut them down in the order that they were launched. Actually, if there was any order to it at all, it would be the reverse of the launch order. Last launched would get shutdown first because of dependancies.
Well i don't know, i just had 2 different aplications show a messagebox in the onClosing event. And whichever one i started first, showed its messagebox first. Unless... if both messages appeared center screen, then the one that started first would have closed last, leaving its message on top... Still, I must admit even if it where the case, it is still unreliable.
You might find a solution in SystemEvents.SessionEnding. I haven't played with it, but it seems like that might work, at least for knowing that log off/shutdown is happening.
It was my understanding that Windows shuts apps down in no guaranteed order, though I'm sure someone will chime in if there is a guaranteed order. It might be as the other reply stated, but I wouldn't necessarily rely upon that.
I'd be interested in what you find out.
It isn't enough to do well in life.
One must do good when and where one can.
Otherwise, what's the point?
...if calling Dispose() results in an implicit call to Close(), why do people say that you shouldn't rely on Dispose to Close your connections? Take this code for example:
if (table != null)
using (OleDbConnection connection = new OleDbConnection())
using (OleDbCommand command = connection.CreateCommand())
command.CommandText = "SELECT obj From " + table.Alias;
using (OleDbDataReader reader = command.ExecuteReader())
//do other things that might cause an exception
MessageBox.Show("There was a GPS Error");
What would have priority over a thrown exception such that it HAD to process before a catch was allowed to handle the Exception? Anything? Dispose?