473,386 Members | 1,801 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,386 software developers and data experts.

Thread not starting.

Fr33dan
57
Hi all,

I have a class that is written to display logging messages to a Form that I wrote. Because I don't want any other code to be stopped by the Form I decided to run the it in a separate thread. That thread however does not seem to be working.

This is the calling code:
Expand|Select|Wrap|Line Numbers
  1. windowThread = new Thread(new ThreadStart(Log.startWindow));
  2. windowThread.Name = "Window";
  3. windowThread.Start();
And this is the method called:
Expand|Select|Wrap|Line Numbers
  1. public static void startWindow()
  2. {
  3.     MessageBox.Show("HEY YOU");
  4.     LogWindow form = new LogWindow();
  5.     Window = form;
  6.     form.ShowDialog();
  7. }
If a just call startWindow() it runs without a problem. When it's called as shown here no MessageBox shows up and the Window variable remains null.

I've tried putting a breakpoint in the method but it never does anything. I can however see in the Threads window that the it does start and run. It just doesn't do anything.

The part that is really bothering me is that this code has worked in the past. I wrote a demonstration application that used this project as a library and it worked fine. Since then this code hasn't been touched.

Thanks in advance for you help,
Fr33dan

Edit: Crap wrote all this then realized I was in the wrong forum. Is it possible for a someone to move this to the C# forum?
May 27 '09 #1
6 5786
PRR
750 Expert 512MB
If i understood you correctly, you want to
1. "Somewhere" in form1, start a thread which calls class function (or object) which in turn takes care of displaying logs?

If you create a thread then to update the UI with "that" thread... You will have to use delegates for that ...You cant update the UI from other threads directly ....
Post some( more) sample code and explain further...
May 28 '09 #2
Fr33dan
57
Actually there is no other form at the moment since I'm working on backbone. Right now I only use the log class from a test file that writes a single statement to the log.

The log class is completely static and I use a static initializer to set everything up. My calling code is in that initializer (Along with the initialization of various other forms of logging output).

I am aware of that cross-threading calls cannot be made directly so I do use the invoke method later when I need to update the window. That code however is what initially gave me a problem (the window was null because this code never ran).

I would post more code but I'm out of the office at the today.
May 28 '09 #3
PRR
750 Expert 512MB
To have a different thread update the UI you can do the following....
* Have a class inherit from EventArgs...
* Declare a delegate and event in this class...
* Declare a Timer function ( assuming that you want to periodically update the UI)
Expand|Select|Wrap|Line Numbers
  1. public class MyClass : System.EventArgs
  2.     {
  3.         private  System.Timers.Timer aTimer;
  4.         // Timer to execute so that UI is updated regurarly 
  5.         // 
  6.  
  7.  
  8.         public  delegate void MyDelegate(string e);
  9.         public static event MyDelegate del;
  10.  
  11.  
  12.         public MyClass()
  13.         {          
  14.  
  15.             aTimer = new System.Timers.Timer(10000);
  16.  
  17.             aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
  18.  
  19.             aTimer.Interval = 2000;
  20.             aTimer.Enabled = true;
  21.  
  22.         }
  23.  
  24.  
  25.         private static void OnTimedEvent(object source, ElapsedEventArgs e)
  26.         {
  27.             if (del != null)
  28.             {
  29.                 del("Prr time is  :" + DateTime.Now.ToString());
  30.             }
  31.  
  32.             //Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
  33.         }
  34.  
  35.     }
  36.  
* The class constructor will initialize the timer function...
May 29 '09 #4
PRR
750 Expert 512MB
Run the program i attached ... you will get an idea.....

1. In the form load you start a thread which calls method to initialize the class object ...
2. You handle the event in the form.cs....( event defined in class)
3. In the event handler you call control.invoke... which basically makes it possible to update the UI part from non UI thread....

"Executes the specified delegate on the thread that owns the control's underlying window handle." MSDN
Attached Files
File Type: zip New WinRAR ZIP archive (2).zip (35.1 KB, 119 views)
May 29 '09 #5
Fr33dan
57
Thank you for your help.

Your response made me realize that I had been over thinking the issue and missing the obvious solution. If I used the asynchronous BeginInvoke method (as opposed to Invoke) to call the methods on my Form the need for me to implement multiple threads goes away.

Thanks,
Fr33dan
Jun 2 '09 #6
PRR
750 Expert 512MB
Welcome! Do continue to post on Bytes ....
Jun 3 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Olivier Parisy | last post by:
Hi, I like to use thread to simplify the handling of independant, blocking tasks. But controling them from a main thread is not always easy to do in a clean way. So I've written some generic...
9
by: phil | last post by:
And sorry I got ticked, frustrating week >And I could help more, being fairly experienced with >threading issues and race conditions and such, but >as I tried to indicate in the first place,...
4
by: cedric | last post by:
hello, I encounter a problem using threads. I have a simple code to use a thread : cSurveillanceBorne = New clsThreadSurveillance _SurveillanceThread = New Thread(AddressOf...
7
by: Ivan | last post by:
Hi I have following problem: I'm creating two threads who are performing some tasks. When one thread finished I would like to restart her again (e.g. new job). Following example demonstrates...
8
by: JS | last post by:
I am monitoring/controlling some realtime activities in a manufacturing process. When a part comes into my station, I have a bunch of processing to do. There are 30-40 data acquisition and data...
5
by: taylorjonl | last post by:
I am completely baffled. I am writting a daemon application for my work to save me some time. The application works fine at my home but won't work right here at work. Basically I have a...
5
by: zxo102 | last post by:
Hi, I am doing a small project using socket server and thread in python. This is first time for me to use socket and thread things. Here is my case. I have 20 socket clients. Each client send a...
3
by: NaeiKinDus | last post by:
Hello, i'm trying to program a thread that would be locked (by a mutex) and that would only be unlocked once that a function (generating data) is done. The purpose is to generate data, and unlock...
16
by: Hendrik van Rooyen | last post by:
I thought I would share this nasty little gotcha with the group. Consider the following code fragment: <start> print 'starting kbd thread' keyboard_thread = thread.start_new_thread(kbd_driver...
6
by: Lars Uffmann | last post by:
In an event routine, I want to end a certain thread. I am setting a flag that is checked by the thread and causes it to end, when it is set. Then the thread sets a "response" flag, just before...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.