|
If you want the value from outside the function to be changed within the function, use a reference type.
If you want to pass a value from outside the function but not let the function change the original then it needs to be a value type or a clone of the reference type.
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)
|
|
|
|
|
Hi Dave,
If i pass parameter as reference type, for eg. if i pass every thing as ref type then is i am saving memory because no extra memory is required in heap , if i pass parameter as ref.
Tell me i am correct or not.
“You will never be a leader unless you first learn to follow and be led.”
–Tiorio
"Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
|
|
|
|
|
I'm no expert in stack/heap stuff, so if what I type below is erroneous, would one of the CP gurus please correct it!
Value types are placed on the stack, reference types are placed on the heap but the reference to them are placed on the stack. A method's parameters are placed on the stack - either a copy of the value type, or a copy of the reference to the reference type, therefore the heap doesn't come into the equasion at all.
Extra stack space is taken whichever way you do it. Reference will be more efficient if you would otherwise be passing large structs (value types), but the stack is very efficient so I wouldn't worry too much.
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)
|
|
|
|
|
You inspired me to read up on this a little!
This[^] is a well explained article. Hope you find it to be of some use.
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)
|
|
|
|
|
This article supports that if you pass very big collection then u should pass by reference.
Am I rite.
“You will never be a leader unless you first learn to follow and be led.”
–Tiorio
"Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
|
|
|
|
|
Yeah. If passed by value, data is copied and you don't want to be creating multiple copies of a large amount of data. In that scenario it's better to pass by reference so only a copy of the reference is made.
If it's only a small amount of data then there may be a performance advantage to using a struct to keep the heap and GC out of it altogether.
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)
|
|
|
|
|
Hi
I have a windows service application that reads service 'StartTime' , 'IntervalTime' from configuration file and runs my method that do the job at specified time and time intervals. That works absolutely fine by reading the start time and Interval Time (interval time to wait to run the service again).
But the problem is when yearly timings changes. In United Kingdom we get timings change twice explained below for this year. This varies year to year:
Clocks went forward one hour on Sunday 30 March 01:00 GMT* (02:00 BST)
* As the UK is currently on GMT, we changed our clocks at 1 am on Sunday 30 March.
Clocks moved one hour backwards. Sunday 26 October 02:00 BST** (01:00 GMT )
**As the UK is currently on BST (British Summer Time), we will change our clocks at 2 am on Sunday 26th October.
How it works just now:
Currently my service starts at start Time specified in configuration file and again after specified interval Time in config file. For example 1000 is start time and interval Time[should be specified in milli seconds] is 86400000[Equals 24hrs]. My service starts at 1000 Today and runs and then sleep for 24hrs AGAIN runs at NEXT DAY 1000. During this 24hours sleeping if day light time is changing, my service will be out of sync.
Only change i like to be done is: Timer object checks system time every minute to find out if it is the time to run the service or not. That way we can hanlde day light savings time changes i think.
Note: May be by checking time every minute to see if current Time is the Time to run the service method or not.
1 public partial class myService : ServiceBase
2
3 {
4
5
6
7 System.Threading.Timer timer;
8
9 public myService()
10
11 {
12 InitializeComponent();
13 }
14
15 protected override void OnStart(string[] args)
16
17 {
18
19 try
20
21 {
22
23
24
25 string startTimeString = ConfigurationSettings.AppSettings ["StartTime"].ToString();
26
27
28
29 DateTime startTime;
30
31
32
33 int millisecondsToStart = 0;
34
35 if (DateTime.TryParseExact(startTimeString, "HHmm", CultureInfo.InvariantCulture, DateTimeStyles.None, out startTime))
36
37 {
38
39 if (DateTime.Now > startTime)
40
41 startTime = startTime.AddDays(1);
42
43 millisecondsToStart = (int)startTime.Subtract(DateTime.Now).TotalMilliseconds;
44
45 }
46
47 timer = new System.Threading.Timer(OnElapsedTime, null, millisecondsToStart, Convert.ToInt32(ConfigurationSettings.AppSettings["intervalTime"]));
48
49
50 }
51
52 catch (Exception ex)
53
54 {
55
56
57
58 System.Diagnostics.EventLog.WriteEntry("error in starting my Service", ex.Message + Environment.NewLine + ex.StackTrace);
59
60 }
61
62 }
63
64
65
66 protected override void OnStop()
67
68 {
69
70
71
72 }
73
74 private void OnElapsedTime(object source)
75
76 {
77
78
79
80 TransferData();
81 }
|
|
|
|
|
In the UK, although the date changes, the daylight saving time changes at 1am/2am on the LAST Sunday of March and October, so you should be able to detect when it happens.
Bob
Ashfield Consultants Ltd
|
|
|
|
|
Thanks for your reply Bob.
I like to write a generic service that handles day light savings. It doesn't matter when it will change.
Service needs to check system time every minute to find out if it is the time to run the service or not. That way even day light savings time changes, service runs at correct time without out of sync.
|
|
|
|
|
Is it possible to test and debug source code which deals with UI and non-UI thread exceptions using Application.ThreadException and AppDomain.CurrentDomain.UnhandledException events?
Regards,
matixsc
|
|
|
|
|
|
I'm not aware of a "space bar button" and I certainly don't have and ASAP.
Asking for an answer ASAP is rude. You need to clarify what you are clicking.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
sachinss1986 wrote: ASAP
Naughty - advice for your future at codeProject - don't use ASAP or Urgent or anything similar. Just ask your question politely with plenty of detail (but not too much) and your far more likely to get a quick answer.
I'm not sure why your rows are deleting. I don't use the dgv myself so not much experience with it but I'm sure it's not the default behaviour so maybe you have KeyDown/Up/Press handler somewhere that's doing this?
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)
|
|
|
|
|
Hi,
I'm wondering where a setting's value comes from, if it is not configured in MyApplication.exe.config.
Didn't find an answer at msdn yet, so could anyone explain please?
|
|
|
|
|
The default value is hard coded as an attribute on the property. Look in the Settings.Designer.cs file for the "DefaultSettingValueAttribute" attribute on each property.
Note that "user" scope settings can also be stored in a config file in the documents and settings/username/localsettings/application data folder.
Simon
|
|
|
|
|
Simon Stevens wrote: The default value is hard coded as an attribute on the property
Thanks, that's what I've been looking for.
|
|
|
|
|
I beleive the info is stored in the appconfig file. So when you right click your application in the explorer window and select properties, the settings tab allows you to store information in the app config table.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
<big><u><b>How can i restore my application from system tray if it is already running</b></u></big>. In my application i have coded to run my application at system startup in minimized state. So i want to maximize my application window when the user clicks the desktop shortcut icon. <u><b>I used Mutex method, ShowWindow P/Invoke method</b></u>. But I cant get the correct one. These two methods restore the application only if the application is minimized in taskbar. Any one can help me quickly.
|
|
|
|
|
Using the mutex is the correct way.
You need a delegate in your main form which calls a Restore method (Visible = true; WindowState = ... etc). Whilst checking your mutex, at the point where you decide you don't need the new instance, invoke the delegate.
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)
|
|
|
|
|
You can use mutex to detect whether you are first instance or not. For communicating with an already running process you can use either IPC or .NET Remoting. There are classes in .Net Framework for all of these.
|
|
|
|
|
Hello!
I use a program which runs several threads to simulate diffrent devices. Each thread simulate one device.
I have no problemmen staring the threads, but I don't know how to pause them. With pausing, I mean the thread should be stopped but not terminated.
Later in the program I wish to restart/continue the threads.
I have tried to use the suspend() and resume() methods, but I have read that those methods are not good to use for this. I also get irritating warnings when I compile the code, with susped() and resume() methods, which suggest I should use some other solution instead of suspend() and resume() methods.
This is my windows application code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
new Bar1("bar1");
new Bar2("bar2");
}
private void btnExec_Click(object sender, EventArgs e)
{
if (!DeviceBase.Run)
{
DeviceBase.execAll();
}
else
{
}
DeviceBase.Run = !DeviceBase.Run;
}
}
abstract class DeviceBase
{
public static Dictionary<string, DeviceBase> devices = new Dictionary<string, DeviceBase>();
private string name;
private Thread execThread;
protected static bool run = false;
public DeviceBase(string name)
{
this.name = name;
devices[name] = this;
devices[name].execThread = new Thread(exec);
}
public string Name
{
get
{
return name;
}
}
public static bool Run
{
get
{
return run;
}
set
{
run = value;
}
}
public static void execAll()
{
foreach (string device in devices.Keys)
{
if (devices[device].execThread.ThreadState == ThreadState.Unstarted)
{
devices[device].execThread.Start();
}
else
{
}
}
}
public static void pauseAll()
{
}
protected virtual void exec()
{
}
}
class Bar1 : DeviceBase
{
public Bar1(string name) : base(name)
{
}
protected override void exec()
{
while (run)
Console.WriteLine(Name + " is running");
}
}
class Bar2 : DeviceBase
{
public Bar2(string name) : base(name)
{
}
protected override void exec()
{
while (run)
Console.WriteLine(Name + " is running");
}
}
I have created two diffrent devices (instances) with the classes 'Bar1' and 'Bar2' in the 'Form1' constructor. Each device class derive from the abstract base class 'DeviceBase'' and each 'Bar-instance' overrides the base class method exec(). The 'exec()'-method is the simullating-method which each thread is using/running.
The thread is connected to the derivied 'exec()'-method when the base instance is created.
I use a button: 'btnExec', in the Windows Application form: ´Form1' to start/continue and pause the threads. The static method in the base class: 'execAll()' starts all threads, if the threads are in 'unstated'-state.
To pause the threads, I would like to use the static method: 'pauseAll()' and to continue the threads again I would like to use the 'execAll()'-method again.
How do I solve this?
|
|
|
|
|
you could change the run boolean to an enum that looks like this:
enum EXECUTION_STATE
{
RUNNING,
SUSPENDED,
STOPPED
}
so that your while loop could be changed to this (after renaming run to something more appropriate):
while (state != EXECUTION_STATE.STOPPED)
{
if (state == EXECUTION_STATE.RUNNING)
{
Console.WriteLine(Name + " is running");
}
else
{
Thread.Sleep(500);
}
}
|
|
|
|
|
Thanks, maybe sleep() method is the easiest solution.
But... I don't know how long the thread should be sleepiing/paused.
Maybe this works, if I add in the 'else' statement?
else
{
Thread.Sleep(Timeout.Infinite);
}
|
|
|
|
|
Mc_Topaz wrote: else
{
Thread.Sleep(Timeout.Infinite);
}
You can never resume from that sleep. I'd suggest using some sort of signaling mechanism, like ManualResetEvent[^]. Your code will look roughly like this
AutoResetEvent evt = new AutoResetEvent(false);
public void Pause()
{
executionState = PAUSED;
}
public void ThreadFunc()
{
while (true)
{
if (executionState == PAUSED)
evt.WaitOne();
}
}
public void Resume()
{
executionState = RUNNING;
evt.Set();
}
|
|
|
|
|
it looks like S. Senthil Kumar has a good idea. I just used a Sleep(500) because it's a quick and easy way to prevent the thread from hogging the processor when it doesn't need it. You can make it do whatever you need it to when it's paused.
|
|
|
|