473,406 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Threads, textboxes and scrolling

Threads, textboxes and scrolling

Thanks in advance for any information on this matter. I have run into
a small richtextbox, scrolling and tread issue. Which has me totally
confused. Instead of trying to explain it I wrote some small C# code
to demonstrate the fact that in the threaded version the textbox
simple doesn't scroll like it should… please look to the
startButton_Click method for the distinction between the two
programs..

Thanks

Wesley

why does this work ?????????????
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Data;

namespace Test
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Test : System.Windows.Forms.Form
{
const string newline = "\r\n";

/// <summary>
/// Required designer variable.
/// </summary>

private System.Windows.Forms.Button startButton;
private System.Windows.Forms.RichTextBox output;
private System.ComponentModel.Container components = null;

public Test()
{
InitializeComponent();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Test());
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.startButton = new System.Windows.Forms.Button();
this.output = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// startButton
//
this.startButton.Anchor = (System.Windows.Forms.AnchorStyles.Top |
System.Windows.Forms.AnchorStyles.Right);
this.startButton.Location = new System.Drawing.Point(424, 8);
this.startButton.Name = "startButton";
this.startButton.TabIndex = 0;
this.startButton.Text = "Start";
this.startButton.Click += new
System.EventHandler(this.startButton_Click);
//
// output
//
this.output.Anchor = (((System.Windows.Forms.AnchorStyles.Top |
System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.output.Location = new System.Drawing.Point(0, 40);
this.output.Name = "output";
this.output.Size = new System.Drawing.Size(512, 296);
this.output.TabIndex = 1;
this.output.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(512, 334);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.output,
this.startButton});
this.Name = "Form1";
this.Text = "Backup Utility";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// this method takes the giving string and writes it to the
richtextbox.
/// this method appends a new line at the end of the input string to
facilite
/// returns and scrolling to the botton.. callers need not add a
newline.
/// </summary>
/// <param name="msg">string</param>
private void DisplayNewEvent(string msg)
{
output.AppendText(msg + newline);
output.Focus();
output.SelectionStart = output.Text.Length + 1;
output.SelectionLength = 0;
output.ScrollToCaret();
}

private void startButton_Click(object sender, System.EventArgs e)
{
GoThroughMyDocuments();
}

/// <summary>
/// this functions goes through all the folders and files and places
the
/// filename in the richtext box...
/// </summary>
private void GoThroughMyDocuments()
{
DirectoryInfo info = new DirectoryInfo(
@"C:\Documents and Settings\" +
SystemInformation.UserName +
@"\My Documents");

RecursivelyTransverseFilesAndFolders(info.GetDirec tories());
GetFileName(info.GetFiles());
}

//recursively find all the files in all directories in "My
Documents"
private void RecursivelyTransverseFilesAndFolders(DirectoryInfo[]
directories)
{
if(directories!=null)
foreach(DirectoryInfo dir in directories)
{
RecursivelyTransverseFilesAndFolders(dir.GetDirect ories());
GetFileName(dir.GetFiles());
}
}

// get each file name and print its full path to the text box
private void GetFileName(FileInfo[] info)
{
if(info!=null)
foreach(FileInfo file in info)
DisplayNewEvent(file.FullName);
}
}
}
and this not work??????????

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Data;
using System.Threading;

