BackgroundWorker not found

本文关键字:found not BackgroundWorker | 更新日期: 2023-09-27 18:09:32

我在windows phone 8.1中使用System.componentmodel引用来获得BackgroundWorker,但每次我把BackgroundWorker给我

错误CS0246类型或命名空间名称'BackgroundWorker'无法找到(您是否缺少using指令或程序集引用?)HealthBand C:'Users'husam'Desktop'Projects'HealthBand'HealthBand'ConnectionManager.cs 16

我尝试添加参考,但它说它已经添加当我输入

using System.ComponentModel;

它说没有必要使用指令

这是我的代码

using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Windows.Networking;
using Windows.Networking.Sockets;
using Windows.Storage.Streams;
using System.ComponentModel;
namespace HealthBand
{
/// <summary>
/// Class to control the bluetooth connection to the Arduino.
/// </summary>
public class ConnectionManager
{
    private BackgroundWorker dataReadWorker;
    /// <summary>
    /// Socket used to communicate with Arduino.
    /// </summary>
    private StreamSocket socket;
    /// <summary>
    /// DataWriter used to send commands easily.
    /// </summary>
    private DataWriter dataWriter;
    /// <summary>
    /// DataReader used to receive messages easily.
    /// </summary>
    private DataReader dataReader;
    /// <summary>
    /// Thread used to keep reading data from socket.
    /// </summary>

    /// <summary>
    /// Delegate used by event handler.
    /// </summary>
    /// <param name="message">The message received.</param>
    public delegate void MessageReceivedHandler(string message);
    /// <summary>
    /// Event fired when a new message is received from Arduino.
    /// </summary>
    public event MessageReceivedHandler MessageReceived;
    /// <summary>
    /// Initialize the manager, should be called in OnNavigatedTo of main page.
    /// </summary>
    public void Initialize()
    {
        socket = new StreamSocket();
        dataReadWorker = new BackgroundWorker();
        dataReadWorker.WorkerSupportsCancellation = true;
        dataReadWorker.DoWork += new DoWorkEventHandler(ReceiveMessages);
    }
    /// <summary>
    /// Finalize the connection manager, should be called in OnNavigatedFrom of main page.
    /// </summary>
    public void Terminate()
    {
        if (socket != null)
        {
            socket.Dispose();
        }
        if (dataReadWorker != null)
        {
            dataReadWorker.CancelAsync();
        }
    }
    /// <summary>
    /// Connect to the given host device.
    /// </summary>
    /// <param name="deviceHostName">The host device name.</param>
    public async void Connect(HostName deviceHostName)
    {
        if (socket != null)
        {
            await socket.ConnectAsync(deviceHostName, "1");
            dataReader = new DataReader(socket.InputStream);
            dataReadWorker.RunWorkerAsync();
            dataWriter = new DataWriter(socket.OutputStream);
        }
    }
    /// <summary>
    /// Receive messages from the Arduino through bluetooth.
    /// </summary>
    private async void ReceiveMessages(object sender, DoWorkEventArgs e)
    {
        try
        {
            while (true)
            {
                // Read first byte (length of the subsequent message, 255 or less). 
                uint sizeFieldCount = await dataReader.LoadAsync(1);
                if (sizeFieldCount != 1)
                {
                    // The underlying socket was closed before we were able to read the whole data. 
                    return;
                }
                // Read the message. 
                uint messageLength = dataReader.ReadByte();
                uint actualMessageLength = await dataReader.LoadAsync(messageLength);
                if (messageLength != actualMessageLength)
                {
                    // The underlying socket was closed before we were able to read the whole data. 
                    return;
                }
                // Read the message and process it.
                string message = dataReader.ReadString(actualMessageLength);
                MessageReceived(message);
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
        }
    }
    /// <summary>
    /// Send command to the Arduino through bluetooth.
    /// </summary>
    /// <param name="command">The sent command.</param>
    /// <returns>The number of bytes sent</returns>
    public async Task<uint> SendCommand(string command)
    {
        uint sentCommandSize = 0;
        if (dataWriter != null)
        {
            uint commandSize = dataWriter.MeasureString(command);
            dataWriter.WriteByte((byte)commandSize);
            sentCommandSize = dataWriter.WriteString(command);
            await dataWriter.StoreAsync();
        }
        return sentCommandSize;
    }
}
}

请告诉我如何修复这个错误

BackgroundWorker not found

BackgroundWorker在WP8.1中不支持。与其使用它,不如用Task将你的工作重定向到ThreadPool运行方法。正如文章用Async和Await进行异步编程所说:

基于异步的异步编程方法几乎在任何情况下都比现有的方法更可取。特别是,对于io绑定操作,这种方法比BackgroundWorker更好,因为代码更简单,而且您不必防范竞争条件。与Task结合使用。运行时,异步编程比BackgroundWorker更适合cpu密集型操作,因为异步编程将运行代码的协调细节与Task的工作分离开来。

更多的帮助,代码示例和比较,请访问Stephen Cleary的博客