|
Marc Clifton wrote:
some serial I/O code in C#
Marc...why aren't you just using the System.IO.Ports.SerialPort class?
When I can talk about 64 bit processors and attract girls with my computer not my car, I'll come out of the closet. Until that time...I'm like "What's the ENTER key?"
-Hockey on being a geek
|
|
|
|
|
Nice to know I'm on your "to read" list.
David Stone wrote:
Marc...why aren't you just using the System.IO.Ports.SerialPort class?
Because I'm not using Longhorn!
And, is there some reason they didn't inlude the parallel port in the Ports namespace? Oh yes, I forgot--Microsoft wishes the thing had never been invented!
Marc
Latest AAL Article
My blog
Join my forum!
|
|
|
|
|
Marc Clifton wrote:
Nice to know I'm on your "to read" list.
Heh...I was just browsing through the forums.
Marc Clifton wrote:
Because I'm not using Longhorn!
Well...you don't have to use Longhorn. You could just use Whidbey.
When I can talk about 64 bit processors and attract girls with my computer not my car, I'll come out of the closet. Until that time...I'm like "What's the ENTER key?"
-Hockey on being a geek
|
|
|
|
|
|
how to draw a X coordinate with a series of datetime data?
my question is mainly how to deal with the datetime and how to draw?
Please give me a sample about it! Thanks a lot!
|
|
|
|
|
Drawing is handled in the OnPaint of a control. Use DataTime to work with data time data. If you want to draw text you use DrawString() method. Doc's here: [^]
Docs for Graphics class : [^]
|
|
|
|
|
Hi All,
how do we interact 2 way BETWEEN A COM COMPONENT[say written in MFC COM] and a simple java class/EJB component.
say i wish to call a method in the java class from the COM component.
From COM component ,i pass it a number,the java class/component must multily it by say 10 and return the result back to COM component.
Is it possible.
Both reside on same machine.
Thanking you..
Regards
God Is Great
|
|
|
|
|
Yes you can. See the JNI reference at http://java.sun.com[^]. Please do not continue this thread, though. This is the C# forum, not the MFC, COM, or Java forum.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks for the link..ur right,i should have posted it in the java or COM forum
Cheers..
God Is Great
|
|
|
|
|
Hi!
May be yo can help me with this problem: I need to create a new instance of class (DLL or Windows Class file) that part of the project in a separate thread. Then I need to communicate with that class by passing some varibales or calling function/methods in this class. So question is: How I can do that (with multythreading) ?
I appreciate if you write me some code too - thanks
"I have not failed.
I've just found 10,000 ways that won't work."
- Thomas Alva Edison (1847-1931)
|
|
|
|
|
Jasper4C# wrote:
Then I need to communicate with that class by passing some varibales or calling function/methods in this class. So question is: How I can do that (with multythreading) ?
That's bit vague, so here goes:
1. look at the "lock" keyword in C# for preventing multiple threads from writing to a variable at the same time.
2. Look at the Mutex class for synchronizing between threads and blocking one thread while another thread is calling a method you want protected from simultaneous execution.
3. Look at AutoResetEvent and ManualResetEvent for signalling between threads.
Hope that helps!
Marc
Latest AAL Article
My blog
Join my forum!
|
|
|
|
|
10x for information but what I mean was - for example:
--------------------------------------------------------
using .... ; //doesn't matter
namespace DllClass
{
public class ClassA
{
private string m_message;
public ClassA()
{
//Empty constr (??)
}
public void ThreadEntrancePoint()
{
//This method will be called when creating
//a new thread
//TODO: Should I put here infinity loop ???
}
//This method will chande variable in this class
public string SetMessage
{
set
{ if (value != null) m_message = value; }
}
}
} // End of DLL file
---------------------------------------------------------
using ....; //All the other
using System.Threading;
using DllClass; //need to create class
namespace MyFormNameSpace
{
public class MyForm: Windows.Form
{
public int Main (...)
{
//Doesn't matter for now
}
//This function will be called when user want's
//to create a new thread
public void btn_CreateThread(...)
{
Thread t = new Thread (new ThreadStart DllClass.ClassA.ThreadEntrancePoint);
t.Start();
}
//This function will be called when user want
//to send a new messge to the class that inside
//thread
public void SendNewMessageToThread (string msg)
{
//TODO: What code should I put here to send
//message to ClassA.SetMessage ??
}
}//class close
}//namespace close
---------------------------------------------------------
You see - in this example I want to completly run ClassA in new thread and also pass some variables to this class and may be receive some inforamtion from it. How I can do that ?
"I have not failed.
I've just found 10,000 ways that won't work."
- Thomas Alva Edison (1847-1931)
|
|
|
|
|
Ok, first off, you can't call ThreadStart() the way you described because you need an instance of the class or the method you're calling has to be static.
If it's static, then anything it manipulates has to be static as well, and that means anything outside of the thread can access it as well, as long as it's static. If you made all the data and methods in ClassA static, then you could simply call ClassA.SetMessage from any thread--main application thread included.
If it's not static, then you need an instance of the object to pass along to the delegate. Since you have the instance, you can also call the SetMessage method from the application thread. For example:
ClassA myClassA=new ClassA();
ThreadStart threadDelegate=new ThreadStart(myClassA.ThreadEntrancePoint);
Thread newThread=new Thread(threadDelegate);
newThread.Start();
myClassA.SetMessage="foobar";
...
I hope that helps. Note that you probably would want a "lock" statement to bracket the setter.
Marc
Latest AAL Article
My blog
Join my forum!
|
|
|
|
|
Hello,
I a one of those bi-lingual programmers who use both VB.NET og C#. While both languages have their strengths and weaknesses I find that one of the thing I am missing the most in C# is background compile which VB.NET does.
Being very interested in the next version of the framework I figured that I could easily find out whether this feature would be included in this version. However, the answer eludes me.
Can anyone tell me if I should expect to see that particular feature in C# in the future release?
Thanks in advance.
|
|
|
|
|
Hello All,
I have an issue that is driving me crazy, no, not that kind of issue.
I have published an interface that I had intended to use to build objects for both client and server. The problem is, I cannot get my projects to recognize that I have declared an event, even though I can see it right there in the code.
A couple of scenarios:
1. If I don't declare an event, then I can use the published interface without any problems.
2. I can write the interface code directly in my project and again, no problem.
The problem with scenario one is that I need to declare events in my interface in order to be able to use them in both the client and the server.
The problem with scenario two is that now I am not publishing an interface, but rather declaring them in the code for both the client and the server objects. Crazy stuff eh
This is my published interface which is compiled into a separate assembly:
using System;
using System.Data;
namespace RemoteObjectInterface {
public delegate void TaskCompleted();
public interface IRemoteObject {
event TaskCompleted TaskComplete; // event to be raised or captured
void StartTask();
string GetHostLocation();
DataTable GetProducts();
}
}
In a separate project, I make a reference to this assembly,
and then create a class that implements it:
using System;
using System.Data;
using System.Timers;
using System.Data.SqlClient;
using RemoteObjectInterface; // Reference to compiled assembly
namespace RemoteObject {
public delegate void TaskCompleted(object sender, TaskCompleteEventArgs e);
public class ProductsDB : MarshalByRefObject, IRemoteObject { // declared implementing interface
private static string _connectDef = "Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI;";
public event TaskCompleted TaskComplete;
private Timer tmr = new Timer();
public void StartTask() {
tmr.Interval = 10000;
tmr.Elapsed += new ElapsedEventHandler(tmrCallback);
tmr.Start();
}
private void tmrCallback(object sender, ElapsedEventArgs e) {
tmr.Enabled=false;
if (TaskComplete != null) {
TaskComplete(this,new TaskCompleteEventArgs("Task completed on server"));
}
}
// Get the data from the Products table in the Northwind database and return them to the client.
public DataTable GetProducts() {
string SQL = "SELECT * FROM Products";
// Create ADO.NET objects.
SqlConnection cnProducts = new SqlConnection(_connectDef);
SqlCommand cmdProducts = new SqlCommand(SQL,cnProducts);
SqlDataAdapter daProducts = new SqlDataAdapter(cmdProducts);
DataSet dsProducts = new DataSet();
// Execute the command and retrieve the data.
try {
cnProducts.Open();
daProducts.Fill(dsProducts,"Products");
}
catch(Exception ex) {
Console.WriteLine(ex.ToString());
}
finally {
cnProducts.Close();
}
return dsProducts.Tables[0];
}
public override object InitializeLifetimeService() {
return null;
}
// This method allows you to verify that the object is running remotely.
public string GetHostLocation() {
return AppDomain.CurrentDomain.FriendlyName;
}
}
[Serializable]
public class TaskCompleteEventArgs : EventArgs {
private string _result;
public string Result {
get{return _result;}
}
public TaskCompleteEventArgs(string result) {
this._result = result;
}
}
}
If you attempt to compile this code, you receive this error:
'RemoteObject.ProductsDB' does not implement interface member 'RemoteObjectInterface.IRemoteObject.TaskComplete'. 'RemoteObject.ProductsDB.TaskComplete' is either static, not public, or has the wrong return type.
and yet this code with the interface included in the same assembly compiles perfectly fine.
using System;
using System.Data;
using System.Timers;
using System.Data.SqlClient;
namespace RemoteObject {
public delegate void TaskCompleted(object sender, TaskCompleteEventArgs e);
public interface IRemoteObject {
event TaskCompleted TaskComplete;
void StartTask();
string GetHostLocation();
DataTable GetProducts();
}
public class ProductsDB : MarshalByRefObject, IRemoteObject {
private static string _connectDef = "Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI;";
public event TaskCompleted TaskComplete;
private Timer tmr = new Timer();
public void StartTask() {
tmr.Interval = 10000;
tmr.Elapsed += new ElapsedEventHandler(tmrCallback);
tmr.Start();
}
private void tmrCallback(object sender, ElapsedEventArgs e) {
tmr.Enabled=false;
if (TaskComplete != null) {
TaskComplete(this,new TaskCompleteEventArgs("Task completed on server"));
}
}
// Get the data from the Products table and return them to the client.
public DataTable GetProducts() {
string SQL = "SELECT * FROM Products";
// Create ADO.NET objects.
SqlConnection cnProducts = new SqlConnection(_connectDef);
SqlCommand cmdProducts = new SqlCommand(SQL,cnProducts);
SqlDataAdapter daProducts = new SqlDataAdapter(cmdProducts);
DataSet dsProducts = new DataSet();
// Execute the command and retrieve the data.
try {
cnProducts.Open();
daProducts.Fill(dsProducts,"Products");
}
catch(Exception ex) {
Console.WriteLine(ex.ToString());
}
finally {
cnProducts.Close();
}
return dsProducts.Tables[0];
}
public override object InitializeLifetimeService() {
return null;
}
// This method allows you to verify that the object is running remotely.
public string GetHostLocation() {
return AppDomain.CurrentDomain.FriendlyName;
}
}
[Serializable]
public class TaskCompleteEventArgs : EventArgs {
private string _result;
public string Result {
get{return _result;}
}
public TaskCompleteEventArgs(string result)
{
this._result = result;
}
}
}
This code compiles fine.
Am I missing something here?
John A Vonesh
Senior Developer
Tran-Tech Inc.
BAIT, n. A preparation that renders the hook more palatable. The best kind is beauty
|
|
|
|
|
CyberHawke wrote:
This code compiles fine.
Am I missing something here?
Ew. Nothing like reading a lot of unformatted code in a message.
I'm right in the middle of writing a chapter for my book on remoting and have figured out a lot of things. If you want, email me the entire project--code, solution file, project files, and I'll take a look at today.
Marc
webmaster@knowledgeautomation.com
Latest AAL Article
My blog
Join my forum!
|
|
|
|
|
Hi All!!!
I have to design an application like access report designer. Where we would have drag n drop contorls like Text Boxes, Check Boxes, Radio Buttons, Labels etc and then have to preview them,select them, resize them and so on.
I was thinkin about Creating standard Contorls but then I realized that its difficult to handle them, for example if you click a textbox to select it for resizing, it will start funtioning, similary if u select checkbox to resize, it will eb checked. And their preview wasnt posisble a well.
So i created my own custom controls using GDI+ whose preview is also possible in PrintPreviewDialog.
Any help (even if is very basic and just provide an idea to take start) will be highly appriciated.
Thanx in Advance
sorry for my bad English.
|
|
|
|
|
Itanium wrote:
Any help (even if is very basic and just provide an idea to take start) will be highly appriciated.
When you click on the control, trap the click by handling the "OnClick" event. Draw a frame around the control and set the focus to the control. You might need to trap the message itself rather than the event and "steal" it from the control.
I've been wanting to write a generic form layout tool for ages, but have never gotten around to it.
Want to collaborate?
Marc
Latest AAL Article
My blog
Join my forum!
|
|
|
|
|
since he is going to use it in a report i think it would be unwise to use actual window controls.
it would be better to create your own gdi+ based 'controls' that render themselves onto a graphics/dc
using true controls he will get a hard time to render them in a good way to the printer , and if the report is big , he will endup with a zillion hwnds that eats resources..
if you want to do a form designer , you should take a look aT:
http://www.windowsforms.com/default.aspx?tabindex=5&tabid=47&ItemID=13&mid=142[^]
//Roger
|
|
|
|
|
Roger J wrote:
using true controls he will get a hard time to render them in a good way to the printer , and if the report is big , he will endup with a zillion hwnds that eats resources..
Those are great points. My thinking cap isn't properly attached this morning.
Marc
Latest AAL Article
My blog
Join my forum!
|
|
|
|
|
Hello Marc!!
Infact i started work on the deisgner few days back. I used the same way as Rojer told. I have made my own custom control classes hierarchy and renedring them both on design time and at preview.
Here i posted because( i was studying Java Reflection API's in previous days there we can have all classes , properties methods etc i htought that if anyone have used same kind of thing in .net (if available))
so that if i could found a better way but i came to know htat i am going in right direction.
You said about "Collboration"...
Sure ...why not...
sorry for my bad English.
|
|
|
|
|
Is there any prior Delphi developers here ? I'm trying to translate an program from Delphi to C#. Most of the program is translater now, only the worst part is shown here. P.S. Even if youre not familiar with Delphi, please take a look anyway ...
The problematic part in the code below is mostly the part GetBook procedure. It takes a record TBookInfo (struct in C#) from an array (ArrayList would have a best use in here) and creates and instance of TMyBook type and assignes it to a varable in the record. The tricky part here is that the instance is created the same type as another variable is - TMyBookClass (witch is class of TMyBook). I have no ideas how to do that in C#. Any ideas (some code would be very nice) ?
Can anyone help me with this ?
<br />
program Project;<br />
<br />
{$APPTYPE CONSOLE}<br />
<br />
uses<br />
SysUtils, Classes;<br />
<br />
type<br />
TMyBook = class<br />
private<br />
FName: string;<br />
public<br />
constructor Create(const AName: string); virtual;<br />
property Name: string read FName;<br />
end;<br />
<br />
TMyBookClass = class of TMyBook;<br />
TMyNewBook = class(TMyBook);<br />
<br />
TBookInfo = record<br />
Name: String;<br />
BookInstance: TMyBook;<br />
BookClass: TMyBookClass;<br />
end;<br />
<br />
var<br />
ActiveBook: TMyBook;<br />
Books: array of TBookInfo;<br />
<br />
constructor TMyBook.Create(const AName: string);<br />
begin<br />
FName := AName;<br />
end;<br />
<br />
function FindBook(const AName: string): Integer;<br />
begin<br />
for Result := 0 to Length(Books) - 1 do<br />
if CompareText(Books[Result].Name, AName) = 0 then Exit;<br />
Result := -1;<br />
end;<br />
<br />
procedure RegisterBookInfo(const AName: string; ABookClass: TMyBookClass);<br />
var<br />
Index: Integer;<br />
begin<br />
Index := Length(Books);<br />
SetLength(Books, Index + 1);<br />
with Books[Index] do<br />
begin<br />
Name := AName;<br />
BookClass := ABookClass;<br />
BookInstance := nil;<br />
end;<br />
end;<br />
<br />
function GetBook(const AName: string): TMyBook;<br />
var<br />
Index: Integer;<br />
begin<br />
Index := FindBook(AName);<br />
with Books[Index] do<br />
begin<br />
Assert(BookInstance = nil);<br />
BookInstance := BookClass.Create(Name);<br />
Result := BookInstance;<br />
end;<br />
end;<br />
<br />
procedure SetActiveBook(const ABookName: string);<br />
begin<br />
ActiveBook := GetBook(ABookName);<br />
end;<br />
<br />
begin<br />
RegisterBookInfo('New Book', TMyNewBook); <br />
SetActiveBook('New Book');<br />
end.<br />
Regards, Desmond
|
|
|
|
|
It looks like all the procedures and functions are designed to perform actions on array elements or instances of TMyBookClass. The array Books contains a set of records, but each record contains a pointer to a BookInstance and a BookClass. So, for example, the GetBook method find the array index of a book whose name is stored in AName, and then, with that array element, it sets the BookInstance pointer to a new TMyBookClass and returns that BookInstance.
But that's just my opinion, I could be wrong.
|
|
|
|
|
And how exacly could I do that in C# (could you show it to me in C# please) ?
|
|
|
|
|
OK, just for fun, I re-wrote your delphi code in C#. Not that I'm sure this implementation is even correct, or that it does anything desirable, but it will compile. So, in TMyBookClass.cs, we have:
using System;
using System.Collections;
namespace Books
{
public abstract class TMyBook {
string FName;
public abstract TMyBook Create(string AName);
public virtual string Name {
get { return FName; }
set { FName = value; }
}
}
public class TMyBookClass : TMyBook {
TMyBook ActiveBook;
ArrayList Books = new ArrayList();
public struct TBookInfo {
public string Name;
public TMyBook BookInstance;
public TMyBookClass BookClass;
}
public TMyBookClass(string AName){
this.Create(AName);
}
public override TMyBook Create(string AName) {
this.Name = AName;
return this;
}
public int FindBook(string AName) {
for(int result = 0; result < Books.Count; result++) {
TBookInfo bi = (TBookInfo)Books[result];
if (bi.Name == AName) { return (result); }
}
return -1;
}
public void RegisterBookInfo(string AName, TMyBookClass ABookClass) {
TBookInfo bi = new TBookInfo();
bi.Name = AName;
bi.BookClass = ABookClass;
bi.BookInstance = null;
Books.Add(bi);
}
public TMyBook GetBook(string AName) {
int index;
index = FindBook(AName);
TBookInfo bi = (TBookInfo)Books[index];
return bi.BookClass.Create(Name);
}
public void SetActiveBook(string ABookName) {
ActiveBook = GetBook(ABookName);
}
}
}
Then, in the main class of a console application, you could...
TMyBookClass TMyNewBook = new TMyBookClass("New Book");
TMyNewBook.RegisterBookInfo("New Book", TMyNewBook);
TMyNewBook.SetActiveBook("New Book");
|
|
|
|