|
Hi,
is there a way to point with a string to a specific class?
I want deserialize a XML-Stream into a structure of variables.
I tried with
Score = (score)serializer.Deserialize(reader);
but all members of Score are null.
I suppose that the xml-file is not compatible with seriliation. So I want do the deserialisation by my own code.
This leeds to the question above: If I read an XML-element, how can I assign the XML-attributes to the approprieate element-class?
Ariadne
|
|
|
|
|
You can use the XmlElementAttribute and XmlAttributeAttribute to map specific XML elements and attributes to specific object fields and properties.
See also: Introducing XML Serialization[^]
-Phil
|
|
|
|
|
Class names are PascalCase and variable names are camelCase:
Score score = (Score)serializer.Deserialize(reader);
On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage
|
|
|
|
|
Hi,
I've got Creative Infra Remote and I want to get it working. Yes, it does have some program that comes with it, but I want to try to write my own. The remote is this one:
Creative Infra Remote
It's using COM port. I connected it and remote itself is working (I verified it by installing supplied software).
The problem is I cannot get it working with NET2.0. I've looked at several articles here on codeproject and on the web, I created test app, but it just won't work. I create SerialPort class using IO.Ports namespace, I open port and then I try to read data either on a button or using DataReceived event and yet, no matter what I try it won't work. The event never fires. I know port is opening because when I run software supplied with the remote and my application at the same time, whenever I try to open port in my test app it throws exception that COM1 port is already used and access is denied. However, even though the port is opened I cannot receive any data.
Am I supposed to receive any data at all using ReadLine or Read or ReadExisting methods? Or am I supposed to do something else? Can anyone point me in the right direction?
Thanks.
|
|
|
|
|
JazzJackRabbit wrote: The problem is I cannot get it working with NET2.0.
You can't get it working with your app, not .NET. Did you open the correct COM port? Did you use the correct parameters?? Baud rate, data bits, stop bits, flow control?
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Yes, the port is correct, COM1. I did not use any parameters since I don't know what they are supposed to be and all of the examples I've looked at use default (empty) constructor.
I actually don't know if I can get it to work at all. I've downloaded WinLIRC, just to see if it will be able to read data from remote, but it won't work either.
-- modified at 18:06 Saturday 30th December, 2006
OK, now I'm getting somewhere. Googled for CIMR100 which is the name of the receiver and used the following settings:
m_SerialPort = new SerialPort("COM1",9600,Parity.None,8,StopBits.One);
m_SerialPort.DtrEnable = true;
m_SerialPort.RtsEnable = true;
And it appears to work, at least DataReceived event is firing.
I get an error though: "Cross-thread operation not valid: Control 'txtInput' accessed from a thread other than the thread it was created on." when I try to show data which was read, but at least that's a start and I can work with it.
|
|
|
|
|
hi,
good that u got it working.
the last error u mentioned is a result of check by the runtime that a thread can access a control only if it created that control. possibly you are accessing txtInput created by 'Main' thread in some other thread. have a look at this.
http://msdn2.microsoft.com/en-us/library/ms171728(VS.80).aspx[^]
regards.
|
|
|
|
|
Yes, I've read this webpage already (that's what VS IDE advised to read when it displayed the error). I don't multithread for now and I don't think I will (doesn't make sense in an app like this, although it may change later). For now all I have is one main form frmMain, I create SerialPort object on the main form, I attach DataReceived event to the SerialPort object and let it be.
Basically something like this:
m_SerialPort = new SerialPort("COM1",2400,Parity.None,8,StopBits.One);
m_SerialPort.DtrEnable = true;
m_SerialPort.RtsEnable = true;
m_SerialPort.Open();
m_SerialPort.DataReceived += new SerialDataReceivedEventHandler(m_SerialPort_DataReceived);
void m_SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
txtInput.Text += " " + m_SerialPort.ReadExisting();
}
Funny, I only see this error when running application from VS IDE. If I run executable from release folder it runs just fine, no error thrown or nothing. This may sound dumb, but this is the first time I see an error like this when I simply access textbox from an event. Am I missing something here, have I been doing something fundamentally wrong for the past three years? And why do I only see this error when running IDE, not executable?
|
|
|
|
|
I expect most IO drivers to use at least one additional thread to handle incoming data:
it needs buffering, decoding, signalling, etc even when there is no client thread actively
asking for anything from the IO. The obvious way to achieve this is for the IO
code to create a local thread;
when something needs signalling (thru a callback function, an event
handler, whatever), I expect that to be handled by the same additional thread.
If you want to verify this theory, you should try to obtain a thread ID (I do not
know by heart how to do this with managed code), do it once in the GUI thread,
and once in the DataReceived handler, and compare them !
Even simpler, you could just print the value of someControl.InvokeRequired from
within the DataReceived handler; if it is true, the above has been proven.
Also the MSDN documentation clearly states "The DataReceived event is raised on a secondary thread when data is received from the SerialPort object".
(see ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.en/cpref8/
html/E_System_IO_Ports_SerialPort_DataReceived.htm)
So in order to access GUI elements from receive handlers (DataReceived, and similarly
PinChanged and ErrorReceived) you should use Invoke and/or BeginInvoke to delegate the GUI work to the right thread.
BTW, that your program works right under some circumstances does not prove it is
correct; especially threading and synchronization problems can be hidden and
only show under some circumstances. Only correct code works all the time.
-- modified at 22:34 Saturday 30th December, 2006
Luc Pattyn
|
|
|
|
|
OK, thanks a lot for explanation. I'll continue playing with it for the next couple of days/weeks, hopefully I won't lose my interest in it after a few days just like with many of my other projects
|
|
|
|
|
Hi I have and application that’s rather complex and componentized, but it wont work all the time when it’s all assembled and compiled. So let me try and get to the point of my problem. The application is composed of a series of threads coupled to some UI. Some of the UI has Socket implementation for talking to a remote device. Each UI component is a Windows Control and has properties and Functions for Access by the other threads running in the application. All the public functions call invoke internally to prevent any UI thread Problems and the Properties Use the Monitor lock to prevent Two threads from changing the properties wall reading or another thread changing them simultaneously. I have Blocking functions in the UI components that need them that look like this:
public override void WateForAck()
{
if (this.InvokeRequired)
this.Invoke(new WateForAckDelegate(WateForAck), new object[] { });
else
{
DateTime EndTime;
EndTime = DateTime.Now + (new TimeSpan(0, 0, 0, 0, viewSequencer.COMMAND_TIME));
while (true)
{
if (WaitHandle.WaitAll((new ManualResetEvent[] { this.BusyEvent }), new TimeSpan(viewSequencer.SAMPLE_TIME), false))
{
if (this.GetState() == Indecator.ESTATES.Error)
this.PostError(this.Channel, 0);
return;
}
if (EndTime < DateTime.Now)
{
Debug.WriteLine(String.Format("Ack timed out on: {0}, {1}.", this.Device, this.Channel));
this.PostError(this.Channel, 0);
this.BusyEvent.Set();
this.SetCursor(false);
return;
}
Application.DoEvents();
//Debug.WriteLine(String.Format("WateForAck on: {0}, {1}.", this.Device, this.Channel));
}
}
}
According to and article I read, on not using the “Application.DoEvents” as I could cose message prosesing problumes in my Application I should be able to elimanat this function call “Application.DoEvents”, but when I do the Sockit IO gets very slow and after a cople of minits fails to connect to the remote device. Question is do’s anyone now whay the “Application.DoEvents” function is interaction with the Socket IO? And is there a better way to let the Socket IO work in background so I can remove the “Application.DoEvents” function?
Guy Winslow
Software Engineer
GDW Engineering
gdwinslo@sonic.net
925-606-5944
|
|
|
|
|
I don't think the problem is any inherent link between Application.DoEvents() and socket I/O. Instead, I believe the problem is the WaitHandle.WaitAll() which blocks the UI thread. If your threads are performing socket I/O, then calling into your UI component which calls Invoke() to get to the UI thread, the WaitHandle.WaitAll() indirectly blocks your threads which will slow your socket I/O. (Remember that Invoke() will block the calling thread until the delegate method completes on the UI thread.) The reason why the Application.DoEvents() makes the application appear to work is that it allows another Invoke() to start executing (which might then call Application.DoEvents() again, and so on, and so forth). Usually this recursive process will end on its own and the Invoke() chain will complete and dissolve itself with no issues. However, the more nested calls there are, the more likely it is to result in a deadlock or other undesirable behavior. Hence, the use of Application.DoEvents() is discouraged.
The way to model your application is to have the threads fire events when certain things happen. The UI components can then subscribe to these events and update their display accordingly. The key thing is that the UI components never actively wait (i.e. block) on the UI thread for the events to occur. The threads themselves do the notifying. This keeps the application's message pump running smoothly.
As a side note, if your UI components properties are only set from the UI thread (i.e. from within an Invoke(), which is good form anyway), then there is no need for them to have locks because access is, by definition, serialized. Locks in this case would just slow things down unnecessarily.
-Phil
|
|
|
|
|
hi
i have a login control to my page, it's work good,but when i at runtime set RememberMe checkBox to true and refresh page or recompile it, the checkbox is empty and not remember me, how to solve this problem ?
thanks
|
|
|
|
|
Here's what I use.
A few things to note:
1. I started with http://www.codeproject.com/useritems/Remember_me_next_time.asp[^]
2. I didn't want the password stored, so that is commented out
2b. Since the password isn't available, the automatic login is commented out
3. You should probably change the name of the cookie
Enjoy
Brad
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.Cookies["LoginCookie"] != null)
{
HttpCookie cookie = Request.Cookies.Get("LoginCookie");
this.Login1.UserName = cookie.Values["ID"].ToString();
}
}
}
protected void Login1_LoggedIn(object sender, EventArgs e)
{
CheckBox rm = (CheckBox)Login1.FindControl("RememberMe");
if (rm.Checked)
{
HttpCookie myCookie = new HttpCookie("LoginCookie");
Response.Cookies.Remove("LoginCookie");
Response.Cookies.Add(myCookie);
myCookie.Values.Add("ID", this.Login1.UserName.ToString());
DateTime dtExpiry = DateTime.Now.AddDays(15);
Response.Cookies["LoginCookie"].Expires = dtExpiry;
}
}
}
|
|
|
|
|
Hi,
I am learning a little C#, and I would love to be able to create a little graphical form with a few buttons on it that will allow me to execute the XP / 2003 SHUTDOWN command.
Ideally I'll have just a handful of buttons, each named after a server, and hitting that button will execute the proper shutdown /r /m \\machinename command, assuming of course that the user has the appropriate permission to reboot \\machinename.
Can this be done? I would like any console access to be hidden, so no DOS box pops up, but that isn't really so important, I can figure out how to do that later.
For now I'm stumped about how to pass a DOS command from a C# GUI.
Would anybody be kind enough to toss me a bone?
Thanks!
|
|
|
|
|
You will need the Process and StartInfo classes, something like:
Process proc=new Process();
proc.StartInfo.FileName="my command";
proc.StartInfo.Arguments="my command arguments";
proc.StartInfo.CreateNoWindow=true;
proc.StartInfo.UseShellExecute=true;
proc.Start();
to organize some details, you may want to set other properties in StartInfo before
starting the process...
Luc Pattyn
|
|
|
|
|
Thank you - when I add this code into my Button, it complains:
Error 2
The type or namespace name 'Process' could not be found (are you missing a using directive or an assembly reference?) C:\<path edited="">Form1.cs
|
|
|
|
|
As it says, you need to add a using statement, in this case:
using System.Diagnostics;
near the other usings already in your file.
May I also suggest you read the MSDN documentation on any classes you decide to use...
Luc Pattyn
|
|
|
|
|
Interesting, that did get me closer to where I want to go, perhaps enough so that my limited knowledge can leverage the MSDN enough to get running.
Thank you very kindly Luc, I appreciate your helpful kindness.
|
|
|
|
|
Hello everyone,
My question is simple (-but the answer may not ) : is it possible to create folders (and/or files) that only allowed programs can access ?
For example; I want to hold some of my audio files in a special folder that only "myAudioPlayer" application can open..
Thanks for your helps and best regards !
Also have nice year !!!
.:: Something is Wrong ::.
|
|
|
|
|
I dont think so...
Luc Pattyn
|
|
|
|
|
The File System is responsible to protect files and folders. The access limitation is defined by account (Windows' users and groups) and permissions (read, write, delete, etc.). You cannot set access permission by application.
Not every file system supports file access protection. NTFS supports, FAT doesn't.
Ami
|
|
|
|
|
For example isn't it possible to set a registry value when you start your app and reset the value when you exit ? And if that registry value is empty the folder may not open ?
I am searching for a couple of hours but found nothing ..
Should I use the WinAPI ? Or the namespace extension, shell etc. ?
There must be something, and I am ready to spend my weeks on this topic...
.:: Something is Wrong ::.
|
|
|
|
|
pashje wrote: isn't it possible to set a registry value when you start your app and reset the value when you exit ? And if that registry value is empty the folder may not open ?
No, it's not. If your in Explorer or some other app, what's going to check that registry setting to see if it's OK to open the file??? Nothing!! You'd have to write an NTFS extension to implement this kind of functionality, and even then, it's easily defeated because it won't work on a FAT32 volume.
pashje wrote: I am searching for a couple of hours but found nothing
And you never will find anything, because it's not possible. Well, not without LOTS of extra work.
pashje wrote: There must be something, and I am ready to spend my weeks on this topic...
Quite wasting your time. This functionality won't exist unless you write an NTFS extension to do it. And even then, it won't work over networked folders.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Create a special user account and restrict access to the file and directories. Then, in your application, you can impersonate. It really won't stop anything but then again, any user with physical access can do anything they want anyway.
On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage
|
|
|
|
|