|
Hi Bruce,
I find a circular buffer much easier to understand and manage with a control variable storing the current data amount. With this additional piece of information in place the buffer full/empty state is obvious and the empty slot can be discarded.
Not tested but something like this:
class CAN {
public struct CANTXSTRUCT {
public int PGN;
public byte DLC;
public byte[] TxData;
}
private const int CANArraySize = 20;
static private int CANFront = 0;
static private int CANRear = 0;
static private int currentCount = 0;
static private CANTXSTRUCT[] arCANTx = new CANTXSTRUCT[CANArraySize];
static public void QueueCANTxMsg(int PGN, byte DLC, byte[] TxData) {
if (currentCount < arCANTx.Length) {
CANRear = (CANRear + 1) % arCANTx.Length;
currentCount++;
} else {
}
}
static public void RemoveMsg() {
if (currentCount > 0) {
CANFront = (CANFront + 1) % arCANTx.Length;
currentCount--;
} else {
}
}
}
Alan.
|
|
|
|
|
Hi Alan,
while I agree on two pointers plus amount being easier to understand, the nice thing about a circular buffer with only two pointers (and an empty slot) is that you don't need to lock if there is only one producer and one consumer: each pointer will have one writer and two readers, so when writing is atomic all is fine (when implemented carefully that is). Adding a currentCount makes the state redundant and prohibits atomic operations.
Remember, in a typical application, the consumer would be the app itself, the producer of incoming messages would be the CAN driver, which operates from an interrupt service routine (an asynchronous handler in .NET speak).
|
|
|
|
|
OnActivated()
MethodInvoker one function to initialize the data of the main form. And before beginInvoke, new the progress form B, the Form B to show the progress to the User. And there is a Timer in the form B, if the time is bigger than 2 seconds, the B Form will showdialog itself to forbide user operation. and then if MethodInvoke function finishs the task, it will stop the form B.
then the error shows. "Cross-thread access Form B from the thread is not the thread created by it."
the MethodInvoker is the another thread?
|
|
|
|
|
You can only change or interact with the user interface on the user interface's thread. If you want to change an element of the UI from another thread you have to do something like use the Invoke method on the Control that you wish to manipulate.
|
|
|
|
|
You mean that:
After initialize data, create a method invoke for the Form B stop function to stop FormB. Right?
I am to know what will do in the invoke method?
|
|
|
|
|
Look up the InvokeRequired property on MSDN or Google. You will find lots of examples there, together with a full explanation of what's going on and why.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
All timers tick on a separate thread, except for Windows.Forms.Timer
|
|
|
|
|
Hi All,
I'm just writing some core 'primitives' in a class library for data processing purposes. These primitives are things like Names, Addresses, Telephone numbers etc ... standard user data stuff.
At present I've defined them as structs but I'm wondering if this is the most inexpensive way to structure them. The objects mostly consist of strings and maybe a couple of other structs. The issue is that we are going to be processing millions of these and in most cases some bits require transmission over Wcf. At some points the Master object will pass into a process via Wcf a subset of its data and then process the results back into itself.
My concern comes from the differences between holding these as value types and by doing so forcing the runtime to make copies of the actual data rather than references. Once some data hits a DataContract it's effectively going to be copied again, sent to the process, that process will send it back only to be copied again when the master object processes the results.
I feel like I should rewrite them as classes, any input would be appreciated.
Cheers,
|
|
|
|
|
If each one is only going to contain a few value types then use a struct. If it's going to contain a large amount or any reference types (a string is a reference type that behaves like a value type) then use a class.
If you decide to use a class and you know it's going to need to be copied, then it may be worth implementing Clone and if needed DeepClone methods to provide suitable copies.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
DaveyM69 wrote: a string is a reference type that behaves like a value type
A string is simply immutable. Value types work best when they are immutable but they don't have to be.
|
|
|
|
|
Make them classes unless there's a good reason to make them structs.
Do you want the type to support null? -- Value types pretty much don't
Do you want to define operators for the type? -- I've found that operators make more sense on value types
Should the type be immutable? -- Value types generally tend toward immutability, but maybe that's due to operators
|
|
|
|
|
PIEBALDconsult wrote: Should the type be immutable? -- Value types generally tend toward immutability, but maybe that's due to operators
I would say the immutability aspect is fairly neutral. If it is mutable then I'd strongly recommend a class as value types are not good at mutability. But if it is immutable, could go either way for me - It wouldn't be a strong factor in making a choice.
|
|
|
|
|
hi everybody
I want to take my database backup which is created in sql server 2000 in windows application using C#
so i have no network its an independent system
i want to display all sqlserver instances in combobox
without internet(Lan etc..) how can i take backup my database using c#
hope you help me
thanks in advance
nagendra...
|
|
|
|
|
I don't know which bit you are needing help with. Listing the SQL Server instances or taking the backup.
You can use SMO (SQL Server Mangement Objects) to enumerate the available servers. http://msdn.microsoft.com/en-us/library/ms162169.aspx[^]
Since a SQL Backup can only be done to the machine that a SQL Server resides on a LAN isn't necessary. In fact, it won't make any difference since the backup file must be local to the SQL Server.
You can issue a backup command through the SqlCommand object in the .NET Framework and ExecuteNonQuery() .
|
|
|
|
|
I am having some issue handling an exception thrown in another thread in my UI. My question is how can I handle exceptions from another thread without syncronized the threads in the UI. I would prefer to sync the threads in the class where the method is called. Any suggestions?
Here is my code:
Localized fields
Test t = new Test();
Method that throws the exception
public class Test
{
public void DoSomething()
{
throw new MyCustomException(this,EventArgs.Empty);
}
}
The exception
public class MyCustomException : Exception
{
...
}
This is in the UI. Not multi-threaded
void CallException()
{
try
{
t.DoSomething();
}
catch(MyCustomException ex)
{
lblMessage.Text = "error";
}
}
This is also in the UI. Multi-threaded
void CallExceptionASync()
{
Thread thrd;
ThreadStart ts = (ThreadStart)delegate
{
t.DoSomething();
};
thrd = new Thread(ts);
thrd.Start();
}
|
|
|
|
|
I have created a workaround but I am not satisfied with it. If anyone has a better suggestion please let me know. Here is the workaround:
void CallExceptionASync()
{
Thread thrd;
ThreadStart ts = (ThreadStart)delegate
{
try
{
t.DoSomething();
}
catch(MyCustomException ex)
{
this.lblMessage.Invoke((MethodInvokder)delegate()
{
this.lblMessage.Text = ex.Message;
});
}
}
}
|
|
|
|
|
if the thread throws an exception, and the catch is to touch a GUI Control, then you need Control.Invoke, there is no way around that since that thread is not allowed to touch any Controls.
BTW: A BackgroundWorker has a Completed event which does the invoking automatically, and its event argument holds the exception that may have occurred.
|
|
|
|
|
i need to creat a windows application with many independent window usin the idea of master page in web development
i tried to use tab but its so bad
generaly i need one window that has a panel embedded into it
this panel contains all othe windows when selected from the main one
can any one auggest any idea??
|
|
|
|
|
I don't see what was wrong with the tab, unless you didn't want to see the tabs.
Each panel should contain one control, a user control that you can define elsewhere. That keeps the code cleaner rather than throw all the controls in one form. Does that help?
|
|
|
|
|
the problem is that
1.when i load the program for the first time all tabs will be loaded
and may be many tabs i will not need
2.not every thing will be valid for atab
i need a centeralized application from one windo
or u can say master window as i described
thanks for ur reply
|
|
|
|
|
You can dynamically add and remove tabs as you need them.
|
|
|
|
|
bad design i think
|
|
|
|
|
Why? Internet explorer and Firefox do it all the time. As I recall, Firefox was lauded for having tabbed windows that you create and dismiss at will.
|
|
|
|
|
|