Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Does this method use plenty of my memory resources?
Private MyWorkerClass worker;
Private Thread myWorkerThread;
//private Thread myWorkerThread= new Thread(worker.doThisWork); // i cant do this, because i cant restart the thread when i construct it here.
public void IwantMyWorkDosomething(){
myWorkerThread= new Thread(worker.doThisWork);
myWorkerThread.start();
myWorkerThread.stopWorking(); // stop my worker class thread running;
}
public void main(){
this.IwantMyWorkDosomething();
this.IwantMyWorkDosomething();
this.IwantMyWorkDosomething();
this.IwantMyWorkDosomething();
this.IwantMyWorkDosomething();
}
my Code is working, but i am not sure whether it gonna crash my program if i run the method 1000 times.
Constructing a thread object is cheap. Also, re-constructing a new one is cheap. The garbage collector will free the unused resources, you just need to make sure you're not unnessecarilly keeping references to finished thread objects.
What could become a resource problem is only when you try to run thousands of threads at the same time. But even then, it's not the memory that will usually cause the bottleneck but the CPU and the task scheduler (i.e the tasks will start to run slower than exected).
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 months ago.
Improve this question
I created a method that polls a database. If two instances of the exe are run, I wouldn't want both instances to be able to run the polling method simultaneously.
How might I best ensure the polling method is only ever active in one thread (regardless of which process owns the thread), and that if another thread calls it it will throw an exception?
I would use Mutex to avoid multiple access to the same recourses.
Here you have a brief piece of code to achieve this
Mutex mutex;
private void StartPolling()
{
mutex = new Mutex(true, "same_name_in_here", out bool createdNew);
if (!createdNew) { throw new Exception("Polling running in other process"); }
//now StartPolling can not be called from other processes
//polling stuff in here
}
private void StopPolling()
{
mutex?.Dispose();
mutex = null;
//now StartPolling can be called from other processes
//Stop any polling operation
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Is there any resources I can go to for tips on how to make a winForm with a long running operation?
My scenario is this. I have a WinForm that calls an API and stores data into a datagridview with multiple columns and about 10,000+ Rows.
I'm running multiple operations, such as counting distinct instances of a value in different columns and running a math operator to get a value. That application runs on about 2000 rows, but when I get to 4000, I get COM errors or other things stop it from executing.
I don't care if I have access to the UI, I just need the application to run.
Is there any resources I can go to for tips on how to make a winForm with a long running operation?
Sure, there are lots of resources for this sort of thing. You are more than likely looking for the BackgroundWorker. There is a great "code project" on it here.
public partial class Form1 : Form
{
BackgroundWorker backgroundWorker;
public Form1()
{
InitializeComponent();
backgroundWorker = new BackgroundWorker();
backgroundWorker.DoWork += OnDoWork;
}
void OnDoWork(object sender, DoWorkEventArgs e)
{
// Long running code here.
}
}
The BackgroundWorker supports cancellation, and even reporting of progress. Additionally, there are several other key events you would want to implement.
Ideally, you will clean up and dispose of the object when your form is done using it - additionally, be sure to unwire event handlers as part of the cleanup. Here is another resource.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have multiple threads in my program, one of which is operating on an internal data structure. Due to some error, this thread quits leaving the data structure in an invalid state. How can other threads properly validate the state of data structure on later access? In general, how to handle such a scenario?
The best answer is to make sure that threads don't quit leaving the data structure invalid. Other than that, the only solution is something like:
In class:
bool m_data_valid = true; // Or possibly 'false' and set it true in constructor
In mutating thread:
m_data_valid = false;
... // Mutate structure
m_data_valid = true;
In other threads:
if (!m_data_valid)
fixup(); // Or whatever you were going to do.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Please let me know if the following design is bad for managing heap /memory and from a design pattern point of view also.
Here I am using a C# Timers.Timer on a windows service where the time tick event fires on multiple threads.
Here my main point is about heap memory consumption , since lots of objects being created in different threads in micro sec and also we cannot guarantee when GC will collect them. so this can be harmful to heap and performance issue later to the application.Am i correct.
timer_tick()
{
Test objTst=null;
try
{
objTst=new Test();
objTst.Process();
}
catch(execption e){}
finally
{
objTst =null;
}
}
Please let me know following design is bad for manage heap /memory wise and design pattern wise also.
This "pattern" serves no purpose. Setting the variable to null is not required. As soon as the method completes, and objTst goes out of scope, it will be eligible for garbage collection, even if you don't set it to null.
I would also recommend not having an empty exception handler that just swallows and ignores exceptions entirely. If nothing else, you should at least log the exception you receive.
In general, I would write this as:
private void timer_Tick(object sender, EventArgs e)
{
var tester = new Test();
tester.Process();
}
If you have an exception logging mechanism, you could wrap it in a try/catch to handle or log the exceptions, but don't just swallow them completely.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am working on one project where I stuck on one point where I have to run two methods in parallel.
In Function 1
In my application what I am doing is I am grabbing images from the IP cam and storing that image into the one folder.
This function is used for continues streaming of camera.
For this you can refer this question which I have asked IP Camera stops streaming.
In Function 2
I will pick images from the path where my Function2 is dumping images.
Here I am doing some other operations like:
Save Image captured from the IP Camera
Detect faces in Image
Draw Face markers on Image
Some database based on result of Face Detection
Delete image File
Function 2 takes more execution time than Function 1.
So for this purpose after searching on google I get to know I can do this by multithreading.
So, I am little bit confused about this and as I am new in c# I am not that much aware of multithreading.
So, can anyone help me out on this?
You do indeed need to use multithreading, and in your case it should not be too difficult.
You'll need to add a "using System.Threading;" to the start of any files that involve threading.
public void Function1()
{
//Do camera stuff
Image image = MagicalCameraStuff();
//Create a thread that the processing will occur on
Thread process = new Thread(() => Function2(image));
//Start the thread
process.Start();
}
public void Function2(Image i)
{
//Do some processing without blocking the main thread
}
More information on threading:
http://msdn.microsoft.com/en-us/library/aa645740(v=vs.71).aspx