TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Khalid Mushtaq
NA
7
18.2k
Application not responding while BackgroundWorker DoWork
Sep 21 2010 7:23 AM
HI,
I am using BackgroundWorker in scanning a document using C#.NET.
The problem is that while I call the BackgroundWorker.RunWorkerAsync() method, application doesn't respond. After finishing the do work it start responding again. As I know BackgroundWorker thread is used to work at background while user can do work in main UI thread... Please make me correct if i am wrong.
Here is the BackgroundWorker code.
BackgroundWorker backWorker = new BackgroundWorker();
backWorker.WorkerReportsProgress = true;
backWorker.DoWork += new DoWorkEventHandler(backWorker_DoWork);
backWorker.ProgressChanged += new ProgressChangedEventHandler(backWorker_ProgressChanged);
backWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backWorker_RunWorkerCompleted);
progressBar.Visible = true;
backWorker.RunWorkerAsync();
}
private void backWorker_DoWork(object sender, DoWorkEventArgs e)
{
try
{
List<Bitmap> docs = new List<Bitmap>();
string currFilename;
// Create a scanner instance (the user can select if more than one)
ItemClass scanner = (ItemClass)wiaManager.Create(ref missing);
// Show the standard scanning dialog (this is not a required step...)
CollectionClass scans = scanner.GetItemsFromUI(
WiaFlag.SingleImage, WiaIntent.ImageTypeText)
as CollectionClass;
// If the user clicks Cancel, collection is NULL
if (scans != null && scans.Count > 0)
{
// Transfer any scanned pictures to disk
ItemClass scan;
foreach (object wiaObj in scans)
{
scan = (ItemClass)Marshal.CreateWrapperOfType(wiaObj, typeof(ItemClass));
// create temporary file for image
currFilename = Path.GetTempFileName();
// transfer picture to our temporary file
scan.Transfer(currFilename, false);
// Create a Bitmap from the loaded file (Image.FromFile locks the file...)
using (FileStream fs = new FileStream(currFilename, FileMode.Open, FileAccess.Read))
{
// KLUDGE: Must wrap the FromStream Image with a new Bitmap.
// Otherwise get OutOfMemoryException later when using ColorMatrix on it.
docs.Add(new Bitmap(Image.FromStream(fs)));
fs.Close();
}
// Don't leave junk behind!
File.Delete(currFilename);
if (docs.Count > 0)
OpenImage(docs[0]);
}
}
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Sorry, image did not scan. Please try again.\n{0}", ex.Message), "Scanner error");
}
}
private void backWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
}
private void backWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
progressBar.Visible = false;
backWorker.ReportProgress(0);
}
The time where application is not responding to user is while executing the code that I have made bold
// transfer picture to our temporary file
scan.Transfer(currFilename, false);
The output is always ok. I just need to make application respond while scanning document at background.
Please help me if you can.
Thanks in advance.
Reply
Answers (
0
)
Bind Datagrid using Database in Silverlight
-- My First Project !! --