|
To add to what Colin said (since a NullReferenceException is the most common exception - and the easiest to prevent!), putting a try-catch around Application.Run doesn't help unless an exception causes the main UI thread to abort, and most won't; it is in your case, which means it's occuring in the message pump, or application thread queue.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I have a problem with the richtextbox. I'd like to read a logfile, then reverse it. This did not seem to be possible with LoadFile so I read the file into an arraylist and outputs it to the richtextbox with AppendText. With this solution it will also be quite easy to add output filters.
The problem is that there seems to be problems with number of lines the richtextbox can hold. If I output 2000 lines it takes approx. 5s before it's done, 10000 lines I waited about 1 minute and it was still not finished. The logfiles will contain much more than 10000 lines so this is a big problem. Using LoadFile it takes no time at all. So something's very wrong with my solution, anyone that can hint to me what's wrong, and maybe suggest a better approach to this.
Since all files starts with the date I added a check that they e´start with 2004 otherwise I had >18000 lines in a logfile with 15000 lines. This seems to be me as one possible problem is that the logfile contains some "forbidden" characters that richtextbox cannot handle. Still LoadFile loads the file without problems.
private void button1_Click(object sender, System.EventArgs e)
{
StreamReader sr = new StreamReader("c:\\logfile.txt");
//StreamReader sr = File.OpenText("c:\\logfile.txt");
string input = null;
//Should read from the bottom, doesn't seem to work with LoadFile...
//richTextBox1.LoadFile("c:\\logfile.txt",RichTextBoxStreamType.PlainText);
ArrayList myList = new ArrayList();
int linecnt = 0;
while ((input = sr.ReadLine()) != null)
{
if ( input.StartsWith("2"))
{
myList.Add(input);
//linecnt++;
}
}
sr.Close();
myList.TrimToSize();
myList.Reverse();
richTextBox1.Clear();
richTextBox1.Enabled = false;
int q = myList.Count;
//richTextBox1.Text = myList.Count.ToString() + "\t" + linecnt.ToString() + "\n";
for (int i=0;i < myList.Count ;i++)
{
richTextBox1.AppendText(myList[i].ToString() + "\n");
}
richTextBox1.Update();
richTextBox1.Enabled = true;
}
|
|
|
|
|
Looking into it more it's seems to me that it works as intened but as soon as the loop where the text is appended to the richtextbox exceeds 2-3000 it will take several minutes.
Is there any better way to add text to the control than AppendText? I'd like to stick with richtextbox since I'm thinking of highlighting some lines.
|
|
|
|
|
The problem lies with how your building the string that is in the RichTextBox. Since Strings are immutable in the .NET Framework, every time you use .AppendText you're actually making a copy of the string in the .Text property, and appending the .AppendText string to it, and then killing off the old string and setting the .Text property to the new one. Do this 3,000 times and the GC has to go back and clean up ALOT of orphaned String objects. This probelm will continue to get worse and worse until the GC catches up to what your doing. Also, the RTB will repaint itself every time the .Text property changes.
The work around for this is to use a StringBuilder object to build the complete string BEFORE you send it to the RichTextBox. You can use the .Capacity property, or the .EnsureCapacity method to setup the StringBuilder with the size of the log file before you start appending text to it. This way, you'll once again avoid excessive allocations as your adding your lines to it.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Using stringbuilder did the trick. Thanx a lot.
|
|
|
|
|
here are a few possible adjustments you can make to your code.
string reader stuff blah blah blah
while ((input=sr.RealLine()) != null)
{
if (input.StartsWith("2"))
{
myList.Add(input);
}
}
sr.Close();
richTextBox1.Clear();
System.Text.StringBuilder data = new StringBuilder(myList.Count*avgLen);
for (int i=myList.Count; i>=0; i--)
{
msg.Append(myList[i]);
msg.Append(System.Environment.NewLine);
}
richTextBox1.Text = msg.ToString();
______________________________
The Tao gave birth to machine language.
Machine language gave birth to the assembler.
The assembler gave birth to ten thousand languages.
Each language has its purpose, however humble.
Each language expresses the Yin and Yang of software.
Each language has its place within the Tao.
Beauty exists because we give a name to C#.
Bad exists because we give a name to COBOL.
|
|
|
|
|
It's so strange that today when I compile some sample code on .net remoting, vs.net 2003 says System.Runtime.Remoting.Channels.Tcp does not exist! also,the .Http namespace does not exist.
I remembered I should have comiled and ran some examples contained Tcp or Http namespace.
Framework 1.1.4322, Win2K advanced server with sp4.
And I reinstalled VS .net 2003 but I takes no effect.
What's the problem? Any one had encountered?
Thanks.
|
|
|
|
|
|
fine. It was that very reason.
thanks.
fay
|
|
|
|
|
Hi,
I'd like to know how can I overload the '=' operator?
the standard '=' does not do what I want...
Best regards.
thanks.
There is no spoon.
|
|
|
|
|
an example
using System;
class Player
{
public int neili;
public int tili;
public int jingyan;
public int neili_r;
public int tili_r;
public Player()
{
neili = 10;
tili = 50;
jingyan = 0;
neili_r = 50;
tili_r = 50;
}
//
public static Player operator ++(Player p)
{
p.neili = p.neili + 50;
p.tili = p.tili + 50;
p.neili_r = p.neili;
p.tili_r = p.tili;
return p;
}
//
public static Player operator +(Player p1, Player p2)
{
Player p = new Player();
p.neili = p1.neili +p2.neili;
p.tili = p1.tili + p2.tili;
p.neili_r = p.neili;
p.tili_r = p.tili;
return p;
}
public static void Main()
{
Player p1 = new Player();
Player p2 = new Player();
p1++;
Console.WriteLine(" {0}", p1.neili);
Player p = p1 + p2;
Console.WriteLine(" {0}", p.neili);
}
}
fay
|
|
|
|
|
hi,
I need to overload the assignment operator (=) not the + or ++.
I want to replace this:
<br />
for (i=1; i < m_nHeight; i++)<br />
for (j=0; j < m_nWidth; j++)<br />
{<br />
m_board[i-1, j].IsSet=m_board[i, j].IsSet;<br />
m_board[i-1, j].BrushType=m_board[i, j].BrushType;<br />
m_board[i-1, j].ForegroundColor=m_board[i, j].ForegroundColor;<br />
m_board[i-1, j].BackgroundColor=m_board[i, j].BackgroundColor;<br />
m_board[i-1, j].LinearGradientMode=m_board[i, j].LinearGradientMode;<br />
m_board[i-1, j].HatchStyle=m_board[i, j].HatchStyle;<br />
};<br />
by simply this:
<br />
for (i=1; i < m_nHeight; i++)<br />
for (j=0; j < m_nWidth; j++)<br />
m_board[i-1, j]=m_board[i, j];<br />
when I use the standard '=' operator the standard assignment fails. that's why I would like to rewrite this operator.
There is no spoon.
|
|
|
|
|
You can't overload the '=' operator. You can overload these[^] operators.
As a workaround, you could write an Assign, or CopyObject, whatever you want to call it, method into your class that accomplishes the same thing.
for (i=0; i < m_nHeight; i++)
for (j=0; j < m_nWidth; j++)
m_board[i-1, j].Assign(m_board[i, j]);
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I was thinking abuot it, but it's not the way I want
I guess C# allows to overload '='...
There is no spoon.
|
|
|
|
|
bouli wrote:
I guess C# allows to overload '='
As the topic that Dave linked stated - straight from the MSDN documentation - the = operator cannot be overloaded whether you like it or not.
You can, however, implement a cast operator so that if you want to customize assignment from another type (say an int to your struct), you could actually do something like this:
MyStruct s = (MyStruct)1;
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi Heath!
ok, thanks,
the code will remain like that...
by the way delegates are very convinient :P I can combine them with DirectX
There is no spoon.
|
|
|
|
|
I did a windows usercontrol,when I put it to a webapplication, a error was happened what "System.Security.Permissions.FileIOPermission, mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 failed."
Because I had some file operations in my windows usercontrol. It may be something question of framework's config?
|
|
|
|
|
Hello jzb, I never saw this error but I know that wincontrol cannot get client machine information for security reasons. Maybe .NET thinks that your try to manipulate files isn't safe.
Good luck...
Wender Oliveira
.NET Programmer
|
|
|
|
|
This is not true at all. See my reply to the original post.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
You mean you embedded the Windows Forms UserControl in a web page? If you simply use it on the server, it won't matter.
If you embedded it in the web page (client-side), then the client's machine's code access security for .NET must be configured. Obviously you're using .NET 1.1 (since .NET 1.0 didn't grant applications in the Internet zone any permissions; thank you're truly as one of the people that asked Microsoft to change this in 1.0). You are only granted the following permissions, though:- Open File Dialog
- 10240 bytes of domain-isolated user storage
- Code execution
- Safe top-level windows
- Own clipboard
- Safe printing
So, you must add a code group to the user's machine. It is absolutely not recommended to chance the existing Internet_Zone code group, as this applies to all code run from the Internet, including that which you do not know and probably wouldn't trust.
Instead, add a code group. You can do this through a custom installer or running a command-line like so:
caspol.exe -m -ag All_Code -url http: Now, it's recommended you don't require FullTrust, but instead create a permission set with the privileges necessary. You can do this with XML files, custom actions, or other methods during installation.
For example, the GotDotNet Workspaces have a .NET UserControl that requires that you download a Windows Installer package (.msi; Visual Studio .NET can make these, as well as the Microsoft .NET Framework Configuration 1.1 snap-in, which is available in Administrative Tools). This installs a code group that allows the control to run. Restart Internet Explorer (since the CLR was already loaded by an unsuccessful attempt to load the control) and the control will load.
You can read more about code access security here on this site. Read Understanding Code Access Security[^]. You should also read the documentation in the .NET Framework SDK about code access security (although that article is comprehensive and very well-written, it's also good to hear straight from the horse's mouth). Read Securing Applications[^] and Configuring Security[^] in the .NET Framework SDK.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi gurus,
According to MS in the .NET Framework documentation, when they declare a delegate, they declare the parameters like that:
<br />
public delegate void AlarmEventHandler(object sender, AlarmEventArgs e);<br />
Is it an obligation that the parameter must be a derived class from the EventArgs class?
Are such declarations valid?
<br />
internal delegate void GameStatusChangedEventHandler(object sender, GameStatus gameStatus, int score, int level, int linesDone);<br />
<br />
internal delegate void LevelChangedEventHandler(object sender, int level);<br />
<br />
public delegate void ReceivedMessageEventHandler(object sender, String message);<br />
Should I create a derived EventArgs class for each delegates?
Thanks.
There is no spoon.
|
|
|
|
|
It's common to use the sender-eventargs-pattern, but it's not binding.
Also your custom eventargs doesn't have to be derived from the System.EventArgs class, but again this is a common approach.
www.troschuetz.de
|
|
|
|
|
ok, thanks.
There is no spoon.
|
|
|
|
|
I have a panel which is located in a AutoScroll-enabled panel. The
problem is when I click in the panel, the contining panel
automatically scrolls to position (0,0).
How can I get around this annoying behaviour?
|
|
|
|
|
i want to create a form and show it in a thread. Like this code:
Thread th = new Thread(new ThreadStart(t));
t.Start();
void t()
{
Form2 f2 = new Form2();
f2.Show();
}
-----------------------
But it doesnt work. How to deal with it ?
|
|
|
|
|