English 中文(简体)
电话/传真 新版的保险单
原标题:Calling/Modifying UI from new thread
  • 时间:2011-11-23 23:41:56
  •  标签:
  • c#

我愿 pro诚地创造新的通道,以方案方式创建浏览器,并在浏览器表内进行活动。 问题在于,我得到的信息是,我不能履行在母系对国际不动产的影响。

因此,我需要找到一种办法,告诉新的线索,以使用主线的国联物体。 这应当容易去做?

在这里,迄今为止,我有幸与以下各方合作:

//....to this point we have told the code to run selected items in a datagrid. Each will have it s own thread.


            if (row.Cells[1].Value.ToString() == "True")
            {
                count_active++;
                //begin multithreading
                Thread this_thread = new Thread(() => f_play_campaign(row.Cells[0].Value.ToString(), row.Cells[2].Value.ToString()));
                this_thread.Name = "thread_" + row.Cells[0].Value.ToString();
                this_thread.IsBackGround = true;
                this_thread.Start();

            }

        }

        if (count_active == 0)
        {
            MessageBox.Show("No campaigns are selected!");
        }
    }


    private void f_play_campaign(string project_id, string tab_name)
    {
        //MessageBox.Show(tab_name);
        //add new tab
        string browsername = f_create_new_tab(tab_name); //this is where the code breaks!
        Control browser = f_get_control_by_name(browsername);

        //MessageBox.Show(browser.ToString());

        //... do more code

能否轻易地告诉人们,谁可以使用主线的质子? 在Im试图从我的手法中获取回报价值时,我无法说明如何使用Invoke()和Im如此新的方法,因此我没有像现在这样说明如何使用背景工人。

我继续阅读关于这一问题的其他思路,但希望有人会知道一种非常容易的、符合像我这样一位“加拿大”方案人的棘手解决办法。

最佳回答

I had to deal with this issue a few days earlier. It s fairly easy to solve:

Thread this_thread = new Thread(() =>
    this.Invoke((MethodInvoker)delegate
    {
        f_play_campaign(row.Cells[0].Value.ToString(), row.Cells[2].Value.ToString();
    }));

基本上,你需要通过<代码>Invoke aMethodInvoker 代表,该代表被定义为public delegation voidmethInvoker()。

如果您希望使用<代码>BackgroundWorker。 它非常相似:

 BackgroundWorker worker = new BackgroundWorker();
 worker.DoWork += (s, e) =>
 {
     this.Invoke((MethodInvoker) delegate { MyJobHere(); });
 };

 // Do the work
 worker.RunWorkerAsync();

如果你想回去某种价值的话,你可以这样做:

BackgroundWorker worker = new BackgroundWorker();
int value;
worker.DoWork += (s, e) =>
{
    this.Invoke((MethodInvoker) delegate { value = DoSomething(); });
};

worker.RunWorkerAsync();


// do other processing
while (worker.IsBusy)
{
    // Some other task
}

// Use the value
foo(value);
问题回答

增加新的方法:

    private void f_play_campaign_Async(string project_id, string tab_name)
    {
        Action<string, string> action = f_play_campaign; 
        this.Invoke(action, project_id, tab_name);
    }

a. 修改该雷达的施工,改用这种方法:

Thread this_thread = new Thread(() => f_play_campaign_Async(row.Cells[0].Value.ToString(), row.Cells[2].Value.ToString()));




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签