Help worker and crossflow problem

I have a Winforms application that works fine .. using BackgroundWorkerThread to control the usability of the GUI while processing serial data on the device.

It works great.

Now I am adding a new method and copying what I did in other forms. But I get a cross-thread exception.

I declare my BWT as follows:

BackgroundWorker bw = new BackgroundWorker();
            bw.WorkerSupportsCancellation = true;
            bw.DoWork += DownloadGpsDataFromDevice;
            bw.WorkerReportsProgress = true;
            bw.RunWorkerAsync();

Then I have a method like this that works in the background:

private void DownloadGpsDataFromDevice(object sender, DoWorkEventArgs e)
{
    _performScreenUpdate = true;
    tsStatus.Text = "Downloading GPS Data...";
    Invalidate();
    Refresh();


    Common.WriteLog("Extracting raw GPS data. Sending LL.");

    ReplyString raw = DeviceServices.ExecuteCommand("$LL");

DeviceServices.ExecuteCommand ("$ LL"); this is the bit that does this work, but I get an exception on the previous line, where I enter the text file. Now it makes you worry - write to a file. However, I have done this thousands of times in another BWT.

. Common.WriteLog:

public static void WriteLog(string input)
{
    lock (_lockObject)
    {
        WriteLogThreadSafe(input);
    }
}

private static void WriteLogThreadSafe(string input)
{
    Directory.CreateDirectory(LogFilePath);
    StreamWriter w = File.AppendText(LogFilePath + @"\" + LogFileName);
    try
    {
        w.WriteLine(string.Format("{0}\t{1}", DateTime.Now, input));
    }
    catch (Exception e)
    {
        System.Console.WriteLine("Error writing to log file!");
        System.Console.WriteLine("Tried to write: [" + input + "]");
        System.Console.WriteLine("Failed with error: [" + e.Message + "]");
    }
    finally
    {
        w.Close();
    }

}

. , . , - ?

+3
4

BackgroundWorker. , Invoke().

private void DownloadGpsDataFromDevice(object sender, DoWorkEventArgs e)
{
    _performScreenUpdate = true;
    Invoke((MethodInvoker)(() => {
             tsStatus.Text = "Downloading GPS Data...";
             Invalidate();
             Refresh();
     });
     ...
+6

, , UI:

DownloadGpsDataFromDevice

tsStatus.Text = "Downloading GPS Data...";
Invalidate();
Refresh();

BackgroundWorker run bw.ReportProgress(0);. ProgressChanged, .

void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    if (e.ProgressPercentage = 0)
    {
        tsStatus.Text = "Downloading GPS Data...";
        Invalidate();
        Refresh();
    }
}
0

. .

:

object locker = ();

SomeObject MyObject = new SomeObject();

private void FromMultipleThread()
{
    lock(locker)
    {
        MyObject = OtherObject;
    }
}

- ManualResetEvent. , WaitOne() ManualResetEvent, , .

Progress . ReportProgress , .

0

Are you sure this is the right line? I don’t think you can upgrade ui from your employee. Try commenting on the gui update and clear and create your solution to see if the problem is with the log. To update ui, install WorkerReportsProgress and create an event handler for this to update ui and report the progress to the worker.

0
source

All Articles