|
I'm not sure I follow you completely. All you need to do is use BeginRead once while your main thread of execution continues (to update an animated "wait" graphics or something). In your AsyncCallback you call EndRead as you're doing and you've call the data.
Take a look in the .NET Framework SDK documentation at the NetworkStream.BeginRead and EndRead methods. The two together form a complete solution (i.e., a callback is created in the docs for BeginRead that is defined in the example for EndRead ). What you have is similar, but the examples - and the documentation itself - may help you discover what the problem is.
Otherwise, please post your complete solution (minus any code that actually acts on the data or updates a UI, etc., for brevity's sake).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
O, you have helped me understand these methods better. But to clear things up in what im trying to achieve..........The bottom line is I want to be able to read data as it is sent down the stream over time.....Thus my program needs to be able to constantly monitor the stream in-order to react as it were when data(messages) are there to be read off.
So in my meain theard after the TCP connection has been set up I call a method which contains the following:
if(stream.CanRead)
{
byte[] myReadBuffer = new byte[2048];
stream.BeginRead(myReadBuffer, 0, myReadBuffer.Length, new AsyncCallback(myReadCallBack),stream);
}
After this is called the callback method is called (were the seperate thread executes)
The code in there looks like this:
public void myReadCallBack(IAsyncResult result)
{
NetworkStream str = (NetworkStream) result.AsyncState;
byte[] myReadBuffer = new byte[2048];
int numberOfBytesRead = 0;
numberOfBytesRead = str.EndRead(result);
myCompleteMessage = String.Concat(myCompleteMessage, Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
while(str.DataAvailable)
{
str.BeginRead(myReadBuffer, 0, myReadBuffer.Length, new AsyncCallback(myReadCallBack),stream);
}
if(stream.DataAvailable == false)
{
TokenizeCommands(myCompleteMessage);
myCompleteMessage = "";
str.BeginRead(myReadBuffer, 0, myReadBuffer.Length, new AsyncCallback(myReadCallBack),stream);
}
}
}
Thats the code. So im trying to achieve structure that will monitor data when it becomes available on the stream but in an iterative way so the main thread can continue to do other things but react when messages become available on the stream.
thanks for ya help, its difficult for me to explain as im a student to this, but im getting there
cheers
mIKE
|
|
|
|
|
just one more thing, when I insert buttons in my form and when I set KeyPreview to true, the left/right/up/down/space keys move the focus from controls to controls. How can I take the control of the left/right/up/down/space keys?
thanks.
There is no spoon.
|
|
|
|
|
In your OnKeyDown override in your form set KeyEventArgs.Handled to true for those keys you want to process yourself.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hi,
Even if I put the KeyEventArgs.Handled to true , the arrow and space bar keys are not processed. The OnKeyDown handler simply do the following:
private void OnKeyDown(object sender, System.Windows.Forms.KeyEventArgs e)<br />
{<br />
Debug.WriteLine("frmMain.OnKeyDown: "+e.KeyCode.ToString());<br />
e.Handled=true;<br />
}<br />
in the OnLoad event of the main form, I just do:
private void OnLoad(object sender, System.EventArgs e)<br />
{<br />
this.MinimumSize=this.Size;<br />
this.KeyPreview=true;<br />
}
Maybe I have done something wrong somewhere???
There is no spoon.
|
|
|
|
|
You dno't need to do that in the Load event handler - just set it in the constructor. The same is true for MinimizeSize . Windows Forms will remember settings if they are not applicable yet, and will apply those when the are applicable (like after the handle is created inside OnHandleCreated ).
If those don't work for you, override (and be sure to call the base implementation) ProcessDialogKey which does handle the arrows and a few others keys in addition to the other keys.
It's all documented in the .NET Framework SDK if you look at the members for Control (from which Form indirectly derives).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
yes, overriding ProcessDialogKey works fine! all keys are processed.
Is the ProcessDialogKey the equivalent of the MFC'sPreTranslateMessage (with trapping WM_KEYDOWN )?
There is no spoon.
|
|
|
|
|
Hello gurus,
There is a strange behaviour with Windows Forms... is it me or is it normal?
Here is what's happening:
When I create an empty form that handles the KeyDown event the event is triggered.
But, when I insert a control like my own control or a buttons etc., the KeyDown event is not triggered: I never enter in the KeyDown event...
Why does this happen?? I have done something wrong?
Thanks
Best regards.
There is no spoon.
|
|
|
|
|
When a control has the focus its message handler (reflected WndProc in managed code) is sent the message. You need to set Form.KeyPreview to true in order for the form to process keyboard messages for child controls. See the .NET Framework SDK documentation for more details.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
ok, it was because of the KeyPreview property. I'm going to check the MSDN...
thanks
There is no spoon.
|
|
|
|
|
I have a quick question about the dateTimePicker included with Visual Studio. I only need to use the month and the date so I was wondering if I could disable or remove the calendar part of the dateTimePicker when the user clicks the drop down arrow?
Thanks
|
|
|
|
|
What exactly are you trying to do? If you're just trying to display a date and not let them pick a date, then just format a DateTime to the stirng format you want:
label1.Text = DateTime.Now.ToString("m"); For more information about the format codes allowed for DateTime.ToString read about it in the .NET Framework SDK, which is installed by default with VS.NET and is available online at http://msdn.microsoft.com/library[^].
If you disable the calendar, how is the user to pick a date? Yes, they can use the arrow buttons but many people don't know about that and it is cumbersome to use.
Do you only want to disable the year selection of the calendar? While not impossible it's not supported by the calendar control (which is actually the native Month Calendar control from Windows Common Controls) and you would have to go to quite a bit of work to clip it or hide that portion of owner-drawn content and to not allow notification messages to be sent when actions occur over those coordinates (like a mouse click).
If you want to allow the user to only see the month and day, then use a custom format by setting the DateTimePicker.CustomFormat property and in the code that uses the DateTime ignore the year.
Other than that you'll need to develop your own control.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
I wanted to only allow the user to pick a month and a year. I do not need to use a date.
|
|
|
|
|
Anonymous wrote:
I only need to use the month and the date...
Your first statement was rather ambiguous.
Still, though, there is no support from the Common Control (and, hence, none in the Windows Forms control that encapsulates it) to hide the day select either.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
I have just started this. and i m trying to make a chat application that runs over LAN.
there might be many other ways and they might be better then using Udp port but i want to understand this thing. i have read some articles before posting but i m now having problem on receiveing messages.
public void waitData()<br />
{<br />
while(true)<br />
{<br />
IPEndPoint from = new IPEndPoint(IPAddress.Any,170);<br />
<br />
<br />
string result = "";<br />
Byte[] drec = receive.Receive(ref from);<br />
result = Encoding.ASCII.GetString(drec);<br />
textBox1.Text = result.ToString();<br />
<br />
}<br />
}
this code is to recieve messages and i made a thread so it keep waiting for any message to recieve. and i m broadcasting message to network.
i m new to this so plz help me
i think its enough to explain what i want to do but if still you think i had to tell more. tell me.
|
|
|
|
|
So what's the problem?
Also, don't keep reinstantiating your IPEndPoint in your while loop. You only need to do this once per end point required for your application.
If you're having trouble using UDP classes in .NET then be sure to read about them in the .NET Framework SDK, like the UdpClient[^] class. Many of these include examples and reading about what the methods and properties actually do is important.
The documentation I've linked in particular shows you how to send and receive buffers using the UdpClient .
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Dear all
I am supposed to develop a application in both English & Arabic in C# 2003. My doubts is, do I have to create all the forms, message boxes in Arabic also (By Mirroring). Does it mean that my application will have duplicate forms. One for Arabic and One for Non-Arabic. I am using SQL Server 2000. How will the Arabic Forms be written in the database? Can i be able to recognise/Display like normal characters in other application?
If any of you have faced/solved such issues, please help me out. If possible
hint me some references also.
Thanks in advance
Chandru.
|
|
|
|
|
|
You need to localize your app so that no text is entered directly onto the form.
You don't want to have to code more than one copy of the same form. If you are careful you need only make one form that is modified at run time to display the correct text.
We completely ignored the Microsoft localization techniques because they expect you to provide localized resource files with your app which for us won't work because we have users that may want to work in different languages in the same office and switch at will and we also wanted to make it very easy for our users to not only make their own language text but be able to modify the stock ones for their own business. I.E. if a field says "Date of employment" in english they might want to change that to "Date hired" or what have you. We figured that since we were going to localize we might as well make it even more useful for the users who often like to make these little changes if they can since terminology varies so much even across english speaking countries.
You may be able to use the more traditional methods referred to in that resource someone else pointed you to, however keep in mind they are static and not flexible.
What we did instead is we put keycode strings into the text properties for controls. For example on a customer form with a date field on it, instead of having a label with the text DATE in it, we put in "Customer.Label.Date". Then when the form is opened we have a static code that substitutes on the fly the correct string before displaying to the user.
All our language strings are stored in a database and loaded into memory when a user logs into our app so they can be displayed as required. No where in our source code are there any hard coded strings, simply keycodes instead.
In your database you must ensure that you are using all the unicode compliant versions of sql server table field definitions. I.E. use NVARCHAR instead of VARCHAR.
|
|
|
|
|
Hello!
I've been trying to figure out how to work with the VS 2005
client/application settings but I can't make it work.
I can go to the project properties and choose the settings tab where I
can enter different settings. But I can't change it att runtime.
If someone could be nice enough to give me an code example of how I
could enter a setting, change it at runtime and save it, I would be
very grateful!
Thanks in advanced!
~~~~~~~~~~~~~~
Martin Lundberg
Student, Sweden
I have to thank every member of the Code Project for making it such a great place for a beginner to learn!
|
|
|
|
|
Those settings are changed through the .config file for the application, which is located in the same directory as the application and with the same filename with ".config" appended.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hello =)
Thanks for answering but I know they are in there, but you shouldn't have to change the values there with xml types. If you let VS make the settings class it creates a class called Settings that inherits from ApplicationSettingsBase. You can also make your own class and inherit from ApplicationSettingsBase, and then add a property for every setting.
Read more about this here:
http://msdn2.microsoft.com/library/0zszyc6e.aspx
~~~~~~~~~~~~~~
Martin Lundberg
Student, Sweden
I have to thank every member of the Code Project for making it such a great place for a beginner to learn!
|
|
|
|
|
Yes, I'm well aware of that.
VS 2005 is in beta, and there's a lot that doesn't work. We're nearing beta 2 when much more works but can release no further details about when beta 2 is due to release.
For now you have to change the settings through the .config file as you do for .NET 1.0 and 1.1 or write your own SettingsProvider which is responsible for serializing the ApplicationSettingsBase class (and its derivatives).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hello again!
Well, I've tested some more and if I use user-scoped settings I am able to change them and save them.
=) Thanks for the answers!
~~~~~~~~~~~~~~
Martin Lundberg
Student, Sweden
I have to thank every member of the Code Project for making it such a great place for a beginner to learn!
|
|
|
|
|
I have tried the above solution .
but if i give File.AppendText(Path) then it will append it at the end
so what to do?
|
|
|
|