|
You might want to phrase it as an obvious question next time; as soon as I read "One cool trick is this", I assumed you where sharing a tip and skipped reading the rest.
You're looking for the filehelpers[^] library. An alternative would be using OleDb[^].
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Let's look at a simple example: I want to create a Button class with a different BackColor and Size than the "normal" button (.Net 4, Windows Forms). Looks easy, doesnt't it? But I face some problems.
A simple "solution" would be:
public class BernieButton : Button
..
public BernieButton()
{
this.BackColor = Color.Green;
this.Size = new Size(150,50);
}
Compile the project, drag a BernieButton onto Form1, and everything looks fine.
But now Sales decides that the BackCOlor must be yellow, and the size is too big. OK, change that in the constructor, test the new button, looks fine. Rebuild the old projects, and - the buttons there look like before. Failure!
How could that fail? When the button was dragged onto the form, the Designer added some lines in the InitializeComponent method of the form:
this.BernieButton1.BackColor = Color.Green;
this.ernieButton1.Size = new Size(150,50);
and that code gets executed after the constructor of the button, thus overriding the recent changes of the default value.
I tried overriding the properties:
[DefaultValue(typeof(Color), "Green")]
public override Color BackColor
{
get { return base.BackColor; }
set { base.BackColor = value; }
}
and then set the default value in the constructor (using reflection, thus circumventing double storage of the new default value). That really works as it is intended, the designer does not write a line for BackColor in the InitializeComponent method.
But it does not work with Size. There is an important difference: BackColor is "virtual " in the Button class, Size is not.
I tried with the "new " keyword, but it fails: a line is added to InitializeComponent , and later changes of the default value are ignored.
I thought of setting the DefaultValueAttribute of the properties in the constructor. But that is not possible, attributes are read-only at run-time.
Do you have some ideas on how to overcome these limitations with inherited non-virtual properties?
|
|
|
|
|
There is a property DefaultSize that you can override. This works for me:
public class MyButton : Button
{
private static readonly Size SizeDefault = new Size(100, 50);
protected override Size DefaultSize
{
get { return SizeDefault; }
}
}
|
|
|
|
|
Sorry, it does not:
Drag the button onto a form, and it looks OK. Then change the DefaultSize, e.g.
private static readonly Size SizeDefault = new Size(200, 100); , rebuild your solution, and the button has still the old size. Only new buttons get the new default size, "old" buttons do not get updated.
Again, a look into Form1.Designer.cs shows the reason:
this.myButton1.Size = new System.Drawing.Size(100, 50); The key to the solution is (likely) in preventing the designer from writing that line - as with the BackColor property.
But a
public bool ShouldSerializeSize() does not help either.
And
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new Size Size
{
get { return base.Size; }
set { base.Size = value; }
} prevents individually sized buttons.
|
|
|
|
|
Hmmm, I see what you're getting at now. It seems this is the case with all controls so maybe it's something coded into System.Windows.Forms.Control ?
I don't have reflector here, but I will have a poke around later and have a look in the source code...
|
|
|
|
|
I have written a simple and minimalist HTTP proxy server that runs on command line. In the Start() method, a TcpListener blocks until it gets a client request and creates a new thread (ThreadHandleClient method) that processes this client, fetches its url and relays data.
The trouble is in the relay logic that I want to refine. In the first inner loop, I receive all webserver data and send it to client until zero bytes are left. In second inner loop, I do vice-versa: receive all client data and send to web-server until zero bytes left. However, the code gets blocked in the beginning of second inner-loop at client.receive(). This is what I'm basically doing:
Any help will be truly appreciated. Thanks in advance.
public void Start(IPAddress ip, int port)
{
try
{
TcpListener listener = new TcpListener(ip, port);
listener.Start(100);
while (!stopFlag)
{
Socket client = listener.AcceptSocket();
IPEndPoint rep = (IPEndPoint)client.RemoteEndPoint;
Thread th = new Thread(ThreadHandleClient);
th.Start(client);
}
listener.Stop();
}
catch (Exception ex)
{
Debug.Print("START: " + ex.Message);
}
}
public void ThreadHandleClient(object o)
{
try
{
Socket client = (Socket)o;
NetworkStream ns = new NetworkStream(client);
byte[] buffer = new byte[2048];
int rec = 0, sent = 0, transferred = 0, rport = 0;
string data = "";
do
{
rec = ns.Read(buffer, 0, buffer.Length);
data += Encoding.ASCII.GetString(buffer, 0, rec);
} while (rec == buffer.Length);
string line = data.Replace("\r\n", "\n").Split(new string[] { "\n" }, StringSplitOptions.None)[0];
Uri uri = new Uri(line.Split(new string[] { " " }, StringSplitOptions.None)[1]);
Debug.Print("CLIENT REQUEST RECEIVED: " + uri.OriginalString);
if (uri.Scheme == "https")
{
rport = 443;
Debug.Print("HTTPS - 443");
}
else
{
rport = 80;
Debug.Print("HTTP - 443");
}
IPHostEntry rh = Dns.GetHostEntry(uri.Host);
Socket webserver = new Socket(rh.AddressList[0].AddressFamily, SocketType.Stream, ProtocolType.IP);
webserver.Connect(new IPEndPoint(rh.AddressList[0], rport));
byte[] databytes = Encoding.ASCII.GetBytes(data);
webserver.Send(databytes, databytes.Length, SocketFlags.None);
Debug.Print("SENT TO SERVER. WILL NOW RELAY: " + data);
buffer = new byte[2048];
rec = 0;
data = "";
do
{
transferred = 0;
do
{
rec = webserver.Receive(buffer, buffer.Length, SocketFlags.None);
Debug.Print("RECEIVED FROM WEBSERVER[" + rec.ToString() + "]: " + Encoding.ASCII.GetString(buffer, 0, rec));
sent = client.Send(buffer, rec, SocketFlags.None);
Debug.Print("SENT TO CLIENT[" + sent.ToString() + "]: " + Encoding.ASCII.GetString(buffer, 0, rec));
transferred += rec;
} while (rec == buffer.Length);
Debug.Print("loop-1 finished");
if (transferred == 0)
break;
transferred = 0;
do
{
rec = client.Receive(buffer, buffer.Length, SocketFlags.None);
Debug.Print("RECEIVED FROM CLIENT: " + Encoding.ASCII.GetString(buffer, 0, rec));
sent = webserver.Send(buffer, rec, SocketFlags.None);
Debug.Print("SENT TO WEBSERVER[" + sent.ToString() + "]: " + Encoding.ASCII.GetString(buffer, 0, rec));
transferred += rec;
} while (rec == buffer.Length);
Debug.Print("loop-2 finished");
} while (transferred > 0);
Debug.Print("LOOP ENDS. EXITING THREAD");
}
catch (Exception ex)
{
Debug.Print("Error occured: " + ex.Message);
}
|
|
|
|
|
If no data has arrived, then Receive is going to block. If this is not what you want, then use BeginReceive .
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Thanks Richard. For reasons of simplicity, I don't want to create anync calls and handle everything in thant one thread only. Is there no way to do this using the sync Receive() method ?
|
|
|
|
|
If you want, you could call Receive on a background worker thread, but that would entail multiple threads.
The bottom line is that Receive will behave synchronously, meaning it will block when there is no data.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Is there a way to check the data in the socket and call Receive() only when data is available for receiving. This way it should not block, right?
|
|
|
|
|
I see that Socket has a method called Poll . Take a look:
Socket.Poll[^]
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Thanks. The Poll() method really helped. I'm now polling for three seconds before receiving for both client and the server. However, there is still some logical issue with my code:
if (client.Poll(3000 * 1000, SelectMode.SelectRead))
{
rec = client.Receive(buffer, buffer.Length, SocketFlags.None);
Debug.Print("RECEIVED FROM CLIENT: " + Encoding.ASCII.GetString(buffer, 0, rec));
sent = webserver.Send(buffer, rec, SocketFlags.None);
Debug.Print("SENT TO WEBSERVER[" + sent.ToString() + "]: " + Encoding.ASCII.GetString(buffer, 0, rec));
transferred += rec;
}
else
{
Debug.Print("No data polled from client");
}
As I said, one logical fault still remains. In the proxy client machine, I'm able to open google.com. Then performed a search that also went fine. However, when I click on a search result, the proxy again gives me google.com!! What am I missing?
|
|
|
|
|
If i want a simple server to client communication as a background service on windows Os, that would only send ISP infor to a hosted domain, WHERE would i start? C# or VB.net or other? Which has an example or godd start?
Experience is the best teacher
|
|
|
|
|
kipchirchir wrote: WHERE would i start?
By explaining that in terms we can understand?
kipchirchir wrote: C# or VB.net or other?
Whichever you know and are comfortable with.
VB and C# are interchangeable, pretty much, anything you can do in one, you can do in the other.
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
See my answer in the .NET forum; BTW, please do not post the same question in multiple forums.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
If I do something like this
XElement root = new XElement("Root",
new XAttribute("Att1", "a"),
new XAttribute("Att2", "b"),
new XAttribute("Att3", "c"));
XDocument document = new XDocument(root);
document.Save("file.xml");
Instead of it writing
Root Att1="a" Att2="b" Att3="c"/
it writes Root Att3="c" Att2="b" Att1="a"/
Even in debugging mode when I look at the XML contents of document it appears to be in the proper order, but when it writes to file it reverses them. Can anyone tell me why it is doing this?
|
|
|
|
|
Efficiency, I guess. Does the order matter?
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Considering XML doesn't give a rats ass about the order of attributes, what's the problem?
|
|
|
|
|
Nukeman33 wrote: Can anyone tell me why it is doing this?
You have a bug either in implementation and/or requirements if you think that the order matters.
|
|
|
|
|
The logic behind writing to the file may be the culprit.
I guess if the order is not important to you, just ignore it!
|
|
|
|
|
|
"Hello" is hardly a question; I can see the question in the subject-line, but it would not have cost you that much time to repeat it in the body and add the code that you're currently working on.
Now, back to the problem, why would you need "multiple" timers?
Imagine you are cooking, and you need to time the eggs (7 minutes) and the pudding (10 minutes). That means you'd use two people, one to warn you after 7 minutes, one after 10 minutes.
Alternatively, you could ask a single person to look at his watch every minute and have him poke you on both the 7-minute marker and the 10-minute marker. That would save the overhead of having to feed another person; or create another object
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Hi,
How i can get all instance in special IP in my network.
I have ip address 192.168.0.2 and wanna get all sql instance .
Thanks in advanced!
|
|
|
|
|
Check out SqlDataSourceEnumerator.Instance.GetDataSources method. You can also have a look at this[^] article.
"Fear no factor", Prime Numbers.
|
|
|
|
|
thanks in advanced !
But no use ! Only return SQL Instance of some Ip address. I have 10 Ip(10Pc) but this only return just 1 of them!
What is the solution?
And One more thing.
Some of my SQL Instance doesnt have user/Pass . i mean they are windows authentication(Integrated Security=true) in some IPs.
|
|
|
|
|