|
|
Adding to the first answer.
It allows to use closures which means you can use local state inside the lambda easily. This is very powerful and simplifies the code.
Best wishes,
Navaneeth
|
|
|
|
|
|
Hi,
I am working on a application that creates a report with pretty complicated graphics, which no doubt takes a lot of time. In order to convince the users that the program is actually working and has not hanged itself up, I thought of using a small window that shows a spinning gif image along with some message asking them to wait for the process to complete.
This is how I have implemented the functionality
class MainForm
{
Thread LenghtyOperationThread;
private void buttonCreateReport_Click(object sender, EventArgs e)
{
LengthyOperationThread = new Thread(() => { CreateReport(); });
RunLengthyThread();
ShowReport();
}
}
And this is the function that deal with showing the wait form
private void RunLengthyThread()
{
WaitForm wf = new WaitForm();
this.Enabled = false;
wf.Show();
LengthyOperationThread.Start();
LengthyOperationThread.Join();
wf.Close();
this.Enabled = true;
}
Everything works as I wanted, except a few minor bugs like the MainForm not getting focus after the WaitForm is closed.
The problems I am facing and don't have a clue about are these -
1. The gif image does not animate. All I can guess is that the WaitForm window is also waiting for the main thread to join the LenghtyOperationThread before animating the gif.
2. I cannot figure out a way to return focus to the MainForm window once the WaitForm window closes.
Help me out? Please?
SOLVED
Thanks for pointing out the errors. Both problems are now solved.
Modified code follows -
private void RunLengthyThread()
{
WaitForm wf = new WaitForm();
Thread WaitFormThread = new Thread(() => {
WaitForm wf = new WaitForm();
wf.ShowDialog();
});
LengthyOperationThread.Start();
LengthyOperationThread.Join();
try
{
WaitFormThread.Abort();
}
catch{ }
}
UPDATE. DON'T DO WHAT I DID ABOVE. THIS CODE WILL GIVE YOU Threading and GDI+ exceptions randomly. I am not sure how, but this code also causes the application to slow down after a few hours. Memory and processor usage will remain minimal, as seen in task manager or process hacker.
New code is posted below. Long story short, use a backgroundwoker.
modified 2-Mar-13 1:26am.
|
|
|
|
|
Amlanjyoti Saikia wrote: LengthyOperationThread.Join(); When this code gets executed, main thread will be blocked until the worker thread finishes execution. Which means none of the UI messages will be processed and that might be the reason why you don't see GIF file animating.
I'd offload the work of creating report to to the WaitForm and do a ShowDialog() instead of Show() for displaying the WaitForm . You can modify WaitForm 's constructor so that you can pass necessary information required for report generation. This should fix both of your problems.
Best wishes,
Navaneeth
|
|
|
|
|
The report is basically a graphics object (not a physical file) for viewing and printing. It has to be created and displayed in the MainForm, so offloading the task to the WaitForm is not possible.
Also, Using WaitForm.ShowDialog() sort of solves the problem, and is not blocked in the main thread, but then I have no way of closing the WaitForm.
However, you were right about the main thread being blocked. I wanted it to be blocked, until report creation is complete. But I had wrongly assumed that WaitForm.Show() would run in its own thread, like a dialog box.
Thanks for pointing it out. I have the solution now.
|
|
|
|
|
1) Run the lengthy thread "Async". As is now, I can see how "createreport" is running on it's own thread, but not the lengthy operation.
2) Try[^] Form.Show(this) . Once the Window closes, focus within the app should move to it's owner (if the app has focus).
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
1. Right on the money. CreateReport() is running on its own thread. But, WaitForm.Show() was not. The main thread blocked it from doing anything. And when as soon as it gets to do something, it was closed. I had stupidly assumed that Form.Show() runs on its own thread, like Form.ShowDialog(). Problem was I had no way of closing the DialogBox after CreateReport was done.
2. Taking care of the first problem took care of the second. Dark magic at work I say. .
Thanks for the idea of 'async' though. Couldn't use it because app has to run on .NET 3.5. So, created a new thread to show the WaitForm. Worked like a charm.
|
|
|
|
|
WRONG! You NEVER put any UI on the a seperate thread. What you did was put the UI on a seperate thread and then launched the long running work in yet another thread.
The way you've done it will appear to work fine but your code WILL run into problems every now and then that you can't replicate and will have a nasty time debugging. DO NOT PUT ANY UI IN A BACKGROUND THREAD!
Your long running work gets put on a background thread, such as using a BackgroundWorker, and then that frees up your UI thread to, well, update the UI. BackgroundWorker exposes a couple of events you can use to manage updating the UI without resorting to putting the UI in another thread.
|
|
|
|
|
Yep, my mistake. I ran into weird ThreadAbortExceptions and GDI+ errors.
Would really like to know why.
|
|
|
|
|
Well, the grossly simplified explanation is that Windows GDI library code that Graphics uses and all Forms and Controls use to draw themselves is not "thread-safe". The calls to various drawing and resource functions can step all over each other in very unsafe ways.
You can try to synchronize all of your resource and drawing calls, but the problem is that your application is not the only one using GDI when it's running, so it's nearly impossible to get a reliable multithreaded graphic routine running using GDI. The only reliable way to do multithreading GDI is to marshall all your control manipulation and drawing to methods on the UI thread.
|
|
|
|
|
My code was absolutely stupid and unnecessarily complicated. Brain fart...
This is the new code sample for anyone interested.
public class MainForm
{
WaitForm waitForm;
MainForm()
{
backgorundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bWorkerCompletedHandler_AllReports);
}
button1_ClickEventHandler(object sender, EventArgs e)
{
waitForm = new Waitform;
waitform.Show();
backgroundWorker1.DoWork += new DoWorkEventHandler(bWorkerHandler_Option1);
backgroundWorker1.RunWorkerAsync(ReportCategory1Object);
}
void bWorkerHandler_Option1(object sender, DoWorkEventArgs e)
{
Foo(e.Argument);
}
button2_ClickEventHandler(object sender, EventArgs e)
{
waitForm = new Waitform;
waitform.Show();
backgroundWorker1.DoWork += new DoWorkEventHandler(bWorkerHandler_Option2);
backgroundWorker1.RunWorkerAsync(ReportCategory2Object);
}
void bWorkerHandler_Option2(object sender, DoWorkEventArgs e)
{
Bar(e.Argument);
}
void bWorkerCompletedHandler_AllReports(object sender, RunWorkerCompletedEventArgs e)
{
backgroundWorker1 = new BackgroundWorker();
backgorundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bWorkerCompletedHandler_AllReports);
waitForm.Close();
}
void Foo(object Something)
{
}
void Bar(object AnotherThing)
{
}
}
|
|
|
|
|
hi to all..i need some help on how to access my ip camera in order to display stream on picturebox..please do help me... my camera has an ip 192.168.8.1..it can also be accessed through firefox by http://192.168.8.1 ..this is also my code,when i click the button, the picturebox should show the stream from the camera, but nothing happens..i am also using aforge.net library..
please check my code.. what do i missed? any help is greatly appreciated..thank you..
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge.Video;
namespace cameratest
{
public partial class Form1 : Form
{
MJPEGStream stream = new MJPEGStream("http://192.168.8.1");
public Form1()
{
InitializeComponent();
}
private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
pictureBox1.Image = eventArgs.Frame;
}
private void Form1_Load(object sender, EventArgs e)
{
stream.NewFrame += new NewFrameEventHandler(video_NewFrame);
}
private void button1_Click(object sender, EventArgs e)
{
stream.Start();
}
}
}
|
|
|
|
|
You already asked this in QA: how to access an ip camera using c# and display stream on picturebox?[^] - don't post it in multiple places, it duplicates work and wastes time. That makes it rude!
Remember it's a weekend, so there are a lot less people here than normal so responses will take longer.
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|
|
Sorry..i wont do it again..
|
|
|
|
|
hello every one i was a question about "property of network base software"
anybody can help me?
thank you....
|
|
|
|
|
Sorry but that makes no sense at all; please try rewording your question.
|
|
|
|
|
Excuse me,I want to learn about network software and how network software programing...?
-these software how to work?
and other questions...
|
|
|
|
|
First, you have to understand networking, which it doesn't sound like you do.
Read[^]
|
|
|
|
|
I have a OpenOffice Excel Sheet
that contains State,District,Taluka,Zone,Region and Territory
exprot these file into datatable or dataset
How it possible.
and while using open office in .net that contains any separate assembley.
Please give my full information.
|
|
|
|
|
You can read these into a dataset directly using OleDB; see these links[^] for some samples.
|
|
|
|
|
am using oledb
but am getting error
which assembly i need to use for OpenOffice.org 3.3
private void button1_Click(object sender, EventArgs e)
{
try
{
System.Data.OleDb.OleDbConnection MyConnection ;
System.Data.DataSet DtSet ;
System.Data.OleDb.OleDbDataAdapter MyCommand ;
MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\csharp.net-informations.xls';Extended Properties=Excel 8.0;");
MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection);
MyCommand.TableMappings.Add("Table", "TestTable");
DtSet = new System.Data.DataSet();
MyCommand.Fill(DtSet);
MyConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show (ex.ToString());
}
}
I am getting Microsoft.jet.OLEDB not registered error
Please Help me For the above Problem
|
|
|
|
|
allasanthosh wrote: I am getting Microsoft.jet.OLEDB not registered error You need to download it from the Microsoft website[^].
|
|
|
|
|
The Jet provider won't work if you're running this code on a 64-bit machine. Jet is 32-bit only.
So, you either have to install the 64-bit ACE engine, downloadable from here[^] or go into your project properties and set the Target CPU to "x86" instead of "AnyCPU". That will force your code to run as a 32-bit application.
|
|
|
|
|
I am working with shape files using windows application
my requirements are
1.excel sheet contains state,district,taluka,region,zone,territory
- export data from excel sheet to datatable/dataset
-Create shape file
-create three columns in shape file automatically
-compare taluka-region
taluka-zone
taluka-territory
only maching values we have to store already created three columns
Please any body help me for this requirement.
Thanks
Santhosh
|
|
|
|