|
Hi, Can you please tell me how I can access the controls of a form from a WCF service? The forms application and the service reside in one solution as seperate projects and I added wcf service the necessary reference to forms project but still I'm not able to access the controls(controls are public). Your valuable suggestions are welcomed.
Thanks!
|
|
|
|
|
Can't be done. The WCF service is running in a seperate process, usually on a seperate machine.
If the service needs access to the controls, it's up to your client app to pass the required values in.
Seriously, no code outside of the form the controls sit on should ever need to access the controls of a form.
|
|
|
|
|
You can't and shouldn't access them directly as has already been said.
If your Forms app is receiving messages from the service then it can respond to those messages and update it's controls itself.
|
|
|
|
|
I prefer to have the Service generate an XML file that the clients read.
|
|
|
|
|
Hi,
First of all, are you sure that's what you want to do? Why? Sorry for asking this, but I believe it's not, and unless you provide a context, I don't know if i'm helping or throwing you a rope to hang yourself.
But to answer your question, you can create an instance of the form in the service's class, and it will be available in that service's context.
Now, I believe you want to publish the form data on the web. Here's how i'd go about it:
1. Keep both projects separated (not in the solution, just reference wise);
2. Implement methods on the web service that let you submit form data through them;
3. Make your forms application a client of the webservice;
4. As the controls on the form are filled, send changes to the web service;
5. All clients of the web service will be able to see those changes.
Hope i helped.
Regards
|
|
|
|
|
hi, thanks for detailed info. Here is my case: this is an internet cafe software and I want to send the time info to clients at specific intervals. The controls that I need to access are created and added to the mainform in form's load event. And the mainform is a singleton. Normally I can access it by; MainForm.Instance.Control
I don't ask for ready code, please just show me the way
Thanks in advance
|
|
|
|
|
Hi again,
Well, start by creating a method in your Forms project that does just that, recieves the time info, and updates the controls. You can do this with
public void updateTime(int timeSpan){
for each(Control c in this.Controls){
c.doSomething();
}
}
then, all you need to do is spawn a thread that wakes up every few minutes (or whatever period of time you want) and access the web service and retrieve the time information from your server. When it wakes up, it calls the method mentioned earlier.
The code i'm about to throw in here is by no means a product of good coding practices and will definitelly have bugs as i'm writing it directly on the reply from here at CodeProject, but might just do the trick.
public class Scheduler : Thread {
private Form parentForm;
private "WebServiceProxy" webServiceProxy;
public Scheduler(Form mainForm){
this.parentForm = mainForm;
this.webServiceProxy = new "WebServiceProxy"();
}
public override void Run() {
while(true) {
int timeSpan = webServiceProxy.getTimeSpan();
sleep(5000);
}
}
}
Now add an instance of the Scheduler to your main form.
In the constructor of your form (or at the InitializeComponent method) initialize the Scheduler field and call the Run() method on it.
Try it for yourself.
Hope it helps
|
|
|
|
|
Thanks for your help si24803!
|
|
|
|
|
It would be possible for you to use a duplex WCF service, and make your form implement a callback interface. Wouldn't put any UI types in your interface though. Generally this isn't a terribly scalable approach however.
|
|
|
|
|
I get this error when trying to send something between two apps over TCP.
The weird part is that I can send one time without any trouble, but whenever I try a second time, I get this error.
I use this code for the sending app:
private void B_Send_Click(object sender, EventArgs e)
{
string send = "";
send = TB_Quotes.Text;
TcpClient tcp = new TcpClient(TB_Host.Text, (int)UD_Port.Value);
NetworkStream ns = tcp.GetStream();
StreamWriter writer = new StreamWriter(ns);
writer.WriteLine(send);
writer.Flush();
ns.Close();
writer.Close();
tcp.Close();
}
And this code for the receiving app:
public Form1()
{
InitializeComponent();
Thread treceive = new Thread(new ThreadStart(Listen));
treceive.Start();
}
public void Listen()
{
int port = 2112;
string result = "";
IPAddress localaddress = IPAddress.Parse("127.0.0.1");
TcpListener listener = new TcpListener(localaddress, port);
listener.Start();
TcpClient client = listener.AcceptTcpClient();
NetworkStream ns = client.GetStream();
StreamReader reader = new StreamReader(ns);
result = reader.ReadToEnd();
Invoke(new UpdateDisplayDelegate(UpdateDisplay), new object[] {result});
}
public void UpdateDisplay(string text)
{
TB_Quotes.Text = text;
}
I'm making sure the port is 2112 (if not, the first time would not be successful), but I don't understand why the 2nd time won't work.
Thanks.
|
|
|
|
|
The Listen method ends after the Invoke bit at the end (also terminating the started thread). Thus it can
not accept new connections (client = listener.AcceptTcpClient(). You'd have to have some kind of loop
where the server continually accepts connections. From within the loop you could start threads that do the
handling for each individual connection.
Cheers
Manfred
|
|
|
|
|
Thanks.
I added a loop and now it works just fine.
|
|
|
|
|
If the answer was helpful, I'd really appreciate a vote.
|
|
|
|
|
Megidolaon wrote: writer.Flush();
one remark: you oinly need to flush when you don't close right away; here you want to close, so don't flush, there is no need to. However, you should close nested things in reverse order: since you opened ns and then writer, you should close writer, then ns. That way, no flush is required.
|
|
|
|
|
Hello, i have a problem regarding threshold filter. The code as below:
<br />
private void button3_Click(object sender, EventArgs e)<br />
{<br />
<br />
<br />
Threshold filter = new Threshold(100);<br />
filter.ApplyInPlace(threImage);<br />
pictureBox3.Image = threImage;<br />
<br />
}
However i have this error " Source pixel format is not supported by the filter. " when the button is being invoked.
anyone know whats the problem?
must the image i apply the filter to be a greyscaled one?
|
|
|
|
|
Presumably you are using the AForge Threshold class[^] - it's the only one I could find on the internet that has identical code to your sample - which doe indeed state it should be a greyscale bitmap. Which makes some sense.
It might bne easier if you point your questions at the folks who wrote the code? They are more likely to know the answers to questions on their own package that a general site like this...
BTW: In future, if you are using a package, name it. It makes life easier for us all.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
|
Did you gave a question or did you just want to post a link and an abscure piece of text for nothing?
|
|
|
|
|
Ah the classic compile error problem. Try one of the following...
IMPORT EXCEL FROM SQL;
EXPORT EXCEL TO SQL;
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
Hi,
I've class, Node, with the following properties and these are the attributes of XML element.
XmlAttribute("id")]
public string Id
{
get { return m_id; }
set { m_id = value; }
}
[XmlAttribute("char")]
public string Char
{
get { return m_ch; }
set { m_ch = value; }
}
When this is serialized, it created the xml elements as below
<Node char="_" id="S_SEPARATOR">
<Node char="" id="S_BLANK">
The requirement is, if id is "S_BLANK", it shouldn't show "char" attribute, as below.
<Node char="_" id="S_SEPARATOR">
<Node id="S_BLANK">
Please guide me to get this output. Thanks in advance.
|
|
|
|
|
Hi,
you can use the ShouldSerialize* pattern
where * is the property name that's supposed to be serialized
public bool ShouldSerializeChar() {
return !"S_BLANK".Equals(id);
}
Cheers
Manfred
|
|
|
|
|
Thanks for your reply. Where should I call ShouldSerializeChar()?
|
|
|
|
|
This function is not meant to be called by you.
It's called by the XML serializer. If the serializer
sees a property with say an XMLAttribute it knows
it should serialize it. Then it checks if there is
a method bool ShouldSerialize(Property name here).
If it finds such a method the serializer calls it
and uses the return value to determine wether it
will really serialize this attribute or not.
Best Regards,
Manfred
|
|
|
|
|
Thank you, Manfred.. Thanks alot for detailed info..
|
|
|
|
|
Voting for a helpful answer is always appreciated.
|
|
|
|