English 中文(简体)
C#reading - a range of thread, where each thread includes a form with an pornography
原标题:C# Threading - an array of threads, where each thread contains a form with an image

我有五条镜子。 每个校对都包含同样的形式,每个表格都放在不同地点进行屏幕上(按这种方法编制:P)。

我试图在填写表格之前装上其内容(图像)。 此时此刻,第一种形式的工程是空白的或消失的:

最初,每种形式都会被放置,但这种方法需要在显示所有形式内容之前完成。

感谢:

public partial class TrollFrm : Form
{
    int number = 0;

    public TrollFrm()
    {
        InitializeComponent();

        startThreads();

    }

    private void TrollFrm_Load(object sender, EventArgs e)
    {

    }

    private void TrollFrm_FormClosing(object sender, FormClosingEventArgs e)
    {
        e.Cancel = true;
    }

    public void startThreads()
    {
        Thread[] ThreadArray = new Thread[5];

        for (int i = 0; i < 5; i++)
        {
            ThreadArray[i] = new Thread(new ThreadStart(createForm));

            ThreadArray[i].Start();
        }
    }

    public void createForm()
    {
        Form frm = new TrollChildFrm();

        Random randomX = new Random();

        Random randomY = new Random();

        number++;

        int xValue;

        int yValue;

        if (number % 2 == 0)    //number is even.
        {
            xValue = (Convert.ToInt32(randomX.Next(1, 1920))) + 200;

            yValue = (Convert.ToInt32(randomY.Next(1, 1080))) - 200;
        }

        else    //number is not even.
        {
            xValue = (Convert.ToInt32(randomX.Next(1, 1920))) - 200;

            yValue = (Convert.ToInt32(randomY.Next(1, 1080))) + 200;
        }

        frm.Show();

        frm.Location = new Point(xValue, yValue);

        Thread.Sleep(1000);
    }
最佳回答

Your forms are not displaying correctly because they are not running on a thread with a message loop. The general rule is that all UI element accesses must occur on the main UI thread.

既然有人要求<代码> Thread.Sleep(1000) 我将假设,在最初展示每种形式之前,你要等待第二次。 在这种情况下,我将使用<代码>System.WindowsForms.timer。 页: 1 活动将直接打电话<代码>createForm。 时间,请见<条码>。 我认为,根本不需要制造任何透镜。

问题回答

The reason your forms aren t displaying is because you are running inside one method on the main UI thread. Instead, you could create a method that spawns a new form and launch that at certain intervals on another thread (making sure the form handling is done on the main UI thread). So you could do something like:

(Pseudo Code)
private const int TIME_THRESHOLD = 100;

int mElapsedTime = 0;
Timer mTimer = new Timer();

.ctor
{
    mTimer.Elapsed += mTimer_Elapsed;
}

private void mTimer_Elapsed(...)
{
    mElapsedTime++;

    if (mElapsedTime >= TIME_THRESHOLD)
    {
        mElapsedTime = 0;

        SpawnForm();
    }
}

private void SpawnForm()
{
    // Make sure your running on the UI thread
    if (this.InvokeRequired)
    {
        this.BeginInvoke(new Action(SpawnForm));
        return;
    }

    // ... spawn the form ...
}

这只是我所建议的一个例子,它并不像守则那样看待,而是应该给你一个执行步骤的想法。

I would suggest to use Thread.Sleep(1000) in this manner

电话科

    for (int i = 0; i < 5; i++)
    {
        ThreadArray[i] = new Thread(new ThreadStart(createForm));
        ThreadArray[i].Start();
    }
    Thread.Sleep(1000);

Also in the method that executing the work for the thread.

    while(!something)
    {
         Thread.Sleep(1000)
    }




相关问题
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. ...

热门标签