|
Kwagga wrote: I can't change this to ReadLine.
Why not?
Most commands are blocking; i.e., execution is halted until the instruction is completed. ReadToEnd() reads the *entire* file, before moving on to the next instruction. This will take a lot of time if you got a large file - in some cases longer than a minute. If Windows' doesn't get any messages from your application, it will assume that your either a dead application, or an application that should be dead. The so much feared,
"XXX has become unresponsive".
Instructions should do their thing fast, and move on. Longer-running processes must give a signal to Windows, just to prove that the application is still actually doing processing, instead of just "hanging dead in some loop".
Windows wants to kill your app, because you didn't phone home from work. So, the right way would be to make that call, whilst working. Now, the problem with ReadToEnd is that you can't stop it and tell it to call Windows.. So, that's something you'll have to build yourself.
string holdLine = string.Empty;
string output = string.Empty;
while (!sr.EndOfStream)
{
holdLine += sr.ReadLine();
Application.DoEvents();
if (String.IsNullOrEmpty(holdLine))
continue;
output += Regex.Replace(holdLine, "\r(?!\n)", " ");
}
sw.WriteLine(output);
sr.Close();
The Streamer now reads all lines as separate lines, does it's work on it,
and appends it to the output-string. The entire outputstring is written to disk when the looping is done.
The above code wasn't tested, but should be enough to get you on track. The Application.DoEvents is there to let Windows' know that your application has not gone James Brown.
You also might want to consider moving this logic to a background-worker. That's going to take more time to research, but once you know how it works, you start re-using the pattern more often. Well worth the investment in time, I daresay
Hope this helps,
I are troll
|
|
|
|
|
I have 30 buttons and I am trying to implement context menu on each button click by using following method.
void OnButtonClick(object sender, EventArgs e)
{
Button btn = (Button)sender;
btn.ContextMenuStrip.Show(btn, new System.Drawing.Point(0, btn.Height));
}
I have 2 context menu strip items, Add text Label and Add colour
private void addTextLabelToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (Control item in panel2.Controls)
{
Button btn = (Button)item;
if (string.IsNullOrEmpty(btn.Text.Trim()))
{
frmAddText form = new frmAddText();
form.ShowDialog();
if (frmAddText.IsTextMod)
{
return;
}
btn.Text = form.TextInfo;
btn.Tag = 0;
} } }
Wen i try to do this it is implementing on last button itself. Here Ia m returning
Here foreach is not acceptable.
private void addColourToolStripMenuItem_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
DialogResult dResult = colorDialog1.ShowDialog();
foreach (Control item in panel2.Controls)
{
Button btn = (Button)item;
string strBtnName = btn.Name;
if (string.Equals(btn.Name, strBtnName, StringComparison.OrdinalIgnoreCase))
{
if (dResult == DialogResult.OK)
btn.BackColor = colorDialog1.Color;
} } }
It is applying color to all buttos. But it should apply for only specific clicked button. Here foreach is not acceptable.
|
|
|
|
|
Udayaraju wrote: string strBtnName = btn.Name;
if (string.Equals(btn.Name, strBtnName, StringComparison.OrdinalIgnoreCase))
{
this condition will always be True and all button will be colored.
Edit - you need to refactor your code. You can get the button on whom the contextmenu click happpened rather than looping through all controls.
Button btn = (sender as ContextMenuStrip).SourceControl as Button;
|
|
|
|
|
There may be a quicker way of doing it, but the code below works.
private void testToolStripMenuItem_Click(object sender, EventArgs e)
{
if(sender is ToolStripMenuItem)
{
ToolStripMenuItem clickedItem = (ToolStripMenuItem)sender;
ContextMenuStrip strip = (ContextMenuStrip)clickedItem.Owner;
if (strip.SourceControl is Button)
{
Button button = (Button)strip.SourceControl;
button.BackColor = Color.Blue;
}
}
}
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
|
You're welcome
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
How to call a from from another call in windows application with c#
|
|
|
|
|
It would help to write a more explanable header to your post.
And trying to understand your query, you want to invoke a FORM ??
YourForm frm = new YourForm();
frm.Show();
Hope you can build on the above code.
|
|
|
|
|
Hi,
I want to create a simple function(only) with input and output parameters in pl/sql. I want the way of that function execution in c#.net.
Pls help. Thanx in advance.
|
|
|
|
|
I'm writting an application that displays data in a rich text box control by
adding a each line that I read in from a file to the control.
at the end of each line I add in a "\n" for a line feed.
the odd thing is that the leading numbers on the line end up at the end of the line just before the line feed.
so the file reads as...
1913 Name Address more text=============
and when I add it to the rich text box text feild I get...
Name Address more text============= 1913
and each line is like that.
My code is simply...
<code segment>
String Temp ="1913 Name Address more text=============";
RichTextBox_control.Text += Temp +"\n";
</code segment>
The odd thing is that when I look at the contents of the RTBcontrol while in debug mode, it looks correct, but it always displays the numbers at the end of the line ie: incorrectly.
Also if I add a letter to the front of the string then the entire string gets displayed correctly
IE:
<code segment>
String Temp ="1913 Name Address more text=============";
RTB.Text += "a " + Temp + "\n";
</code segment>
results in following being displayed in the box...
a 1913 Name Address more text=============
What gives? Is there a control code I can put in the text to make it display correctly or am I missing something that's just under my nose?
Thanks,
Will
|
|
|
|
|
Hi,
welcome to CodeProject.
I haven't seen such behavior yet; 3 suggestions:
- use Visual Designer to add a new RTB and check whether that behaves like that too;
- are you involved in right-to-left stuff, did you touch RTB.RtlTranslateContent or something similar?
- show all the RTB initialization code
BTW: to show multiple code lines, use the "code block" button below the edit window, it inserts
PRE tags providing background color and a monospaced font, and preserving formatting.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Hi Guys,
I have created a WindowApplication based project for my client and it works fine for him. my client want to sell this product to further clients, so for this he wants that in deployment of the project there should be 1 license key/product key assigned for every installation package of the project.. can somebody teach me or help me on how to incorporate in my project this License Key/Product Key and check if it is valid or not valid during installation.
If any one help me then I will be highly thankful to that person.
I am working in C# Visual Studio 2008
|
|
|
|
|
Hi
You can do that when the key evry hour change.
And the code is in a mathematic cryptical logic Exemple:
Code =c
Day = d
Hour = h
Year = y
c = ((y+d)*(c+h)5):5 + 2423
Your program must save the Registration time and you can send the crypted time to your client and your program compare the registration time with the crypted registration time.
I use this methode
DJ-Boris
|
|
|
|
|
thx for the reply sir Boris.
can u give me idea how to do this logic. because what my client wants is
he's the one who will give or produce the "KEY" and distribute it to
their client also.
if im going to do the logic you give, do i have to save the registration time in my project? or it's just compare it to the crypted time.?
about the registration time how to do this.?
thx for the reply..
|
|
|
|
|
If you are willing to try a commercial licensing solution then check Ellipter
It creates short product keys with embedded product info and/or expiration time handled automatically.
Also it has a GUI based serials generator which can be used by your client to create serials.
|
|
|
|
|
I have a problem with slow application startup due to security issues. I have tried ngen and profiled application twice but without any improvement. Still the biggest problem is in security and "unmanaged world" when .NET loads and calls methods like
<br />
PermissionSet.DecodeXml - takes enormous time to complete
CodeSecurityAccess.Demand internaly calls AppDomain.SerializeObject and runs CrossAppDomainSerializer (why when I have only ONE AppDomain?)
note: Assemblies have
[assembly: SecurityPermission(SecurityAction.RequestMinimum, UnmanagedCode=true, SkipVerification = true, Execution=true)]<br />
[assembly: UIPermission(SecurityAction.RequestMinimum, Unrestricted = true)]<br />
And I also suspect AuthentiCode and another unmanaged world issues to be a problem. Is there a tool or a white paper to figure it out?
Thanks for help.
Wizard_01
|
|
|
|
|
Hi,
I'm just sick of this lengthy switch case drama and I have a feeling there are already better solutions out there, if not, please help me with a creative way you may have used once to make it easier and shorter..
Many thanks for sharing guys!
All generalizations are wrong, including this one!
(\ /)
(O.o)
(><)
|
|
|
|
|
You have a few options... it depends on the situation, really.
One possibility is to take the bodies of the case statements and make methods out of them.
Another is to create a dictionary of key to delegate, and do a dictionary lookup/invoke in place of a switch.
Another is to look for an object-oriented way where you differentiate behavior by overriding an abstract/virtual method, in place of a switch statement somewhere.
All depends, though.
|
|
|
|
|
Thanks, anyway to do it with Enum s you know??
All generalizations are wrong, including this one!
(\ /)
(O.o)
(><)
|
|
|
|
|
Yes, it should work with enums.
Again, though, it really depends a lot on what you're trying to do, so it's hard to recommend one approach over another... but here's a sample of some different techniques. Each have their strengths and weaknesses, depending on your situation.
Assuming an enum like:
enum MyEnum { Foo, Bar, Baz }
switch way:
bool method(MyEnum val)
{
switch (val)
{
case MyEnum.Foo:
Console.WriteLine("Some");
Console.WriteLine("big");
Console.WriteLine("case");
Console.WriteLine("statement");
Console.WriteLine("that");
Console.WriteLine("Foo");
Console.WriteLine("does");
return true;
case MyEnum.Bar:
throw new Exception("Bar!");
case MyEnum.Baz:
Console.Beep();
Console.WriteLine("Beep.");
return false;
default:
throw new ArgumentOutOfRangeException("val");
}
}
Refactor to methods way:
bool method(MyEnum val)
{
switch (val)
{
case MyEnum.Foo:
return FooMethod();
case MyEnum.Bar:
return BarMethod();
case MyEnum.Baz:
return BazMethod();
default:
throw new ArgumentOutOfRangeException("val");
}
}
bool FooMethod()
{
Console.WriteLine("Some");
Console.WriteLine("big");
Console.WriteLine("case");
Console.WriteLine("statement");
Console.WriteLine("that");
Console.WriteLine("Foo");
Console.WriteLine("does");
return true;
}
bool BarMethod()
{
throw new Exception("Bar!");
}
bool BazMethod()
{
Console.Beep();
Console.WriteLine("Beep.");
return false;
}
Dictionary way (.Invoke() is just for clarity):
delegate bool BoolFunc();
readonly Dictionary<MyEnum, BoolFunc> methods;
MyClassConstructor()
{
methods = new Dictionary<MyEnum, BoolFunc>();
methods.Add(MyEnum.Foo, FooMethod);
methods.Add(MyEnum.Bar, BarMethod);
methods.Add(MyEnum.Baz, BazMethod);
}
bool method(MyEnum val)
{
BoolFunc funcToCall;
if (!methods.TryGetValue(val, out funcToCall))
throw new ArgumentOutOfRangeException("val");
return funcToCall.Invoke();
}
bool FooMethod()
{
Console.WriteLine("Some");
Console.WriteLine("big");
Console.WriteLine("case");
Console.WriteLine("statement");
Console.WriteLine("that");
Console.WriteLine("Foo");
Console.WriteLine("does");
return true;
}
bool BarMethod()
{
throw new Exception("Bar!");
}
bool BazMethod()
{
Console.Beep();
Console.WriteLine("Beep.");
return false;
}
Subclass/override way (the enum actually goes away here):
bool method(MyNonEnumClass val)
{
return val.DoYourThing();
}
abstract class MyNonEnumClass
{
public abstract bool DoYourThing();
}
class FooClass : MyNonEnumClass
{
public override bool DoYourThing()
{
Console.WriteLine("Some");
Console.WriteLine("big");
Console.WriteLine("case");
Console.WriteLine("statement");
Console.WriteLine("that");
Console.WriteLine("Foo");
Console.WriteLine("does");
return true;
}
}
class BarClass : MyNonEnumClass
{
public override bool DoYourThing()
{
throw new Exception("Bar!");
}
}
class BazClass : MyNonEnumClass
{
public override bool DoYourThing()
{
Console.Beep();
Console.WriteLine("Beep.");
return false;
}
}
Hope this helps ease your switch/case pain!
modified on Wednesday, February 11, 2009 5:21 PM
|
|
|
|
|
There's an even easier way. Take a look at my post below - it's as easy as this.
|
|
|
|
|
Thanks... I did write that way under Dictionary way already, though.
|
|
|
|
|
Wow, thanks akidan!
I think I will go with the last one.
All generalizations are wrong, including this one!
(\ /)
(O.o)
(><)
|
|
|
|
|
My pleasure! Happy I could help.
|
|
|
|
|
Why would you like to throw away the switch case?
This is a great way to code statemachines, for instance when you've got a status.
Or you use if else, but is like the same as switch case.
It really depends on what is it when you would like to get rid of the switch case. It depends on the situation. Show a situation and we'll give the solution.
|
|
|
|