'How to return value after BackgroundWorkerCompletedEvent C#
I have this method in my program which returns boolean , but in the same method there's a background worker.
my goal is to be able to return the value of the boolean after the worker executed RunWorkerCompletedEvent. Instead it immediately returns the value of boolean even if the RunWorkerCcompletedEvent is not yet Executed
how can I achieve this?
screeenshot: Code
public bool LoadRequiredFont(string mode)
{
bool isLoaded = false;
try
{
#region "Font Loading Logic"
if (String.IsNullOrEmpty(mode) == false)
{
_mode = mode;
}
string file = path + _selectedFont;
if (_selectedFont != null || _selectedFont != string.Empty)
{
foreach (string line in System.IO.File.ReadLines(file))
{
countoflines = countoflines + 1;
if (file != string.Empty)
{
byte[] result = ReadGBMFile(line);
if (result != null)
{
FontImage image = new FontImage(result);
string[] command = image.GetCommand();
if (command != null)
{
commands.Add(command);
}
}
}
// }
}
string sampleCommandFile = path + _selectedFont.Substring(0, _selectedFont.IndexOf('.') + 1) + "TRY";
string[] sampleCommand = ReadFontSampleFile(sampleCommandFile);
if (sampleCommand != null)
{
commands.Add(sampleCommand);
}
#endregion
#region "Print Command
_worker = new BackgroundWorker();
_worker.DoWork += new DoWorkEventHandler(m_oWorker_DoWork);
_worker.WorkerSupportsCancellation = true;
_worker.WorkerReportsProgress = true;
_worker.ProgressChanged += new ProgressChangedEventHandler(m_oWorker_ProgressChanged);
_worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(m_oWorker_RunWorkerCompleted);
_worker.WorkerReportsProgress = true;
_worker.WorkerSupportsCancellation = true;
// _worker.RunWorkerAsync();
StartWorker();
#endregion
}
else
{
}
}
catch (Exception ex)
{
}
return isLoaded;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
