C# in Depth: Random numbers. When you see the word "random" in a question title on Stack Overflow you can almost guarantee it will be the same fundamental problem as countless similar questions. This article takes a look at why randomness causes so many problems, and how to address them. The problem The Stack Overflow (or newsgroup, or mailing list etc) question usually goes something like this: I'm using Random.Next to generate random numbers, but it keeps giving me the same number. It changes from run to run, but each time it will generate the same number lots of times. This is caused by code like this: for (int i = 0; i < 100; i++) { Console.WriteLine(GenerateDigit()); } ...static int GenerateDigit() { Random rng = new Random(); return rng.Next(10); } So, what's going wrong here? The explanation The Random class is not a true random number generator.
All of this is deterministic. So what was wrong in our example code? What can we do about it? Use a cryptographic random number generator Compare this with Random. BackgroundWorker Tutorial. BackgroundWorker makes threads easy to implement in Windows Forms. Intensive tasks need to be done on another thread so the UI does not freeze. It is necessary to post messages and update the user interface when the task is done. Threads Steps In the image, you see the Visual Studio designer view. The fly-out panel is the Toolbox—it contains links for all kinds of controls. It also contains the BackgroundWorker link, which we can use in the following steps. First, click on BackgroundWorker. Second:Highlight backgroundWorker1. Third:Look for the lightning bolt.You will see a lightning bolt icon in the Properties pane.This is Microsoft's icon for events.
Tip:BackgroundWorker is event-driven, so this is where we will make the necessary events to use it. Then, double-click on DoWork. The rectangles show the tray at the bottom containing the backgroundWorker1 icon, and the Properties panel on the right. Tip:This UI is far better than trying to type the methods in manually. DoWork Sleep Properties. Irony - .NET Language Implementation Kit. Keeping your UI Responsive and the Dangers of Application.DoEvents - jfo's coding. What’s the difference between Application.Run and Application.DoEvents? Application.Run continually processes window messages for your application, raising events as necessary.
Application.DoEvents processes all the window messages in the queue until it has run out of messages, then returns. Making your CPU work too hard Given this definition, one could conceivably suggest that Application.Run could be replaced with: while(! Quit) { Application.DoEvents();} Unfortunately, when you run the application, switching over to the while loop spikes the CPU to 100% utilization.
Why? Adding new codepaths that might be unhandled Let’s take the example where you add a whole bunch of items to your listbox in a button click event. Private void button1_Click(object sender, System.EventArgs e) { for (int i = 0; i < 10000; i++) { listBox1.Items.Add(i.ToString()); Application.DoEvents(); // <-- Calling DoEvents processes other events that may occur } this.Controls[0].Text = "Done"; } The codepath when it works:
Introduction to Neural Networks for C# Callback to C# from Unmanaged Fortran. Using unmanaged code eg Fortran to callback to C# can be a nightmare to get right. But once you know it, it is just following a recipe. Hence without further explanation, an example and recipe is given below. It is quite self-expalnatory I hope. Just note that, the example is more than a simple call back. --- Fortran code -----module f90Callback contains subroutine func1(iArr, progressCllBak) ! ! ! Return end subroutine func1 end module f90Callback ------- C# code --------- using System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices; namespace CScallbackDriver{ class Program { // 0. // 1. // 2. Public Program() { // 3. // 4. Int iArg; static void Main(string[] args) { Program myProg = new Program(); myProg.localCounter = 0; int[] iArrB = new int[2]; //6.
Console.WriteLine("Retrieve callback value as {0}", myProg.localCounter); Console.ReadKey(); } // 5. C# in Depth: C# Language Specifications. The C# language has two major sources of specifications: ECMA and Microsoft. The ECMA specification only goes as far as C# 2.0, even though it's the fourth edition, confusingly enough. This page is a collection of all the versions I've found - but it's quite possible that some will move around, so please let me know if you have any problems, or find any more. Microsoft specifications C# 5.0 (2012; Word document) (The C# 4.0 specification is currently missing in action, so to speak - the previous link has been replaced by the 5.0 spec.) C# 3.0 (unified, 2007; Word document)C# 2.0 (September 2005; Word document)C# 1.2 (2003; Word document)C# 1.0 (2002; Word document) If you have Visual Studio 2012 installed, you can also find the C# 5.0 specification under the installation directory (e.g.
"C:\Program Files (x86)\Microsoft Visual Studio 11.0") in a subdirectory of "VC#\Specifications\1033". ECMA specifications Annotated specifications. How do I access controls on a different form within a VC++ .NET application? Hi SiSteve, This is because frmFind need to include “frmMain.h” to keep the instance of the frmMain. It becomes cyclic including in C++. The workaround is when you create and show the frmFind, pass the controls to frmFind from frmMain instead of whole form, in this case, the RichTextBox. The following is the sample snipping about Form1 pass a textbox control to Form2, Form2 modify the textbox by a mouse click. // Form1.h #include "Form2.h" private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { Form2 ^ frm2 =gcnew Form2(); frm2->setTextBox(this->textBox1); // pass textBox to Form2; frm2->Show(); //Form2.h (Doesn't need to include Form1.h) public: TextBox ^tBox; public: System::Void setTextBox(TextBox ^t){ this->tBox = t; tBox->Text = "Hello World"; If you have any concern, please let me know.
Cheers, Yi Please remember to mark the replies as answers if they help and unmark them if they provide no help.Welcome to the All-In-One Code Framework!