namespace ThreadTest
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class ThreadTest : System.Windows.Forms.Form
{
const string newline = "\r\n";

Thread worker1;
/// <summary>
/// Required designer variable.
/// </summary>

private System.Windows.Forms.Button startButton;
private System.Windows.Forms.RichTextBox output;
private System.ComponentModel.Container components = null;

public ThreadTest()
{
InitializeComponent();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new ThreadTest());
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.startButton = new System.Windows.Forms.Button();
this.output = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// startButton
//
this.startButton.Anchor = (System.Windows.Forms.AnchorStyles.Top |
System.Windows.Forms.AnchorStyles.Right);
this.startButton.Location = new System.Drawing.Point(424, 8);
this.startButton.Name = "startButton";
this.startButton.TabIndex = 0;
this.startButton.Text = "Start";
this.startButton.Click += new
System.EventHandler(this.startButton_Click);
//
// output
//
this.output.Anchor = (((System.Windows.Forms.AnchorStyles.Top |
System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.output.Location = new System.Drawing.Point(0, 40);
this.output.Name = "output";
this.output.Size = new System.Drawing.Size(512, 296);
this.output.TabIndex = 1;
this.output.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(512, 334);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.output,
this.startButton});
this.Name = "Form1";
this.Text = "Backup Utility";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// this method takes the giving string and writes it to the
richtextbox.
/// this method appends a new line at the end of the input string to
facilite
/// returns and scrolling to the botton.. callers need not add a
newline.
/// </summary>
/// <param name="msg">string</param>
private void DisplayNewEvent(string msg)
{
output.AppendText(msg + newline);
output.Focus();
output.SelectionStart = output.Text.Length + 1;
output.SelectionLength = 0;
output.ScrollToCaret();
}

private void startButton_Click(object sender, System.EventArgs e)
{
worker1 = new Thread(new ThreadStart(GoThroughMyDocuments));
worker1.Start();

// primary thread continues its execution while the worker goes
// throught its laborous task.
}

/// <summary>
/// this functions goes through all the folders and files and places
the
/// filename in the richtext box...
/// </summary>
private void GoThroughMyDocuments()
{
DirectoryInfo info = new DirectoryInfo(
@"C:\Documents and Settings\" +
SystemInformation.UserName +
@"\My Documents");

RecursivelyTransverseFilesAndFolders(info.GetDirec tories());
GetFileName(info.GetFiles());
worker1.Abort();
}

//recursively find all the files in all directories in "My
Documents"
private void RecursivelyTransverseFilesAndFolders(DirectoryInfo[]
directories)
{
if(directories!=null)
foreach(DirectoryInfo dir in directories)
{
RecursivelyTransverseFilesAndFolders(dir.GetDirect ories());
GetFileName(dir.GetFiles());
}
}

// get each file name and print its full path to the text box
private void GetFileName(FileInfo[] info)
{
if(info!=null)
foreach(FileInfo file in info)
DisplayNewEvent(file.FullName);
}
}
}
Nov 15 '05 #1
1 3572
I hit the wrong "reply" button again. maybe it's time to change
newsreaders...

----- Original Message -----
From: "Wesman" <we********@hotmail.com>
Thanks in advance for any information on this matter. I have run into
a small richtextbox, scrolling and tread issue. Which has me totally
confused. Instead of trying to explain it I wrote some small C# code
to demonstrate the fact that in the threaded version the textbox
simple doesn't scroll like it should. please look to the
startButton_Click method for the distinction between the two
programs..

Well, the first thing I noticed in Form2 is that it appears you are trying
to update a UI from another thread. That is, you start a new thread with
the method:
GoThroughMyDocuments
that calls
GetFileName
which finally calls
DisplayNewEvent
This method works with the UI. However, it is not running on the same
thread as the UI (sometimes called the UI thread). Big no-no.

See this article for info:
http://msdn.microsoft.com/msdnmag/is...g/default.aspx

Replace your GetFileName() with something like the following, and it seems
to work a lot better...

delegate void DisplayFilenameHandler (string text);

// get each file name and print its full path to the text box
private void GetFileName(FileInfo[] info)
{
DisplayFilenameHandler show = new DisplayFilenameHandler
(DisplayNewEvent);
if(info!=null)
foreach(FileInfo file in info)
{
output.Invoke (show, new object[] {file.FullName});
//DisplayNewEvent(file.FullName);
}
}

Additionally, i'd be tempted to add a cancel button. The above article
should give you some tips on doing that. I'd suggest inheriting from the
authors AsyncOperation class to create your "worker" class. Put all your
code in "DoWork" with checks for the canceled flag, and using his event
firing methods to assist with updating the UI.
Mike Mayer
http://www.mag37.com
mm*****@mag37.com
Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bob Greschke | last post by:
I have a program where the user pushes a button, a "starting" message is ..inserted to a text field with an associated scroll bar, a thread is started that inserts a "working..." message on to the...
1
by: Midas NDT Sales | last post by:
I have been looking at a simple example of a scrolling text box (the one in the SAM book) as below: <script language="JavaScript"> var pos=100; function Scroll() { if...
44
by: Jim M | last post by:
I have had great success with using <iframe> with overflow-y set to auto. I can get a similar look with the <iframe> tag. BUT... In all cases I need to have fixed heights. Is there a way to...
2
by: P2P | last post by:
Hi I am wondering if someone know of a free cross-browsers vertical scrolling script that - is cross cross-browsers - will call the scrolling content from an external html page or from a...
3
by: Arv Carsen | last post by:
Im a beginner with C#, but as I try to access a textbox from a thread I get the error message : "An unhandled exception of type 'System.InvalidOperationException' occurred in...
5
by: PythonistL | last post by:
I am a newbie with Javascript. I have this simple script for scrolling text <HTML> <HEAD> <TITLE>Scrolling Message Script</TITLE> <SCRIPT language="JavaScript"><!-- var msg = 'My scrolling...
3
by: Chamnap | last post by:
Hello everybody, I have one problem. I want to do something after the user finished scrolling. The scroll event fires whenever the user is scrolling. I don't want this actually. Does anyone has...
1
by: strontiumpaul | last post by:
I've created a customer panel that works as a RSS scrolling banner. If I start two scrolling banners (Component.Add), as one of the panels gets a new RSS feed it completely stops the other panel from...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.