[.NET WIndows Forms]

How to run a windows form in a new thread?

Page 1 of 1

9 Replies - 1798 Views - Last Post: 17 September 2008 - 01:40 PM Rate Topic: -----

#1 Zammy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 21-October 06

[.NET WIndows Forms]

Post icon  Posted 15 September 2008 - 01:24 PM

Topic description says it all, How to run a windows form in a new thread?

Thank you all.
Is This A Good Question/Topic? 0
  • +

Replies To: [.NET WIndows Forms]

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: [.NET WIndows Forms]

Posted 15 September 2008 - 01:43 PM

[rules][/rules]

For the record, the application runs in it's own thread, I'm not aware of a way to change this
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: [.NET WIndows Forms]

Posted 15 September 2008 - 01:48 PM

hitting F5 in Visual Studio. any executable will start on it's own thread.
Was This Post Helpful? 0
  • +
  • -

#4 Zammy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 21-October 06

Re: [.NET WIndows Forms]

Posted 16 September 2008 - 03:19 AM

I am sorry if the post seems like some freshman year school boy. Was little bit irritated at the time of the posting. I can upload the whole project to see it.

Basically I am doing an animation application (was plugin but now I gave up). It opens a file, you select the rectangles of the frames and you start it.
A new window pops (excatlly where I want new thread) which cuts the picture to pieces and plays the animation in it. Problem is that when I display a Form with .ShowDialog() my main application is not responsive anymore.

I was wondering if maybe I should do it with MDI so I can have support for multiple windows but it is not exactlly the case as the new window should be just one never mind how many files are opened.

I am not posting code here as I need to post at least 400 lines because of the code is spread among few classes. I have been implementing it as Paint.net plugin but got frustrated and I think I will do it in stand alone application. Ill rewrite it tonight (its Europe here).

I hope that post makes things clear, if you are still not convince ill post code soon.

Thank you all.
Was This Post Helpful? 0
  • +
  • -

#5 dineeshd  Icon User is offline

  • member icon

Reputation: 38
  • View blog
  • Posts: 619
  • Joined: 30-June 08

Re: [.NET WIndows Forms]

Posted 16 September 2008 - 03:39 AM

Why are you using form1.ShowDialog(), try using form1.show(). form1.ShowDialog() will show the child form as model dialog box, I think you don't required that.
Was This Post Helpful? 0
  • +
  • -

#6 Zammy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 21-October 06

Re: [.NET WIndows Forms]

Posted 17 September 2008 - 11:39 AM

When I use .Show() it just hangs on me.

Do you know how I can force constant repaint on a window form?

Here is the current version of the code. Everything is fine I just want to enforce constant repainting.

[csharp]

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;


namespace PNGPlugin
{
class AnimationWindow : System.Windows.Forms.Form
{
private Frame[] frames;
private Bitmap picture; // not really needed could be removed
private bool windowShown = false;

private PictureBox box;
private Stopwatch stopWatch;

private int passedTimeInMilliSeconds = 0;
private int currentFrame = 0;
private Bitmap[] images = null;

#region Properties

public Frame[] Frames
{
get { return frames; }
set
{
frames = value;
if (this.images == null)
{
this.images = new Bitmap[value.GetLength(0)];
}
}
}

public Bitmap Picture
{
get { return this.picture; }
set
{
picture = value;
for (int i = 0; i < images.GetLength(0); i++)
{
images[i] = Animationwindow.CropBitmap(value, frames[i].rec);
}
}
}

public bool WindowShown
{
get { return windowShown; }
}

private int CurrentFrame
{
get { return currentFrame; }
set { currentFrame = value % frames.GetLength(0); }
}

#endregion

public AnimationWindow()
: base()
{
//box = new PictureBox();
//this.Controls.Add(box);
stopWatch = new Stopwatch();
this.Shown += new EventHandler(OnShown);

}

void AnimationWindow_Shown(object sender, EventArgs e)
{
throw new Exception("The method or operation is not implemented.");
}

private void StartAnimation()
{
stopWatch.Start();
//box.Image = this.images[0];
this.BackgroundImage = this.images[0];
this.BackgroundImageLayout = ImageLayout.Stretch;
}

private void StopAnimation()
{
stopWatch.Stop();
}

protected override void OnPaint(PaintEventArgs e)
{
if (stopWatch.IsRunning)
{
passedTimeInMilliSeconds += stopWatch.Elapsed.Milliseconds;
}
if (frames != null)
{
if ((passedTimeInMilliSeconds >= frames[currentFrame].drawingTime))
{
passedTimeInMilliSeconds = 0;
CurrentFrame++;
this.BackgroundImage = this.images[currentFrame];
this.Refresh();
}
}
base.OnPaint(e);
}

private void OnShown(object o, EventArgs e)
{
this.windowShown = true;
this.StartAnimation();
}


protected override void OnClosed(EventArgs e)
{
this.windowShown = false;
this.StopAnimation();
base.OnClosed(e);
}


private static Bitmap CropBitmap(Bitmap inputImage, Rectangle rec)
{
Bitmap returnImage = new Bitmap(rec.Width, rec.Height);
for (int x = 0; x < rec.Width; x++)
for (int y = 0; y < rec.Height; y++)
{
returnImage.SetPixel(x, y, inputImage.GetPixel(x + rec.X, y + rec.Y));
}

return returnImage;
}
}
}
[/csharp]

This post has been edited by Zammy: 18 September 2008 - 03:37 AM

Was This Post Helpful? 0
  • +
  • -

#7 zakary  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Re: [.NET WIndows Forms]

Posted 17 September 2008 - 11:42 AM

but ShowDialog() works or hangs too
Was This Post Helpful? 0
  • +
  • -

#8 djkitt  Icon User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 189
  • Joined: 22-May 08

Re: [.NET WIndows Forms]

Posted 17 September 2008 - 01:20 PM

View PostZammy, on 17 Sep, 2008 - 01:39 PM, said:

Do you know how I can force constant repaint on a window form?
Here is the current version of the code. Everything is fine I just want to enforce constant repainting.


Try throwing Application.DoEvents() into your loop.
Was This Post Helpful? 0
  • +
  • -

#9 Zammy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 21-October 06

Re: [.NET WIndows Forms]

Posted 17 September 2008 - 01:24 PM

ShowDialog() works great but it does not give me any access to the parent window.
Was This Post Helpful? 0
  • +
  • -

#10 Zammy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 21-October 06

Re: [.NET WIndows Forms]

Posted 17 September 2008 - 01:40 PM

I have done this with Timer class. Excellent class for rising time dependent events! Cheers for the idea. I have posted the last version of the code. Works perfect.

ShowDialog() works great but it does not give me any access to the parent window.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1