arcengine无法使用backgroundwork的解决方法

本文详细描述了一个在ArcEngine中遇到的问题,即由于某些算法模块不支持后台工作,作者通过创建线程并使用自定义Converter类实现了进程显示和进度更新。代码展示了如何启动线程,更新GUI以及在计算过程中检查中断情况。
摘要由CSDN通过智能技术生成

因为arcengine的一些算法模块实现是com模块不支持backgroundwork,想要进程展示就需要自己创建线程,参考大佬的代码进行了修改和实现。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        pgStatus.Maximum = 100;

    }

private volatile bool AbortOperation;
Func<bool> AbortOperationDelegate;
FinishProcessDelegate finishDelegate;
UpdateGUIDelegate updateGUIDelegate;

private delegate void UpdateGUIDelegate(int progress, object message);
private delegate void FinishProcessDelegate();
public delegate void ProgressEventHandler(int progress, string msg);

private void ProcessFinished()
{
    // Post processing.
    MessageBox.Show("执行完毕");
    return;
}

private void UpdateGUI(int progress, object message)
{
    // If the call has been placed at the local thread, call it on the main thread.
    if (this.pgStatus.InvokeRequired)
    {
        UpdateGUIDelegate guidelegate = new UpdateGUIDelegate(UpdateGUI);
        this.Invoke(guidelegate, new object[] { progress, message });
    }
    else
    {
        // The call was made on the main thread, update the GUI.
        pgStatus.Value = progress;
        lblStatus.Text = (string)message;   
    }
}


private void StartProcess()
{    
    // Update GUI.
    updateGUIDelegate(0, "Beginning process...");

    // Create object.
    Converter converter = new Converter(AbortOperationDelegate);
    // Parse the GUI update method to the converter, so it can update the GUI from within the converter. 
    converter.Progress += new ProgressEventHandler(UpdateGUI);
    // Begin converting.
    converter.Execute();

    // Tell the main thread, that the process has finished.
    FinishProcessDelegate finishDelegate = new FinishProcessDelegate(ProcessFinished);
    Invoke(finishDelegate);

    // Update GUI.
    updateGUIDelegate(100, "Process has finished.");
}


private void button1_Click(object sender, EventArgs e)
{
    // Create finish delegate, for determining when the thread is done.
    finishDelegate = new FinishProcessDelegate(ProcessFinished);
    // A delegate for updating the GUI.
    updateGUIDelegate = new UpdateGUIDelegate(UpdateGUI);
    // Create a delegate function for abortion.
    AbortOperationDelegate = () => AbortOperation;

    Thread BackgroundThread = new Thread(new ThreadStart(StartProcess));
    // Force single apartment state. Required by ArcGIS.
    BackgroundThread.SetApartmentState(ApartmentState.STA);
    BackgroundThread.Start();
}

private void button2_Click(object sender, EventArgs e)
{
    AbortOperation = true;
}

}

public class Converter
{
    private Func<bool> AbortOperation { get; set;}


    public Converter(Func<bool> abortOperation)
    {
        AbortOperation = abortOperation;
    }

    public void Execute()
    {
        // Calculations using ArcGIS are done here.
        int i = 0;
        Type factoryType = Type.GetTypeFromProgID("esriGeometry.SpatialReferenceEnvironment");

        System.Object obj = Activator.CreateInstance(factoryType);

        ISpatialReferenceFactory pfactory = obj as ISpatialReferenceFactory3;

        var selectReference = pfactory.CreateESRISpatialReferenceFromPRJFile(@"F:\proj\GCS_92xiamen.prj");
        var spatialReference = pfactory.CreateESRISpatialReferenceFromPRJFile(@"F:\proj\GCS_China_Geodetic_Coordinate_System_2000.prj");
        while(i<100) // Insert your own criteria here.
        {
            // Update GUI, and replace the '...' with the progress.
            OnProgressChange(new ProgressEventArgs(i, "Still working..."+i));

            //pp = GetGeo(tPntCollection.Point[i].X, tPntCollection.Point[i].Y);
            IPoint pt = new PointClass();
            pt.PutCoords(39594469.58710, 2713518.80515);
            IGeometry geo = pt;
            geo.SpatialReference = spatialReference;
            geo.Project(selectReference);//目标坐标系

            IPoint pp = geo as IPoint;

            Thread.Sleep(500);
            i++;

            // Check for abortion at anytime here...
            if (AbortOperation.Invoke())
            {
                MessageBox.Show("任务终止");
                return;
            }
        }
    }
    public DesktopWindowsApplication2.Form1.ProgressEventHandler Progress;
    public virtual void OnProgressChange(ProgressEventArgs e)
        {
            var p = Progress;
            if (p != null)
            {
                // Invoke the delegate. 
                p(e.Progress, e.Message);
            }
        }    
}

public class ProgressEventArgs : EventArgs
{
    public int Progress { get; set; }
    public string Message { get; set; }
    public ProgressEventArgs(int _progress, string _message)
    {
        Progress = _progress;
        Message = _message;
    }
}

参考于https://stackoverflow.com/questions/658301/threading-and-arcgis

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值