|
I agree with you: the Socket class has much more functionality. If you can bear with me, though, I modified my last example with the TcpLiseners to handle actual messages passed into the child forms.
using System;
using System.Drawing;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;
namespace FormsSpawnedByThreads
{
public class MainForm : Form
{
public MainForm() : base()
{
Text = "Spawn Some Forms";
Height = 150;
Width = 300;
Button b = new Button();
b.Text = "Connect";
b.Top = ( Height - b.Height ) / 2 - ( b.Height / 2 );
b.Left = ( Width- b.Width ) / 2;
b.Click += new EventHandler( b_Click );
Controls.Add( b );
Thread t = new Thread( new ThreadStart( ListenNewConnections ) );
t.IsBackground = true;
t.Name = "Main Listener";
t.Start();
clients = new TcpClient[ 5 ];
Closing += new System.ComponentModel.CancelEventHandler( MainForm_Closing );
}
private void ListenNewConnections()
{
IPEndPoint endPoint = new IPEndPoint( IPAddress.Parse( "127.0.0.1" ), 5555 );
listener = new TcpListener( endPoint );
listener.Start();
while( true )
{
try
{
TcpClient client = listener.AcceptTcpClient();
this.Invoke( new HandleNewConnection( OpenChildForm ), new object[] { client } );
}
catch( Exception ) {}
}
}
private delegate void HandleNewConnection( TcpClient client );
private void OpenChildForm( TcpClient client )
{
ChildForm cf = new ChildForm( client );
cf.Show();
}
private void b_Click( object sender, EventArgs e )
{
clients[ clientIndex ] = new TcpClient( "127.0.0.1", 5555 );
byte[] b = System.Text.Encoding.ASCII.GetBytes( message );
clients[ clientIndex ].GetStream().Write( b, 0, b.Length );
clientIndex++;
if( clientIndex == 5 )
{
Controls[ 0 ].Enabled = false;
}
}
private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if( listener != null )
{
listener.Stop();
}
}
private TcpClient[] clients;
private int clientIndex;
private TcpListener listener;
[STAThread]
public static void Main()
{
Application.Run( new MainForm() );
}
private string message = "It was many and many a year ago in a kingdom by the sea\n" +
"Where a maiden there lived whom you may know by the name of Annabel Lee.\n" +
"And, this maiden, she lived with no other thought than to love and be loved by me.";
}
public class ChildForm : Form
{
public ChildForm( TcpClient client ) : base()
{
Label l = new Label();
l.Dock = DockStyle.Fill;
Controls.Add( l );
c = client;
Closing += new System.ComponentModel.CancelEventHandler( ChildForm_Closing );
buffer = new byte[ 256 ];
Thread t = new Thread( new ThreadStart( HandleConnection ) );
t.Start();
}
private void HandleConnection()
{
string s = null;
NetworkStream cnxStream = c.GetStream();
int len;
try
{
while( ( len = cnxStream.Read( buffer, 0, 256 ) ) > -1 )
{
Controls[ 0 ].Text += System.Text.Encoding.ASCII.GetString( buffer, 0, len );
}
}
catch( Exception ){}
}
private void ChildForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
c.Close();
}
private byte[] buffer;
private TcpClient c;
}
}
"we must lose precision to make significant
statements about complex systems."
-deKorvin on uncertainty
|
|
|
|
|
Just curious if this happens to you...
my machine seems to drastically drag when i start running the 2nd thread
Both threads are continously running in the loop
while(true)
{
if Connection is Pending then
Accept Socket
}
one is listening for new connections and the other listening with a direct connection
Is this best practice for listening on sockets..... (running loops like this)
How do yahoo instant messengers handle listening on sockets?
what about AOL messengers?
Is there another way of handling this w/o loops?
do they all have loops running like this....
just curious
Kourvoisier
-- modified at 18:19 Monday 14th November, 2005
|
|
|
|
|
How can I set a form's owner to be a window outside my application?
I have a form which I want to hover in front of a specific window. The target window is not a form in my application; it is a standard windows window (type "Overlapped.") I have written user32.dll calls to get the target window's handle and position; I position my form in the appropriate place on the screen. However, to ensure that my form stays visibly in front of the target window, I have had to set it to be Always in Front.
This is a kludge, because the form is also in front of any other windows I may open, which is inappropriate. I only want my form to stay in front of the target window.
I can manually play with the z-order to ensure that my form is front of the target window and no other, but it would be more elegant to set the target window to be the owner of my form's window and let the system deal with the rest.
I understand that the user32.dll function CreateWindowEx can only set the owner of a window at creation time. I can provide the handle when I create the form, but I have been unable to divine a way to provide it. SetParent will allow me to change the parent window, but the new parent must belong to the same application.
.Net allows me to change the owner of a form I create, but only lets me change it to another .Net form; I can not find a way to user the window handle I have.
Any suggestions?
|
|
|
|
|
Basically, you can't do this. You can only set the owner to a window in your own process. You can't set it to be owned by a window in another process.
You might want to do some research into "DLL Injection" to get around this limitation. In theory, your application injects code into another process so that that code could create the new window and assign it's parent to the window you want. In theory...
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Update to this old thread -- I've learned you CAN do this.
When you use SetParent on a window against a window in another process, the other window does not become the Parent, but it DOES become the Owner, making your window an Owned window.
The resulting behavior is that the owned window always appears in front of the owner (z-order wise), and will be minimized when the owner is minimized.
The owned window will not MOVE with the owner, however. That still has to be arranged otherwise.
Also, when the owned window overlaps the owner, you must be careful with your code when hiding/showing things - the owner, which is hidden, will not always refresh itself appropriately, since the owned window can't pass invalidate messages to it as a parent.
|
|
|
|
|
I may have misread your original post, but it doesn't sound like you got the effect you were looking for. You've gotten something else LIKE what you were describing though, with LOTS of problems.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Actually, I'm in pretty good shape. . .
I already have separate code which runs on a timer loop which checks the target window positions and moves my overlays to match. I've written code to adjust the z-order on each pass, but that looked kludgy because my overlays would disappear and then reappear every time the target windows were clicked (they would come to the fore, and then my overlay would be brought back in front.) I could avoid the blinking by setting them TopMost, but then my overlays would cause problem when windows are overlapped; any window opened in front, like a browser, would have my overlays popping through.
This solution fixes the z-order problem entirely, does not cause blinking or pop-through, and does not even require my code to do anything - windows handles the z-order processing efficiently for me.
The one problem - that of a failed refresh if I minimize one of my overlays - is not a major problem to me. It was only causing a problem when I hid an overlay window after some third window was interposed between the target window and the owned window, which is a rare condition (once I ensured that my own application didn't do this to me! When switching overlays, I needed to ensure I was hiding the old before showing the new, which is a simple fix.)
If, in some other circumstance, the condition isn't rare, the fix is a simple matter of adjusting the .Hide() call; it should include a SendMessage call which sends an invalidation method to the owner window. Voila!
|
|
|
|
|
hi there!
can i know how to set the date format of the calendar in a datetimepicker?
actually i know it can be set in the regional settings of your machine but how about during run-time?
any suggestions or ideas will be very much appreciated!
tnx in advance!
|
|
|
|
|
Hi
public void SetMyCustomFormat()
{
// Set the Format type and the CustomFormat string.
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "MMMM dd, yyyy - dddd";
}
Hope it helps
|
|
|
|
|
hello!
I already tried that before i post and it only change the text of the datetimepicker. the calendar still has the format that is set on your regional settings.
anyway, tnx for the reply!
|
|
|
|
|
What is static and Dynamic Assemblies?
What is the use of these Assemblies in our project ?
|
|
|
|
|
This is a forum for asking for specific help with programming questions, usually requiring code, and a description of a problem.
Google is a tool which can be used for general research, including but not limited to, homework.
|
|
|
|
|
Hi.
I am working on a project in which I need to encrypt data I am storing in MySql Database.There are many codes available on Internet but most of them are in PHP and I have to encrypt data in C# windows application .Any help please.
Thank you.
|
|
|
|
|
Hi, I a trying to capture the voice using direct sound, but it is not working,here is my code can anyone help me, I will be very thankful to him.
private void CaptureThread()
{
MemoryStream inputStream = new MemoryStream(intervalSize);
while (button1.Text == "Stop")
{
bool val=bufferNotifyEvent.WaitOne();
int currentPos;
int readPos;
captureBuffer.GetCurrentPosition(out currentPos, out readPos);
inputStream.Position = 0;
captureBuffer.Read(0, inputStream, intervalSize, LockFlag.None);
outputBuffer.Write(0, inputStream, intervalSize , LockFlag.None);
outputBuffer.Play(0,BufferPlayFlags.Looping);
}
Stop();
}
the above code is the main thread that run to capture the voice, and some initializations are below
private void Form1_Load(object sender, System.EventArgs e)
{
WaveFormat waveFormat;
CaptureBufferDescription capBufDesc;
BufferPositionNotify[] notifyPositions;
bitsPerSample = 8; // 8 bits per sample
const int samplesPerSecond = 11025; // 11 KHz
const double interval = 1/45f; // Seconds
intervalSize = (int) (interval * samplesPerSecond * bitsPerSample / 8);
int samplesPerInterval = intervalSize * 8 / bitsPerSample;
int bufferSize = intervalSize * 2;
captureDevice = new Capture();
waveFormat = new WaveFormat();
waveFormat.BitsPerSample = bitsPerSample;
waveFormat.SamplesPerSecond = samplesPerSecond;
waveFormat.Channels = 1;
waveFormat.AverageBytesPerSecond = bitsPerSample * samplesPerSecond / 8;
waveFormat.BlockAlign = (short)(bitsPerSample / 8);
waveFormat.FormatTag = WaveFormatTag.Pcm;
capBufDesc = new CaptureBufferDescription();
capBufDesc.Format = waveFormat;
capBufDesc.BufferBytes = bufferSize;
captureBuffer = new CaptureBuffer(capBufDesc, captureDevice);
bufferNotify = new Notify(captureBuffer);
bufferNotifyEvent = new AutoResetEvent(false);
notifyPositions = new BufferPositionNotify[2];
notifyPositions[0] = new BufferPositionNotify();
notifyPositions[0].EventNotifyHandle = bufferNotifyEvent.Handle;
notifyPositions[0].Offset = intervalSize - (bitsPerSample / 8);
notifyPositions[1] = new BufferPositionNotify();
notifyPositions[1].EventNotifyHandle = bufferNotifyEvent.Handle;
notifyPositions[1].Offset = bufferSize - (bitsPerSample / 8);
bufferNotify.SetNotificationPositions(notifyPositions);
BufferDescription outputBufferDescription;
outputDevice = new Device();
outputDevice.SetCooperativeLevel(this, CooperativeLevel.Normal);
outputBufferDescription = new BufferDescription(waveFormat);
outputBufferDescription.BufferBytes =(int)(intervalSize*1.5f-50);
outputBufferDescription.ControlVolume = true;
outputBufferDescription.GlobalFocus = true;
outputBuffer = new SecondaryBuffer(outputBufferDescription, outputDevice);
}
all the initialization in done in Load_page and the thread will run the user will press the button, but no voice is captured, plz guide me, I will be thankful to u,
|
|
|
|
|
Hi,
I have a question on sql writing
for a table schema as below
Table :
(studentID, studentName, courseID, classID, Age)
What is the correct sql of " select studentID from table where classID = "101" and group by courseID and order by age " ? (ie. Group by courseID, and within the same courseID, the studentID are ordered by age)
Thanks
|
|
|
|
|
I might have this wrong after a few beers, but:
SELECT studentID FROM Table
WHERE classID = '101'
GROUP BY studentID, courseID
ORDER BY Age
Hope that helps
Jon Humphreys
ActivePlanet Software
Bangkok, Thailand
|
|
|
|
|
what is the complete code for downloading a file using FTP through ASP.NET/C#
-- modified at 1:50 Friday 11th November, 2005
|
|
|
|
|
Perhaps FileWebRequest [^] will provide the answer?
I assume you could replace the file:// protocol with ftp:// . If you need to supply a username and password, use the form ftp://username:password@somewhere.com .
/ravi
My new year's resolution: 2048 x 1536
Home | Music | Articles | Freeware | Trips
ravib(at)ravib(dot)com
|
|
|
|
|
Hi guys,
I don't know why this error below keeps popping-up when I try to run my application and crashes.
Anybody seen this before? The application autoruns when the Windows startup.
************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Windows.Forms.NotifyIcon.WndProc(Message& msg)
at System.Windows.Forms.NotifyIconNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Thanks guys.
|
|
|
|
|
Im going to take a wild stab in the dark
nosferatu2000 wrote: System.NullReferenceException: Object reference not set to an instance of an object.
You have an object somewhere, which you try to call a method on, however that object is Null.
Null Reference exceptions are pretty cut and dry... not really that much ambiguity except maybe WHAT is actually null.
Debug your code, step through it until this error occurs and then you'll know what is null.
|
|
|
|
|
private void Form1_Resize(object sender, System.EventArgs e)
{
if (FormWindowState.Minimized == this.WindowState)
{
this.Hide();
this.ShowInTaskbar = false;
this.Visible = false;
notify.Visible = true;
}
}
private void notifyIcon1_DoubleClick(object sender, System.EventArgs e)
{RestoreDisplay();}
private void RestoreDisplay()
{
Show();
WindowState = FormWindowState.Normal;
ShowInTaskbar = true;
this.Visible = true;
notify.Visible = false;
}
private void menuItem1_Click(object sender, System.EventArgs e)
{RestoreDisplay();}
private void menuItem2_Click(object sender, System.EventArgs e)
{Dispose(true);}
private void Form1_Load(object sender, System.EventArgs e)
{
this.Resize += new EventHandler(Form1_Resize);
notify.DoubleClick += new EventHandler(notifyIcon1_DoubleClick);
}
i'm having problems restoring it from system tray.
the display is buggy. basically what happens is that the tabs contents dont appear. sometimes the treeview contents dont appear either.
i can get it up and down fine, however making it functional seems to be a little harder.
|
|
|
|
|
I am developing a system on Visual C#. What I want to do is create an icon on my desktop that will open up my system. I also want the icon to produce a loading page and then open the first window of my system. Any ideas? Please do not tell me to google it.
|
|
|
|
|
vandread1 wrote: Please do not tell me to google it.
*grin* Now why would we say that ?
I don't get your problem - do you want to create the icon in code ? Because otherwise, it sounds like your icon will be a shortcut to your exe, surely ? How does MySQL come into this ?
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
I accidently put mysql as the title. It doesn't have to do anything with my question. The icon is a shortcut to my exe, but I want a loading page to appear before my exe.
|
|
|
|
|
OK - so the question is, how do I get a splash screen ?
Basically, you want to store a picture in your resources for your exe, then you want to create a frameless dialog which shows the picture, and loads it from resources. Make it a member variable, call Show on it as your first step in initialisation, and then destroy it when your program is fully loaded and ready to go.
LMK if you need help with any of these steps.
Or you could use this[^] ( for what it's worth, I googled it )
Christian Graus - Microsoft MVP - C++
|
|
|
|
|