|
|
just so you know, by saying buddy, i was following the 8th point in the link you provided !!!!!!!!
peace buddy
|
|
|
|
|
I have been programming professionally for about 3 years. I know VB.NET well. I am wanting to branch out and learn C#. Do any of you have suggestions for Open Source projects written in C#? I would like to work on something where I don't have to be very committed because it will just be in my spare time. I would also like to work on small pieces of a project at least until I get used to the syntax. Any help is appreciated.
Humble Programmer
|
|
|
|
|
If you want to learn C#, I recommend you a book named "A Programmer's Introduction to C# , 2nd Edition". And online forums are good learning tools, but to have a solid foundation, I suggest that you buy a good book and learn step by step.
April
Comm100 - Leading Live Chat Software Provider
modified 27-May-14 8:49am.
|
|
|
|
|
Thank you for the suggestion on the book but the reason I ask about an open source project is because unless I have a reason to produce something such as helping a cause I will not write code after coding all day.
Humble Programmer
|
|
|
|
|
I have a NetForce USB modem (v.92 data transfer tech) which I want to send commands to. The problem is that I don't know what to send to the SerialPort (with WriteLine method) to receive answer from my modem. I tried "AT+DS44?" as the argument of WriteLine method and expected to receive all valid values of my modem as I read in this page : http://www.modemsite.com/56K/v92c.asp but in my DataReceive event handler when I call ReadLine function, I see that the return value is just what I wrote on the buffer; Meaning either the modem hasn't read the data(but why the event handler is risen?) or my modem is merely echoes what I write to it! I think I don't know sth about sending AT commands to modems but I don't know what?! Here's my whole c# code:
class Program
{
static SerialPort sp = new SerialPort("COM8", 56000, Parity.None, 8);
static void Main(string[] args)
{
if(!sp.IsOpen)
sp.Open();
sp.WriteLine("AT+DS44=?");
sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
ThreadStart ts = new ThreadStart(f);
Thread t = new Thread(ts);
t.Start();
}
static void f()
{
while (true) ;
}
static void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
Console.WriteLine("DataRecieved..............................");
string str = sp.ReadLine();
Console.WriteLine(str + "......................................");
}
}
|
|
|
|
|
Hi,
I have several comments:
1.
having a thread execute an empty loop is the perfect way to waste a CPU core. Are you sure you want this? I wouldn't.
2.
you may want to clear both inbound and outbound buffers when using a serial port. if anything is still in there, your PC app or your peripheral could receive it once the conditions are right.
3.
The DataReceived event is raised when some data is available (some as in "an undefined quantity"). Only reading a single line of text may not return all that is available. You might just have looked over the data you are expecting!
4.
modems (or whatever is at the far end) may echo all incoming characters; that is what you would hope for when using a terminal+modem to connect to a distant computer e.g.
5.
if relying on messages (e.g. strings terminated by \r\n) you might want to explicitly program the terminator; Environment.NewLine (I assume that is the default for SerialPort.NewLine) may vary when your app travels the world and/or goes to non-Windows platforms.
6.
The hardest one of all: serial communication can be very hard; there isn't much that forces message characters (such as text ending on \r\n) to stay together; you might get a DataReceived event halfway, etc.
I tend to do one of these two:
- insert a small delay between event and Read operation
- or provide my own buffering, then extract messages when they are completely available within my buffer.
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]
|
|
|
|
|
Luc has valid points; listen to him.
I don't bother with events and handlers when listening to such things (serial ports, network sockets, etc.).
I have a thread running a loop that reads any available data (if any) and then cycles. Processing the data is then handled by another part of the system (this may be what Luc referred to as implementing your own buffering).
And I don't care about newlines; I definitely would not use ReadLine on a socket or serial device.
|
|
|
|
|
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]
|
|
|
|
|
I have a simple method that takes a string as input and returns the time (in seconds) it takes to generate a another string that is the same – using System.Random.
private double timeOccurance2(string target)
{
string trial = string.Empty;
double intialTime = DateTime.Now.ToOADate();
Random rand = new Random();
while (trial != target)
{
StringBuilder b = new StringBuilder();
char[] ch = new char[target.Length];
for (int i = 0; i < target.Length; i++)
{
ch[i] = Convert.ToChar(Convert.ToInt32(Math.Floor(10 * rand.NextDouble() + 48)));
}
for (int i = 0; i < target.Length; i++)
{
b.Append(ch[i].ToString());
}
trial = b.ToString();
}
double finalTime = DateTime.Now.ToOADate();
return 24 * 60 * 60 * (finalTime - intialTime);
}
If I set target = “123246” and run the method repeatedly by clicking a button over and over, I get a random distribution of times, as expected.
Next, using the same target, I run multiple iterations of the method via BackGroundWorkers:
private void button1_Click(object sender, EventArgs e)
{
for (int i = 1; i <= 40; i++)
{
BackgroundWorker Computation = new BackgroundWorker();
Computation.DoWork += new DoWorkEventHandler(Computation_DoWork);
Computation.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Computation_RunWorkerCompleted);
Computation.RunWorkerAsync();
private void Computation_DoWork(object sender, DoWorkEventArgs e)
{
e.Result = timeOccurance2("123456").ToString();
}
private void Computation_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
richTextBox1.Text += (string)(e.Result);
richTextBox1.Text += (char)(10);
}
The result nearly always has a group of 4 numbers within the results that have nearly identical values. for example 407, 407, 422, 438. (Humm, my computer has 4 logical processors.) The cluster does not always appear at the beginning of the result set.
Why?
modified on Tuesday, February 2, 2010 9:38 AM
|
|
|
|
|
Probably because you are creating a new instance of Random, which takes the time as seed every time
|
|
|
|
|
See here.
"The random number generation starts from a seed value. If the same seed is used repeatedly, the same series of numbers is generated. One way to produce different sequences is to make the seed value time-dependent, thereby producing a different series with each new instance of Random.
To improve performance, create one Random to generate many random numbers over time, instead of repeatedly creating a new Random to generate one random number."
Me, I'm dishonest. And a dishonest man you can always trust to be dishonest. Honestly. It's the honest ones you want to watch out for...
|
|
|
|
|
|
Thanks,
I have the code block tag understood now. It really is more readable. Sorry.
I have tried instantiation of one Random instance as a class member in the button click event that starts the BackGroundWorkers. I place the instant creation just before the loop that creates the BGWs. When I use this approach the application either returns a set of results containing blocks of zeros mixed with blocks of identical times or it hangs.
Random rand;
private void button1_Click(object sender, EventArgs e)
{
rand=new Random();
for (int i = 1; i <= 40; i++)
{
BackgroundWorker Computation = new BackgroundWorker();
Computation.DoWork += new DoWorkEventHandler(Computation_DoWork);
Computation.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Computation_RunWorkerCompleted);
Computation.RunWorkerAsync();
}
}
private double timeOccurance2(string target)
{
string trial = string.Empty;
double intialTime = DateTime.Now.ToOADate();
int targetLength = target.Length;
while (trial != target)
{
StringBuilder b = new StringBuilder();
char[] ch = new char[target.Length];
for (int i = 0; i < target.Length; i++)
{
ch[i] = Convert.ToChar(Convert.ToInt32(Math.Floor(10 * rand.NextDouble() + 48)));
}
for (int i = 0; i < target.Length; i++)
{
b.Append(ch[i].ToString());
}
trial = b.ToString();
}
double finalTime = DateTime.Now.ToOADate();
return 24 * 60 * 60 * (finalTime - intialTime);
}
private void Computation_DoWork(object sender, DoWorkEventArgs e)
{
e.Result = timeOccurance2("124456").ToString();
}
private void Computation_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
richTextBox1.Text += (string)(e.Result);
richTextBox1.Text += (char)(10);
}
|
|
|
|
|
I did not read that. See my sig.
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 (one can edit an existing message!) [The QA section does it automatically now, I hope we soon get it on regular forums as well]
|
|
|
|
|
i have 2 user controls and a main form.
On form load, it will load usercontrol1. this is what i did.
public partial class main4 : Form<br />
{<br />
private UserControl1 userControl1 = new UserControl1();<br />
public main4()<br />
{<br />
InitializeComponent();<br />
this.Controls.Add(userControl1); <br />
} <br />
}
In userControl1, i've got a button that when clicked will navigate to userControl2.
My question is, how do i expose the button click event in userControl1 to the main form so that when the button is clicked, it will tell the main form to remove userControl1 and then add userControl2??
|
|
|
|
|
Well, your user control is private, so you'll have to implement a custom event that will reflect the button press back to the main form. Alternatively, just make the user control public, and then you can add an event to your mainform:
myMain4.userControl1.myButton.Click += ...
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
Handle the Button's Click event in the user control and raise your own event:
public partial class UserControl1 : UserControl
{
public event EventHandler Button1Click;
public UserControl1()
{
InitializeComponent();
button1.Click += new EventHandler(button1_Click);
}
void button1_Click(object sender, EventArgs e)
{
OnButton1Click(e);
}
protected virtual void OnButton1Click(EventArgs e)
{
EventHandler eh = Button1Click;
if (eh != null)
eh(this, e);
}
}
Now the new Button1Click event is available for the Form/Container and can be handled like any other event.
|
|
|
|
|
hey both. tks i shall try and get back again
|
|
|
|
|
Hi i was wondering if there is an easy way to loop through a list to find the differences and at the "positive" differences to add that item to the list but at the "negative" differences to remove it from the list?
Doing a clear() and then add all the items isn't going to cut it because the list is databound to a DataGrid and it resets the selection
Thanks in advance and sorry my bad English gramma.
|
|
|
|
|
You could derive a new class like so:
public class MyListOfObjects : List<MyObject>{}
and then add a method like so:
public void AddUnique(MyObject obj)
which would see if the list contains obj , and if not, then add it.
At that point you can just loop through your new list and call the databound list's AddUnique method with each item in the new list.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
Thats just like doing a if(!list.contains(myobject)) { list.add(myobject); } just in a nice package and it also only solves the adding, not the removing which imo is the biggest problem because all the times the list have to be looped through starts to grow exponentially...
|
|
|
|
|
0) You gave us precious little info to work with here
1) You wanted a method to do it, I gave you one. Since I didn't know what you were working with (there was no real indication that you're working with intrinsic types), I couldn't recommend using a hash set. In fact, you talked about binding in a grid, so I presumed a complex object.
2) I suppose you could use Linq, but I'm not so sure that it would be any faster, and even if it was, I would still be prompted to do the list class override and pass the new list into it if for no other reason than to abstract the functionality out of the code that's using it, and into the code that should do it.
3) In the time we've been discussing it, you could have tried both ideas to see which one works best for you.
4) I wish the List object had a Merge method that allowed us to optionally omit non-unique objects.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
0 and 1) true, i'll try to explain a little better:
I have a base object that 4 other class inherit from, in the base object class there is a uinque guid(a ulong) that never will be different from object to object.
2) i've had a look at linq but i could only find a way to find equal objects
3) i already have a working solution but it's not fast enough, what it does is to put all the objects from the reading function into a new list, then a foreach that does a if(!oldList.contains(currentObject)) where i then add it, after that i do the same but with switched roles of the new and old list and add the guid of those to be removed to a new list because i can't modify a collection while enumerating and finally remove the objects from the oldList
and oh btw i the information my objects are read from another applications(well game) memory
Sorry if my previous post sounded rude because that wasn't my intention
|
|
|
|
|
Two options(for faster solutions):
1) you could use a dictionary<key, value> => extremely fast but you can only search(fast) based on the key. And some more memory required(for the keys)
2) Implement IComparable and use something like:
int idx = oldList.BinarySearch(currentObject);
if(idx<0)
|
|
|
|
|
actually i did use a dictionary but changed it to a list for various reasons, anyway i think i'll try to make a reverse for loop like this:
for(int i = GetLengthOfLongestList(); i>0; i--)
{
try
{
var currentNewObject=newList[i];
}
catch
{
}
and a
try
{
var currentOldObject=oldList[i];
}
catch
{
}
/after checks
oldList.removeAt(i);
oldList.add(currentNewObject);
}
}
modified on Wednesday, February 3, 2010 10:46 AM
|
|
|
|