|
I guess you would have someone to answer you if you made your code available somewhere.
How low can you go ? (MS rant)
|
|
|
|
|
here you are, this is the library:
<br />
using System;<br />
<br />
namespace ChatLib<br />
{<br />
public class ChatMsg<br />
{<br />
public string content;<br />
public long authorid;<br />
public long messageid;<br />
<br />
public ChatMsg()<br />
{<br />
this.content = "";<br />
this.messageid = 0;<br />
this.authorid = 0;<br />
}<br />
public ChatMsg(string msg)<br />
{<br />
this.content = msg;<br />
this.messageid = 0;<br />
this.authorid = 0;<br />
}<br />
public ChatMsg(string msg, long number)<br />
{<br />
this.content = msg;<br />
this.messageid = number;<br />
this.authorid = 0;<br />
}<br />
public ChatMsg(string msg, long number, long id)<br />
{<br />
this.content = msg;<br />
this.messageid = number;<br />
this.authorid = id;<br />
}<br />
}<br />
<br />
}
this is the webservice:
<br />
using System;<br />
using System.Collections;<br />
using System.ComponentModel;<br />
using System.Data;<br />
using System.Diagnostics;<br />
using System.Web;<br />
using System.Web.Services;<br />
using ChatLib;<br />
<br />
<br />
namespace chatserver02<br />
{<br />
public class Service1 : System.Web.Services.WebService<br />
{<br />
<br />
private const int buffersize = 5;<br />
private static ChatMsg[] msgbuffer = new ChatMsg[buffersize];<br />
private static long lastid = 0;<br />
private static int lastpos = 0;<br />
<br />
private ChatMsg MsgNum(int pos)<br />
{<br />
if(pos >= buffersize)<br />
{<br />
ChatMsg nullmsg = new ChatMsg("", -1);<br />
return nullmsg;<br />
}<br />
int realpos = 0;<br />
realpos = (lastpos - pos)%buffersize;<br />
return msgbuffer[realpos];<br />
}<br />
<br />
public Service1()<br />
{<br />
InitializeComponent();<br />
}<br />
<br />
#region Component Designer generated code<br />
<br />
private IContainer components = null;<br />
<br />
private void InitializeComponent()<br />
{<br />
}<br />
<br />
protected override void Dispose( bool disposing )<br />
{<br />
if(disposing && components != null)<br />
{<br />
components.Dispose();<br />
}<br />
base.Dispose(disposing); <br />
}<br />
<br />
#endregion<br />
<br />
<br />
[WebMethod]<br />
public ChatMsg Horch(long lastmsg)<br />
{<br />
<br />
<br />
<br />
if(lastmsg < (lastid - buffersize))<br />
{<br />
lastmsg = lastid - buffersize;<br />
}<br />
<br />
ChatMsg returnmsg = new ChatMsg("", lastid);<br />
<br />
if(lastmsg < lastid)<br />
{<br />
while(lastmsg < lastid)<br />
{<br />
returnmsg.content = returnmsg.content + MsgNum((int)lastid - (int)lastmsg).content;<br />
lastmsg ++;<br />
}<br />
return returnmsg;<br />
}<br />
else<br />
{<br />
return new ChatMsg("", lastid);<br />
} <br />
}<br />
<br />
[WebMethod]<br />
public int Verkuend(string text)<br />
{<br />
lastid = lastid + 1;<br />
ChatMsg newmsg = new ChatMsg(text, lastid);<br />
newmsg.content = newmsg.content + "\n\n";<br />
lastpos = (lastpos + 1)%buffersize;<br />
msgbuffer[lastpos] = newmsg;<br />
return 0;<br />
}<br />
}<br />
}<br />
and this is the client:
<br />
using System;<br />
using System.Drawing;<br />
using System.Collections;<br />
using System.ComponentModel;<br />
using System.Windows.Forms;<br />
using System.Data;<br />
using ChatLib;<br />
<br />
namespace chatclient02<br />
{<br />
public class Form1 : System.Windows.Forms.Form<br />
{<br />
public static Form1 me;<br />
<br />
private long mylastmsg = 0;<br />
<br />
private System.Windows.Forms.TextBox inbox;<br />
private System.Windows.Forms.Button sendbutton;<br />
private System.Windows.Forms.TextBox outbox;<br />
private System.Windows.Forms.Button startbutton;<br />
private System.Windows.Forms.StatusBar feedbackbar;<br />
private System.ComponentModel.Container components = null;<br />
<br />
public Form1()<br />
{<br />
InitializeComponent();<br />
<br />
}<br />
<br />
protected override void Dispose( bool disposing )<br />
{<br />
if( disposing )<br />
{<br />
if (components != null) <br />
{<br />
components.Dispose();<br />
}<br />
}<br />
base.Dispose( disposing );<br />
}<br />
<br />
private void sendbutton_Click(object sender, System.EventArgs e)<br />
{<br />
localhost.Service1 pf = new localhost.Service1();<br />
<br />
AsyncCallback cb = new AsyncCallback(Form1.donothing);<br />
<br />
IAsyncResult ar = pf.BeginVerkuend(inbox.Text, cb, pf);<br />
<br />
<br />
<br />
<br />
inbox.Text = "";<br />
inbox.Focus();<br />
<br />
feedbackbar.Text = "sende botschaft...";<br />
}<br />
<br />
public static void donothing(IAsyncResult ar)<br />
{<br />
localhost.Service1 pf = (localhost.Service1) ar.AsyncState;<br />
me.feedbackbar.Text = "botschaft gesendet";<br />
}<br />
<br />
#region Windows Form Designer generated code<br />
private void InitializeComponent()<br />
{<br />
this.inbox = new System.Windows.Forms.TextBox();<br />
this.sendbutton = new System.Windows.Forms.Button();<br />
this.outbox = new System.Windows.Forms.TextBox();<br />
this.startbutton = new System.Windows.Forms.Button();<br />
this.feedbackbar = new System.Windows.Forms.StatusBar();<br />
this.SuspendLayout();<br />
this.inbox.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) <br />
| System.Windows.Forms.AnchorStyles.Right);<br />
this.inbox.Location = new System.Drawing.Point(0, 152);<br />
this.inbox.Multiline = true;<br />
this.inbox.Name = "inbox";<br />
this.inbox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;<br />
this.inbox.Size = new System.Drawing.Size(288, 48);<br />
this.inbox.TabIndex = 0;<br />
this.inbox.Text = "";<br />
this.sendbutton.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);<br />
this.sendbutton.Location = new System.Drawing.Point(144, 200);<br />
this.sendbutton.Name = "sendbutton";<br />
this.sendbutton.Size = new System.Drawing.Size(144, 32);<br />
this.sendbutton.TabIndex = 1;<br />
this.sendbutton.Text = "send";<br />
this.sendbutton.Click += new System.EventHandler(this.sendbutton_Click);<br />
this.outbox.AcceptsReturn = true;<br />
this.outbox.AllowDrop = true;<br />
this.outbox.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) <br />
| System.Windows.Forms.AnchorStyles.Left) <br />
| System.Windows.Forms.AnchorStyles.Right);<br />
this.outbox.BackColor = System.Drawing.SystemColors.Window;<br />
this.outbox.Multiline = true;<br />
this.outbox.Name = "outbox";<br />
this.outbox.ReadOnly = true;<br />
this.outbox.RightToLeft = System.Windows.Forms.RightToLeft.No;<br />
this.outbox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;<br />
this.outbox.Size = new System.Drawing.Size(288, 152);<br />
this.outbox.TabIndex = 2;<br />
this.outbox.Text = "";<br />
this.startbutton.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);<br />
this.startbutton.Location = new System.Drawing.Point(0, 200);<br />
this.startbutton.Name = "startbutton";<br />
this.startbutton.Size = new System.Drawing.Size(144, 32);<br />
this.startbutton.TabIndex = 3;<br />
this.startbutton.Text = "start session";<br />
this.startbutton.Click += new System.EventHandler(this.startbutton_Click);<br />
this.feedbackbar.Location = new System.Drawing.Point(0, 238);<br />
this.feedbackbar.Name = "feedbackbar";<br />
this.feedbackbar.Size = new System.Drawing.Size(288, 16);<br />
this.feedbackbar.TabIndex = 4;<br />
this.feedbackbar.Text = "...";<br />
this.AcceptButton = this.sendbutton;<br />
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);<br />
this.ClientSize = new System.Drawing.Size(288, 254);<br />
this.Controls.AddRange(new System.Windows.Forms.Control[] {<br />
this.feedbackbar,<br />
this.startbutton,<br />
this.outbox,<br />
this.sendbutton,<br />
this.inbox});<br />
this.MinimizeBox = false;<br />
this.Name = "Form1";<br />
this.Text = "Form1";<br />
this.ResumeLayout(false);<br />
<br />
}<br />
#endregion<br />
<br />
[STAThread]<br />
static void Main() <br />
{<br />
me = new Form1();<br />
Application.Run(me);<br />
}<br />
<br />
private void startbutton_Click(object sender, System.EventArgs e)<br />
{<br />
localhost.Service1 pf = new localhost.Service1();<br />
<br />
AsyncCallback cb = new AsyncCallback(Form1.incomming);<br />
<br />
IAsyncResult ar = pf.BeginHorch(mylastmsg, cb, pf);<br />
<br />
feedbackbar.Text = "erwarte daten von " + ar.ToString();<br />
}<br />
<br />
public static void incomming(IAsyncResult ar)<br />
{<br />
localhost.Service1 pf = (localhost.Service1) ar.AsyncState;<br />
ChatMsg ergebnis = pf.EndHorch(ar);<br />
<br />
<br />
if(me.mylastmsg != ergebnis.messageid)<br />
{<br />
me.feedbackbar.Text = "habe daten empfangen";<br />
me.mylastmsg = ergebnis.messageid;<br />
me.outbox.AppendText(ergebnis.content.Replace("\n","\r\n"));<br />
}<br />
<br />
me.feedbackbar.Text = "erwarte daten...";<br />
<br />
System.Timers.Timer aTimer = new System.Timers.Timer();<br />
aTimer.Elapsed+= new System.Timers.ElapsedEventHandler(OnTimedEvent);<br />
aTimer.Interval = 250;<br />
aTimer.AutoReset = false;<br />
aTimer.Enabled = true;<br />
}<br />
<br />
public static void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)<br />
{<br />
localhost.Service1 pf = new localhost.Service1();<br />
AsyncCallback cb = new AsyncCallback(Form1.incomming);<br />
<br />
IAsyncResult arn = pf.BeginHorch(me.mylastmsg, cb, pf);<br />
}<br />
}<br />
}<br />
hope this might help...
PS: the code is a bit messy i guess, parts of it are from the VS help, anyway, the compiler has its problem with this line, where i want to start the webmethod:
IAsyncResult ar = pf.BeginHorch(mylastmsg, cb, pf);<br />
|
|
|
|
|
I found a solution when i made a little exsample application concentrating on this whole using library business (for a prof who did not quite understand my problem):
It turns out to run properly when i refer to ChatMsg as localhost.ChatMsg (i think it's the namespace of the proxy) not using or refering any ChatLib in the client. This might not have worked for the original purpose because of the messy code, i have to tidy it up i guess
Greetings and sorry for the code
|
|
|
|
|
Hi! I've been working with C# for a while now but have hit a few snags...I would sure like info about the following things:
1. Hardware and C#
2. Accessing hardware serial numbers in C#
3. Computer System Hardware Classes and C#
4. Accessing Win32_BIOS - Win32_DiskDrive functions in C#
5. How to get a list of functions contained in a DLL file
Any help would be appreciated and thanks for the time spent on answering this beforehand...
Fahad
<fahad_khalil@msn.com>
|
|
|
|
|
For anything Win32 related you should go to MSDN[^] and check out Eric Gunnerson's latest article. He has a really good explanation of how to do Win32 stuff from C#.
As for the hardware related issues...I don't think you can do that in C#. I believe you would have to use, again, the Win32 API. However, you may not even be able to do that(You might have to go all the way down to Assembler).
And lastly, as for the DLL. You can use the System.Reflection.Assembly class to load a DLL and examine it's properties and methods. Even if it's a COM DLL, then .NET will create a wrapper for you and you'll be able to see the info that you want.
Hope this helps.
You will now find yourself in a wonderous, magical place, filled with talking gnomes, mythical squirrels, and, almost as an afterthought, your bookmarks
-Shog9 teaching Mel Feik how to bookmark
|
|
|
|
|
The fact you would use C# is questionable. You need low-level here, that's definitely not what .NET is about.
How low can you go ? (MS rant)
|
|
|
|
|
HI
Have alook at WMI and the System.Management namespace. There is an article on CP (but I'm to lazy to search) , just search for WMI C#.
I think that should provide all the answers u need
"There are no stupid question's, just stupid people."
|
|
|
|
|
I was reading some stuff on MSDN and I was wondering if anyone could tell me the difference between a property and a field?
|
|
|
|
|
I think not even MS can
lazy isn't my middle name.. its my first.. people just keep calling me Mel cause that's what they put on my drivers license. - Mel Feik
|
|
|
|
|
Well I looked but I couldnt understand why you would use a property instead of a field and visa-versa.
|
|
|
|
|
A property allows you to add additional login before getting or setting values. A field is for direct getting and setting of values.
|
|
|
|
|
|
Hi
Properties are a replacement for all those get/set methods we have in C++ / Java. It also allows you to expose a different type than the field you setting. AFAIK any fields should be never be public.
Hope this helps
"There are no stupid question's, just stupid people."
|
|
|
|
|
Wow, this seems to be a good definition!
lazy isn't my middle name.. its my first.. people just keep calling me Mel cause that's what they put on my drivers license. - Mel Feik
|
|
|
|
|
Yeah, its best to use properties rather than public fields. A property acts as an encapsulation around and a buffer between a classes private fields and the function accessing them.
Where with a public field all you can do is get and set:
class Test
{
public int ABool = 0;
}
Test t = new Test();
t.ABool = 1;
A property allows you to do many things before the field itself is set. A property also allows you to restrict the access to a field, allowing only getting or setting, if that is what your class requires:
class Test
{
private int aBool = 0;
public int ABool
{
set
{
// restrict the value of aBool to positive and zero
if (value >= 0)
{
aBool = value;
}
}
get
{
return aBool;
}
}
}
Test t = new Test();
t.ABool = -1;
|
|
|
|
|
Thanks for the code sample, it makes it much clearer to why properties would be usefull.
|
|
|
|
|
Thank you.
|
|
|
|
|
leppie wrote:
AFAIK any fields should be never be public
Unless they're readonly.
Kevin
|
|
|
|
|
Kevin McFarlane wrote:
Unless they're readonly.
More like const fields I would say. Read-only properties are quite common. Look at the TimeSpan and DateTime classes.
"There are no stupid question's, just stupid people."
|
|
|
|
|
If I understand the question correctly...this may give a good definition.
Property: This is exposed via public accessors (get/set) that allow a user to indirectly modify an internal variable. This process allows the developer to a) encapsulate the actual data b) include process against the data before it is set in the variable c) define that variable as read-only / write-only / read/write-capable.
Field: Directly exposes an internal variable as PUBLIC. This, to some extent, breaks encapsulation, does not keep control of the variable within the control of the object, and is not CLS Compliant. No process can be performed against the public property (such as security or business logic) and as such the value of the variable should be treated as suspect by the owning object.
Does this help?
---------------------------------------------
Once I thought I was wrong but I was happy to discover that was a mistake.
Condor
|
|
|
|
|
|
theRealCondor wrote:
Field: Directly exposes an internal variable as PUBLIC. This, to some extent, breaks encapsulation, does not keep control of the variable within the control of the object, and is not CLS Compliant. No process can be performed against the public property (such as security or business logic) and as such the value of the variable should be treated as suspect by the owning object.
I don't understand your statment, you can set the field to be public AND readonly in which case control of the variable is kept within the object that contains it.
ASP.NET can never fail as working with it is like fitting bras to supermodels - it's one pleasure after the next - David Wulff
|
|
|
|
|
You are correct in stating that I can do:
public readonly int myInteger
and that variable will be in my control as far as it being a computed field. But I still cannot maintain control over that property as to who can access this property, nor can I hide the implementation of the data.
I also was not looking at specific instance examples, but rather full utilization of either public accessors or exposed public fields. So from that all-or-nothing viewpoint, my statement still holds true. Just to make sure we are on the same page....this is the definition I go by:
Encapsulation means that a group of related properties, methods, and other members are treated as a single unit or object. Objects can control how properties are changed and methods are executed. For example, an object can validate values before allowing property changes. Encapsulation also makes it easier to change your implementation at a latter date by letting you hide implementation details of your objects, a practice called data hiding.
From this definition, even if I maintain some amount of encapsulation with the public readonly direction, I also do not hide the implementation of that data. As such, if that integer now has to be long, I just broke my consumer because they know my implementation is integer. Thus I have broken some form of encapsulation.
_____________________________________________
I have a tendancy to where my mind on my sleeve I have a habit of losing my shirt...
|
|
|
|
|