|
Kenneth Haugland wrote: it does work but it won't allow the connected classes to be garbage collected Do you want connected classes to be able to be GC'ed individually or is it enough that they can be GC'ed once you're done with all of them?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
I have to allow them to be GB individually, as I might exchange one of the classes with a new instance.
|
|
|
|
|
I was able to make it work now:
internal class WeakPropertyDescriptor
{
public PropertyMediator _MethodOwner;
public WeakReference _sender;
public string _PropertyName;
public string _MessageKey;
public EventHandler<EventArgs> handler;
private Type constructedWEMType;
public WeakPropertyDescriptor(PropertyMediator MethodOwner, Object sender, string PropertyName, string MessageKey)
{
_MethodOwner = MethodOwner;
_sender = new WeakReference(sender);
_PropertyName = PropertyName;
_MessageKey = MessageKey;
handler = new EventHandler<EventArgs>(WeakPropertyDescriptor_PropertyChanged);
Type unboundWEMType = typeof(WeakEventManager<,>);
Type[] typeArgs = { _sender.Target.GetType(), typeof(EventArgs) };
constructedWEMType = unboundWEMType.MakeGenericType(typeArgs);
MethodInfo addHandlerMethod = constructedWEMType.GetMethod("AddHandler");
addHandlerMethod.Invoke(null, new object[] {_sender.Target , "PropertyChanged" , handler});
}
private void WeakPropertyDescriptor_PropertyChanged(object sender, EventArgs e)
{
PropertyChangedEventArgs arg = (PropertyChangedEventArgs)e;
if (arg.PropertyName == _PropertyName)
_MethodOwner.NotifyColleaguesOfValueChanged(_sender.Target, e, _MessageKey, _PropertyName);
}
public void RemoveHandler()
{
MethodInfo removeHandlerMethod = constructedWEMType.GetMethod("RemoveHandler");
removeHandlerMethod.Invoke(null, new object[] { _sender.Target, "PropertyChanged", handler });
}
}
modified 25-Jan-16 4:53am.
|
|
|
|
|
To be honest, given what you're trying to do, I would look to use RX to observe property changes. In fact, that's exactly what I have done for some of my clients.
This space for rent
|
|
|
|
|
|
I tend to use a variation of code that builds on something like this[^].
This space for rent
|
|
|
|
|
|
Thank you, Kenneth! I'll take a look at it.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
when i click a button just like i click a "Category" it auto generate all the category values button.
|
|
|
|
|
It would help to know what UI you are building for...
Have a collection of categories in the code behind
Instead of buttons use a list control (listbox with a button as the item content) and bind the category collection to the list control (DataSource or ItemSSource).
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
it is also an perfect but i want to build and application to touch screen.
|
|
|
|
|
Touch screen does not dictate the structure. You may be interested in some of these articles[^]
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
the above c sharp code will send client desktop images to server . it works for single client . we want to implement this for multiple client. please provide help to implement it.
SERVER code:-
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.Remoting;
using System.Linq;
using System.Text;
using System.Net .Sockets ;
using System.Windows.Forms;
using System.Threading ;
using System.Runtime.Serialization.Formatters.Binary;
using System.Net;
namespace simpleserver
{
public partial class Form2 : Form
{
private readonly int port;
private TcpClient client;
private TcpListener server;
private NetworkStream mainstream;
private readonly Thread Listening;
private readonly Thread Getimg;
public Form2(int Port)
{
port = Port;
client = new TcpClient();
Listening = new Thread(StartListening);
Getimg = new Thread(ReceiveImage);
InitializeComponent();
}
private void StartListening()
{
while (!client.Connected)
{
server.Start();
client = server.AcceptTcpClient();
}
Getimg.Start();
}
private void stop()
{
server.Stop();
client = null;
if (Listening .IsAlive )Listening .Abort ();
if ( Getimg .IsAlive )Getimg .Abort ();
}
private void ReceiveImage()
{
BinaryFormatter binformatter = new BinaryFormatter();
while (client.Connected)
{
mainstream = client.GetStream();
pictureBox1.Image =(Image ) binformatter.Deserialize(mainstream);
}
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
server = new TcpListener(IPAddress .Any ,port );
Listening .Start ();
}
}
}
CLIENT CODE:-
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
namespace sampleclient
{
public partial class Form1 : Form
{
private readonly TcpClient client = new TcpClient();
private NetworkStream mainStream;
private int portNumber;
private static Image GetDesktop()
{
Rectangle bounds = Screen.PrimaryScreen.Bounds;
Bitmap screenshot = new Bitmap(bounds .Width ,bounds .Height,PixelFormat .Format32bppArgb );
Graphics g = Graphics.FromImage(screenshot);
g.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy);
return screenshot;
}
private void SendDesktop()
{
BinaryFormatter binformatter = new BinaryFormatter ();
mainStream = client.GetStream();
binformatter.Serialize (mainStream, GetDesktop());
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
portNumber = int.Parse(textBox2.Text);
try
{
client.Connect(textBox1.Text, portNumber);
MessageBox.Show("connected success");
timer1.Start();
}
catch (Exception)
{
MessageBox.Show("fail");
}
}
private void timer1_Tick(object sender, EventArgs e)
{
SendDesktop ();
}
}
}
modified 23-Jan-16 12:12pm.
|
|
|
|
|
Apart from that being terrible code - VS default names for everything, commented out blocks, assumptions that users never type mistakes, ignoring exception detail when you do catch them, and so forth - we have no idea what you mean by "make it work for multiple clients", or what you problem with doing it yourself is.
And when you add that we aren't here to do your job for you I think it's very unlikely that you will get what you want with this question.
Instead, sit down and work out exactly what you are trying to do, and work out what you need to implement in order to do it. Then make a list of what you do and don;t know how to do, and implement the stuff you do.
For the stuff you don't start with google, and ask specific questions when you can't find what you need, or don't understand whatyou find.
But just saving "provide help to implement it" isn't going to get you very far...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
First of all, have you considered using
code pre tags when posting code? It makes it much easier to read.
Second, what have you attempted so far?
"I've seen more information on a frickin' sticky note!" - Dave Kreskowiak
|
|
|
|
|
|
As I suppose you have found, a point-to-point socket connection is not what you want for this.
You will likely find success by using a Web Service or similar, but I expect a database connection would be even simpler because you wouldn't actually need to implement anything on the server.
|
|
|
|
|
PIEBALDconsult wrote: but I expect a database connection would be even simpler
Isn't it not good to expose the database across the network like that?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
I assume it's a local network, but if not, then a Web Service may provide better security.
|
|
|
|
|
Well, even though as others as pointed out, this is not good code... One easy answer is simply that each client code append say a block of a given length before the data for identification purpose (assuming this is for educative purpose).
Philippe Mori
|
|
|
|
|
Context for the solution helps. So is this a commercial solution, something for school, experimenting, etc.
As others have noted there are standard idioms that you can research for doing client/server setups.
If you want to roll you own then
1. Separate out communication from processing. Different classes. So you need classes that 'send' a message and classes that 'receive' a message. Then different classes that package a 'desktop image' as a message
2. On the server side when a server accepts a client connection then the server spins up a new thread to handle that request (for messages between the original client and the server.) It does that for each client and perhaps for each message that a unique client sends. A quick glance at your code suggests that you are not creating a new thread for each request.
|
|
|
|
|
I am aware of two "fake data" generators for C#, Faker and Faker.NET, and I have posted a message with information about them on the "Free Tools" forum today: [^].
These generators are both libraries of static functions that return strings. Faker.Net (available through NuGet) has some interesting coding techniques.
I am curious if anyone knows of a generator that spits out DataTables.
thanks, Bill
«Tell me and I forget. Teach me and I remember. Involve me and I learn.» Benjamin Franklin
|
|
|
|
|
BillWoodruff wrote: I am curious if anyone knows of a generator that spits out DataTables. Shouldn't be too hard to build..
Then again, what use would an app be that generates random tables with random relations? Makes more sense to fill an existing table-structure with fake or random data.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi Eddy,
I have a need for some huge auto-generated DataTables ... for stress-testing a UI generator... and, indeed, I am going to write one, and have started writing one, but wanted to see if there was one already out there. Right now, I am using Faker.Net for a random-data-by-Type provider, and building on top of that.
Being free of life$tyle constraints enables me to pursue such esoteric frivolities.
cheers, Bill
«Tell me and I forget. Teach me and I remember. Involve me and I learn.» Benjamin Franklin
modified 22-Jan-16 21:25pm.
|
|
|
|
|
BillWoodruff wrote: stress-testing Wouldn't call that frivolities
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|