Click here to Skip to main content
15,902,112 members
Home / Discussions / C#
   

C#

 
AnswerRe: Convert.ToByte('Š') throws an exception Pin
Dragonfly_Lee4-Jan-09 22:03
Dragonfly_Lee4-Jan-09 22:03 
Questionhow to restrict only one instance to run at a time on one machine Pin
prasadbuddhika3-Jan-09 17:36
prasadbuddhika3-Jan-09 17:36 
AnswerRe: how to restrict only one instance to run at a time on one machine Pin
N a v a n e e t h3-Jan-09 18:06
N a v a n e e t h3-Jan-09 18:06 
GeneralRe: how to restrict only one instance to run at a time on one machine Pin
prasadbuddhika3-Jan-09 18:50
prasadbuddhika3-Jan-09 18:50 
GeneralRe: how to restrict only one instance to run at a time on one machine Pin
N a v a n e e t h3-Jan-09 19:42
N a v a n e e t h3-Jan-09 19:42 
QuestionRounding a float to the nearest int Pin
Tony Pottier3-Jan-09 13:38
Tony Pottier3-Jan-09 13:38 
AnswerRe: Rounding a float to the nearest int Pin
PIEBALDconsult3-Jan-09 13:46
mvePIEBALDconsult3-Jan-09 13:46 
QuestionRemote event update RichTextBox problem Pin
ZarazaPhd3-Jan-09 8:05
ZarazaPhd3-Jan-09 8:05 
I wrote a little application to try Remote Events
I have two versions of this application

The first one: both, server and the client are Console application
The first one is working fine
I did not write any methods in Client, just do the Console.Writeline()

The second : server is a console application and the client is winform application. I have problem with second:

The event riesed but this event need to update RichTextBox in my Main form
I get this exception:
Cross-thread operation not valid: Control 'rtb_in' accessed from a thread other than the thread it was created on.

I tryed many approuches nothing helped.
Please help me with this issue.

Here is my code sample

Server:
using REWFComm;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Serialization.Formatters;
using System.Collections;
using System.Reflection;

namespace Server
{
class ServerRemote : MarshalByRefObject, ICommon
{
public void Message(string passed, object Client)
{
Console.WriteLine(passed);
MessageEvent(passed + ": your message is arrived",Client);
}

public event MessageHandler MessageEvent;
}

class Server
{
static void Main(string[] args)
{
Console.WriteLine("Server started\nListening...");
BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();
serverProvider.TypeFilterLevel = TypeFilterLevel.Full;
IDictionary props = new Hashtable();
props["port"] = 8888;
props["typeFilterLevel"] = TypeFilterLevel.Full;
TcpChannel chan = new TcpChannel(props, null, serverProvider);
ChannelServices.RegisterChannel(chan, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(ServerRemote),
"ServerRemote",
WellKnownObjectMode.Singleton);


Console.ReadLine();
}
}
}

Common object:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;

namespace REWFComm
{
public delegate void MessageHandler(string myStr, object Sender);
public interface ICommon
{
void Message(string passed, object Sender);
event MessageHandler MessageEvent;
}

public class Messages : MarshalByRefObject
{
public void MessageArrived(string myStr, object Sender)
{
((RichTextBox)Sender).AppendText(myStr); //-- HERE I GET THE EXCEPTION!!!!!
}
}
}


Client:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using REWFComm;

namespace Client
{
public partial class Main : Form
{
object formobj;

public Main()
{
InitializeComponent();
formobj = this.rtb_in;
}

private void btn_send_Click(object sender, EventArgs e)
{
ICommon obj = (ICommon)Activator.GetObject(typeof(ICommon), "tcp://localhost:8888/ServerRemote");
Messages ms = new Messages();
obj.MessageEvent+=new MessageHandler(ms.MessageArrived);
obj.Message(tb_out.Text,formobj);
}
}
}

modified on Saturday, January 3, 2009 2:13 PM

GeneralRe: Remote event update RichTextBox problem Pin
Luc Pattyn3-Jan-09 8:51
sitebuilderLuc Pattyn3-Jan-09 8:51 
GeneralRe: Remote event update RichTextBox problem Pin
ZarazaPhd3-Jan-09 9:10
ZarazaPhd3-Jan-09 9:10 
GeneralRe: Remote event update RichTextBox problem Pin
Luc Pattyn3-Jan-09 9:44
sitebuilderLuc Pattyn3-Jan-09 9:44 
GeneralRe: Remote event update RichTextBox problem Pin
ZarazaPhd3-Jan-09 10:59
ZarazaPhd3-Jan-09 10:59 
GeneralRe: Remote event update RichTextBox problem Pin
Luc Pattyn3-Jan-09 11:34
sitebuilderLuc Pattyn3-Jan-09 11:34 
GeneralRe: Remote event update RichTextBox problem Pin
ZarazaPhd3-Jan-09 11:47
ZarazaPhd3-Jan-09 11:47 
GeneralRe: Remote event update RichTextBox problem Pin
Luc Pattyn3-Jan-09 12:20
sitebuilderLuc Pattyn3-Jan-09 12:20 
GeneralRe: Remote event update RichTextBox problem Pin
ZarazaPhd4-Jan-09 3:04
ZarazaPhd4-Jan-09 3:04 
QuestionUsing Type and ConstructorInfo: Can't understand why this doesn't work... Pin
pikmindoctor3-Jan-09 4:19
pikmindoctor3-Jan-09 4:19 
AnswerRe: Using Type and ConstructorInfo: Can't understand why this doesn't work... Pin
User 66583-Jan-09 4:26
User 66583-Jan-09 4:26 
GeneralRe: Using Type and ConstructorInfo: Can't understand why this doesn't work... Pin
Luc Pattyn3-Jan-09 4:28
sitebuilderLuc Pattyn3-Jan-09 4:28 
QuestionQuestion on string.Format(...) Pin
Charith Jayasundara3-Jan-09 4:08
Charith Jayasundara3-Jan-09 4:08 
AnswerRe: Question on string.Format(...) Pin
Colin Angus Mackay3-Jan-09 5:21
Colin Angus Mackay3-Jan-09 5:21 
GeneralRe: Question on string.Format(...) Pin
Charith Jayasundara3-Jan-09 6:17
Charith Jayasundara3-Jan-09 6:17 
GeneralRe: Question on string.Format(...) Pin
Colin Angus Mackay3-Jan-09 6:32
Colin Angus Mackay3-Jan-09 6:32 
GeneralRe: Question on string.Format(...) Pin
Charith Jayasundara3-Jan-09 8:31
Charith Jayasundara3-Jan-09 8:31 
QuestionAutoincrement ID field in a xsd Pin
tharkaway3-Jan-09 2:50
tharkaway3-Jan-09 2:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.