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.
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 8 years ago.
Improve this question
I want to read sms from my GSM modem.
I wrote C# code.
This code run when I click start button.
I want to my program read sms when received, not click button.
thanks.
Your program is keyed to activate when you press a button, some method is called. You need to call this method when SMS data is received instead. This could be done using threads (SMS thread and main thread showing data) although it could just as easily be done using a cycle. In pseudo code:
while (don't quit) {
display page;
check for sms data;
sleep for small time to allow other OS programs to run also;
}
This is a "tight loop" and can use excessive amounts of CPU time depending on the code of the actual steps. For a tight program loop one simple method is to apply some sort of sleep method.
There are other ways to do the same thing, visitor pattern could probably be used, threads, etc...
It seems that you are only lacking the cycle. Your programs is probably more like:
while (don't quit) {
display page.
wait for button press.
}
although that flow wouldn't be obviously apparent at first glance without studying your program flow.
If you are using triggers (the button press is probably a trigger) you can trigger on a timer that fires as often as you want (100ms, 1 second, whatever) that checks for SMS data when fired, if there is SMS data it updates the form.
Many, many ways to do this. A quick Google for "program flow" doesn't find any useful links at first glance that would explain the many ways you could do this. Perhaps looking at other's code would help. I've often searched open source repositories for code I could look at to see how someone else did something.
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
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 9 years ago.
Improve this question
I need to generate a GUI for a WinForm in Thread1 using the result of Thread2.
However, Thread1 should start from Thread2.
If the result of Thread2 is equal to 1, I need to show a grid on my WinForm. If not, I need to show a tab control on my WinForm.
The UI thread must build it or atleast add it to the form.
But in Thread 2 you can collect all required information or maybe controls and pass them by invoke or as BackgroundWorker result to the UI thread.
You should only keep a single UI thread, and never create any UI elements in another thread.
Microsoft has many articles on this topic and demonstrates the correct way to handle async operations, such as
http://msdn.microsoft.com/en-us/library/ms951089.aspx
If I understood you correctly there is a need of creating GUI elements in one thread for later using them in GUI thread.
And as far as I remember it is not possible because control remembers the thread it was created on and later checks whether calls are made within that thread.
I'd recommend you to generate data only in background thread and pass it to gui thread where you can bind this data or generate ui elements to represent it. Moreover it conforms to the best practice of separation of concerns.
Lex Li has posted the link in comments to the question devoted to passing data to GUI thread.
I am not sure, I can understand your problem correctly but here is the code to create UI controls in a different thread.
var th = new Thread(() =>
{
//A sample form with a RichTextBox control.
var f = new Form();
f.Controls.Add(new RichTextBox() { Dock = DockStyle.Fill });
Application.Run(f);
});
th.SetApartmentState(ApartmentState.STA);
th.Start();
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).