|
|
Opps, missed that part, sorry Johnyy. Thanks for pointing that out Nish, it's been another long day.
Nick Parker
|
|
|
|
|
You can use following
while(Cosnole.Read() != 'e')
{
.
.
//Do something
.
.
}
read only reads one character at a time
|
|
|
|
|
It's my understanding that System.Console.Read() reads everything until a return (someone presses enter). That while loop stops and waits for keyboard input, I dont want that... there's got to be some way of doing this I just gotta find it.
|
|
|
|
|
One option is to have a thread that reads the keyboard and have it change the value controlling the while loop when the key you want is pressed.
/ So code could be something like this…
static void Main(string[] args)
{
bool bLoop = true;
YourThreadedClass ytc = new YourThreadedClass(ref bLoop);
ytc.Start();
while(bLoop)
{
}
}
Unfortunately I’m new to C# so haven’t had chance to look at creating threaded classes yet. Also have a feeling doing it this way might be bad style, as it looks like a nasty hack
|
|
|
|
|
I think the solution is workable
|
|
|
|
|
johnyy wrote:
It's my understanding that System.Console.Read() reads everything until
It will wait till a key is pressed (not enter key but any key). You can try the method given below by Humpo. Another method might be to subclass the console window.
|
|
|
|
|
Ok... threads I can figure out, Console.Read() I cant. I can't find documentation on this function on msdn, and when i try and use it in my code it wont work because i dont know what it returns (is it a int, if so how do you converty int to char to string?).
Thanks.. ive got a multithread program now
|
|
|
|
|
hope this helps..
class Class1
{
private bool bLoop;
private Thread myThread;
[STAThread]
static void Main(string[] args)
{
Class1 c1 = new Class1();
}
public Class1()
{
bLoop = true;
KeyReader kr = new KeyReader();
kr.KeyEvent += new KeyReader.KeyEvents(KeyEvent);
myThread = new Thread(new ThreadStart(kr.ThreadRun));
myThread.Start();
while(bLoop)
{
System.Console.WriteLine(System.DateTime.Now);
}
myThread.Abort();
}
private void KeyEvent(int k)
{
System.Console.WriteLine("Key Event");
if(k=='e'|| k=='E')
bLoop = false;
}
}
public class KeyReader
{
public delegate void KeyEvents(int c);
public event KeyEvents KeyEvent;
public void ThreadRun( )
{
bool loop = true;
while(loop)
{
int ch = System.Console.Read();
if(KeyEvent != null)
{
System.Console.ReadLine();
KeyEvent(ch);
}
}
}
}
if anyone spots errors in code, please point them out for me
|
|
|
|
|
Hello everyone, is it possible to use the "\" charactor in a string? I need to do this becuase I need to store a folder path (I.E. c:\sierra5\logs\) into a string. Thanks...
|
|
|
|
|
Yes, use \\ or put an @ before the start of the string (@"c:\sierra5\logs\" ).
James
|
|
|
|
|
|
I know JTJ has already answered your question, but just in case you are interested Dr. GUI.NET has written a very good article on the string objects on MSDN. Here is the link.
Strings in the .NET Framework
Nick Parker
|
|
|
|
|
I want to create a Hashtable of strings and function pointers. How can I do that in C#/ASP.NET?
I'm just guessing here...
public string MyFunc(string instr)
{
return instr + "blah";
}
Hashtable table = new Hashtable;
table.Add("myfunc", MyFunc);
string str = "blah";
string blahblah = (table["myfunc"])(str);
something like that? But I'm not sure how to store the function in the hashtable and how to retrieve it and use it.
Todd Smith
|
|
|
|
|
In C# a function pointer is a delegate, so you'll be storing a delegate in the hashtable, and using the string as the key.
The delegate that can be used for MyFunc above would be:
public delegate string FunctionDelegate(string instr);
To add that to the hashtable you would do something like this.
Hashtable table = new Hashtable();
table["myfunc"] = new FunctionDelegate(MyFunc); (you could also use the Add method, but I prefer the array-like access).
Now to get it back out and use it;
FunctionDelegate d = table["myfunc"] as FunctionDelegate;
d(str); I found understanding delegates the hardest part for me to learn; once you get it though you'll wonder why you didn't get it sooner
Good luck,
James
|
|
|
|
|
Sweet. Now I'll be able to pull specific fields out of an ASP.NET form and convert the values before they get updated into the database.
public delegate string HoursToSecsCallbackType(string hours);
HoursToSecsCallbackType fp = new HoursToSecsCallbackType(Utils.HoursToSecs);
Hashtable table = new Hashtable();
table.Add("hours", fp);
string column = "hours";
TextBox textbox = (TextBox)objArgs.Item.FindControl(column);
if (table.ContainsKey(column))
{
HoursToSecsCallbackType fp = (HoursToSecsCallbackType) table[column];
textbox.Text = fp(textbox.Text);
}
Todd Smith
|
|
|
|
|
Is there a good tutorial somewhere that includes basic file i/o?
I've found this:
FileStream fs = new FileStream(@"c:\mcb.txt" , FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter filef = new StreamWriter(fs);
filef.BaseStream.Seek(0, SeekOrigin.End);
filef.Write(" File Write Operation Starts : ");
filef.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
DateTime.Now.ToLongDateString());
filef.WriteLine(" First Line : Data is first line \n");
filef.WriteLine(" This is next line in the text file. \n ");
filef.Flush();
but it doesn't work... I guess I'm looking for a basic explanation of what each one does and how to use them. A simple working example would be great, a tutorial would be great. Any help would be great
Thanks for the help.
|
|
|
|
|
Aside from missing a call to the Close() method on the stream that should work; what errors are you getting?
James
|
|
|
|
|
|
This is the error I get(in runtime):
Application has generated an exception that could not be handled.
Then it gave me the process id and thread id (do you want those?).
Well anyways, here is my script. I an very new to C# so chances are I am making some stupid error where you wouldn't expect it (which is why I include the whole source).
using System;
using System.IO;
class functions
{
public static void writefile()
{
FileStream fs = new FileStream("data.txt" , FileMode.Create, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("Testing\n TESTING!\n yay!");
sw.Flush();
}
}
class app
{
public static void Main()
{
Console.WriteLine("Before writefile()");
functions.writefile();
Console.WriteLine("After writefile()");
}
}
|
|
|
|
|
I was able execute the code you gave with no errors, file was created and contained the expected text.
Try putting sw.Close(); after sw.Flush() and see if that has any effect.
Try debuging the project to identify which line causes the problem- use "step over"(F10) and "step into"(F11), found under the debug menu in Visual studio.
Sorry this isn't much help, maybe the problem lies somewhere else- OS related maybe?
|
|
|
|
|
As Humpo said the code works fine here.
The error you stated doesn't help us, that error message is just saying some error happened. You should see something that says 'BlahBlahException'; that is the part we need to know so we know what error occured.
James
|
|
|
|
|
Sorry, using the command line compiler... thanks anyways for the help though.
I'll keep plowing through it.
|
|
|
|
|
Well i fixed it, Im not sure how- but i did.
Could you guys tell me how to move the file pointer around? My only real programming experience before this has been php. I know a open file has a internal pointer of some sort, i just dont know how to access it with c#.
Thanks
|
|
|
|
|
Think this is what you are looking for
FileStream objects support random access to files using the Seek method. The Seek method allows the read/write position to be moved to any position within the file.
here's an example of using seek to move to the last 128 bytes of a file...
System.IO.FileStream istream = theFile.Open(FileMode.Open);
long fileLength = istream.Length;
if(fileLength > MP3_LENGTH)
{
byte[] ba = new byte[MP3_LENGTH];
istream.Seek(fileLength-MP3_LENGTH, SeekOrigin.Begin);
istream.Read(ba, 0 , MP3_LENGTH);
GetFields(ba);
}
No doubt, as with all my code, there is a more efficient way
|
|
|
|