|
Hi,
1)
there are basically two ways to have a switch of main form:
- inside static main() you have something like Application.Run(new Form1());
which creates your Form1, shows it, and runs a message loop on it, until you somehow
close that Form1. So if you need a Form2 after closing Form1, you could add
Application.Run(new Form2());
- an alternative is to keep static main as is, change Form1 to be invisible,
and let it create and show one or more forms at will, as in:
Form2 f2=new Form2();
f2.ShowDialog(); // = show and wait until closed
Form3 f3=new Form3();
f3.ShowDialog();
return;
of course, you can add any logic you need; or you could use Show() to see
several forms at once.
2)
Dont know, seems like a bug: initially blueish gradient, when changed, then
set back to BackColor=Control, it remain Control-gray (and the code is back to its
original !?).
Luc Pattyn
|
|
|
|
|
hmm that one looks helpful but u know I tried to change the color of the BindingNavigator back to control but its not giving the gradient effect on it anymore and it doesnt look good that way... well may be its bug but I hope its not and now I'm trying to do something abt that... hope something good comes up
thanks for ur help
Rocky
|
|
|
|
|
I have an application that will be running in the background and I want it to run every 5 minutes to check a file execute it and then wait another 5 minutes to do this. I tried doing this, but I get the following error.
Unhandled Exception: System.Threading.THreadStateException: Thread is running or terminated; it ca not restart.<br />
<br />
Thread t1 = new Thread( new ThreadStart(myMethod));<br />
bool start = true;<br />
int stop = 0;<br />
while(start)<br />
{ <br />
t1.Start(); <br />
Thread.sleep(5000); <br />
if(stop==1000)<br />
{<br />
start = false;<br />
} <br />
stop++;<br />
} <br />
<br />
|
|
|
|
|
You don't want the application controlling the thread sleeping, you want the thread itself sleeping and determining when to exit.
void MakeThread()
{
Thread t1 = new Thread( new ThreadStart(MyMethod));
t1.IsBackground=true;
t1.Start();
}
void MyMethod()
{
while (++stop != 1000)
{
Thread.Sleep(5000);
}
}
Note setting the IsBackground to true, so if your application exits, the thread will terminate too. Otherwise you can have threads running even when your application has quit.
However, this isn't really what threads are intended for. Sounds more like what you want is a timer that fires every 5 seconds.
Marc
Thyme In The CountryPeople are just notoriously impossible. --DavidCrow There's NO excuse for not commenting your code. -- John Simmons / outlaw programmer People who say that they will refactor their code later to make it "good" don't understand refactoring, nor the art and craft of programming. -- Josh Smith
|
|
|
|
|
You are restarting the thread every five seconds, not every five minutes. If the thread ever takes more than five seconds to complete, you will be trying to restart a thread that is still running.
---
Year happy = new Year(2007);
|
|
|
|
|
I concur with the previous remarks, but want to add you constructed a loop that
really deserved a for statement:
for (int i=0; i<1000; i++) {
// do here what has to be done periodically
// ...
// now wait
Thread.Sleep(5000);
}
This approach repeats an operation with (an attempt to) a fixed time lapse in between;
if you want (an attempt to) predictable starting times, you definitely need to use a timer.
Luc Pattyn
|
|
|
|
|
I thought using thread will make the methods to run every 5 minutes.
So basicly i want the application to run in the background, I don't want to open the WindowsConsole and to exit the program I want to go to the task manager to stop it. So how can I make this that every 5 minutes it executes MyMethod():
|
|
|
|
|
You can make an app that does not show its main form, but shows a NotifyIcon.
Its main task would be to either hold a timer or a wait loop (as I have shown),
so whatever needs to be done gets done periodically.
You could add a ContextMenu with an "Exit" menu item that stops it (no need to
kill it with Task Manager!), and possibly some other menu items that allow you
to see some status, or set some parameters.
I guess there must be several examples available on CodeProject.
Luc Pattyn
|
|
|
|
|
thanks for the suggestion, I think is a good idea to have a ContextMenu and and a textarea to check the status.
|
|
|
|
|
Hey, everyone. I know it's possible, but i don't know how to access the filetypes part of Windows Explorer. I need to access the ext., the path, the name, and the image. And is it possible to open up the filetypes dialog without opening up Windows Explorer (from my program)? Thanks.
|
|
|
|
|
look at the Path class, it has static methods for almost anything you may need.
Luc Pattyn
|
|
|
|
|
I've done something similar in VBA once. Take a look into BuiltInDocumentProperties, maybe you'll finf what you need
<br />
Public Function GetWordTitle(strFileName As String) As String<br />
Dim wordApp As New Word.Application<br />
<br />
wordApp.Documents.Open strFileName, , True<br />
wordApp.Visible = False<br />
GetWordTitle = wordApp.ActiveDocument.BuiltInDocumentProperties("Title")<br />
<br />
<br />
wordApp.ActiveDocument.Close False<br />
Set wordApp = Nothing<br />
End Function<br />
|
|
|
|
|
Hi ALL
How can I read text file and make some process on it after it uploaded on the server.
Ala Qunaibi
Ala Qunaibi
|
|
|
|
|
That depends, is it your server ? Do you want to read it on the server, or on the client ? Once it's gone to the server, the client can only read it if you write methods on the server to send it back down again.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Thanks Christian Graus
I want to read uploaded text file on my server.
How can i do that?
Ala Qunaibi
|
|
|
|
|
OK, then exactly as I said, it needs to exist in a way that can be browsed to, or it needs otherwise to be exposed by the web site.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
Hello everybuddy,
I am a little confused about why reading and writing a resource concurrently may be unsafe in some scenarios as I explain:
1. Blocking multiple threads from writing a resource based on its current value (such as incrementing some number, or manipulating some collections) is logical and I have no problem with it.
2. Blocking multiple threads from setting a resource to some new value independent of its current value seems unnecessary in my opinion, I need some clarification by some friend here why such synchronization is required. (more details: while there is no guarantee that if we block at start of the write process, which thread enters the section first, then that should not be important in not-locking scenario, which thread completes actual write instructions first and the other overwrites it, so why should we lock?! ).
3. Blocking two threads that one is writing the resource and another is reading it, also seems unnecessary, again I need clarification. (More details: Again we don't know which thread enters the locked section first, so that should not be important in not-locking scenario if the reader thread reads the value before the write process completely takes place or after that).
Thank you so much for any help
- den2fly
---
"Art happens when you least expect it."
|
|
|
|
|
For sure, there are situations where a single variable can be written and read by multiple
threads without needing a lock.
And there are some situations where a more complex set of data can be maintained without
a lock. Example: a circular buffer used by only one producer and only one consumer.
When you assemble such queue with a fixed-size array, a last-produced index and a
last-consumed index, it can work perfectly without any synchronization.
But then there are many situations where a group of values describe the state of something,
and not all combinations of values are allowed; so when a new state needs to be established
multiple values need updating, and reading some of them while that update is happening
would result in an unconsistent view. Here you definitely need locks.
Hope this helps.
Luc Pattyn
|
|
|
|
|
Thank you very much for the helpful info yo provided. But I still need dedicated replies on the three situations I explained in details.
---
"Art happens when you least expect it."
|
|
|
|
|
If you provide a code snippet where you are not sure a lock is required, I could help
you in showing it does or does not need a lock. For each method shown, please indicate
how many threads (1 or more?) could be executing it.
Luc Pattyn
|
|
|
|
|
Sample:
Here I have a collection class that I want to be thread safe for reading from and manipulation (Not also in the case of iterations). It is assumed any number of threads may enter any of the methods or indexers at the same time. I think for reading from indexers there is no need for a lock, even if there is a chance for more than one thread to enter the indexers, and/or at the same time to one of the Add, Remove, Clear methods. Is it correct? (sorry I don't know how to format code snippets in the post)
internal class MyThreadSafeCollection : System.Collections.Specialized.NameObjectCollectionBase
{
const int readLockTimeout = 100;
const int writeLockTImeout = 100;
private ReaderWriterLock rwLock = new ReaderWriterLock();
public MyThreadSafeCollection()
{
}
public object this[int index]
{
get
{
rwLock.AcquireReaderLock(readLockTimeout); // IS THIS REQUIRED ??
try
{
return BaseGet(index);
}
finally
{
rwLock.ReleaseReaderLock();
}
}
}
public SocketClientData this[string id]
{
get
{
rwLock.AcquireReaderLock(readLockTimeout); // IS THIS REQUIRED ??
try
{
return BaseGet(id);
}
finally
{
rwLock.ReleaseReaderLock();
}
}
}
public void Add(string id, SocketClientData client)
{
rwLock.AcquireWriterLock(writeLockTImeout); // I Know it is required. (isn't it?)
try
{
BaseAdd(id, client);
}
finally
{
rwLock.ReleaseWriterLock();
}
}
public void Remove(string id)
{
rwLock.AcquireWriterLock(writeLockTImeout); // I Know it is required. (isn't it?)
try
{
BaseRemove(id);
}
finally
{
rwLock.ReleaseWriterLock();
}
}
public void Clear()
{
rwLock.AcquireWriterLock(writeLockTImeout); // I Know it is required. (isn't it?)
try
{
BaseClear();
}
finally
{
rwLock.ReleaseWriterLock();
}
}
---
"Art happens when you least expect it."
|
|
|
|
|
OK, AFAIK the general answer is yes, you need all the locks:
1)
for operations that modify the data (add, remove, clear) if more than one thread were
to perform a modification at the same time, the result would be unpredictable without lock.
2)
for operations that just read the data, if another thread were to write at the same time,
the results again COULD be unpredictable (it depends on how the data actually is structured),
so you should acquire a reader lock (which is less demanding than a writer lock, it does not block other readers on the same object).
3)
of course there is an overall concern as to where the lock needs to be implemented.
My answer typically is at the highest possible level, i.e. the level where your
functional primitives are located. Example: if you use an indexor into a collection,
what is the meaning of the index if the collection could change in the mean time ?
void sumAll(myCollection) {
int sum=0;
int n=myCollection.Count;
for (int i=0; i<n; i++) sum=sum+myCollection[i];
return sum;
}
in the above snippet, seems to me the entire for loop needs a lock, otherwise how
can we be sure we get the collection items we intended, since without an overall lock
the collection (and hence the index-object relations) may change (probably not so
if lookup by key). In the example, the items summed may not be the ones that were in
the collection at the time the count was obtained !
Once you do use an overall lock, you may not want a low-level lock
anymore (and that is, I guess, why the NET Collections dont have a lock built-in).
So in conclusion:
- there has to be a lock on all operations
- positioning the lock code is a design issue:
too low ==> may produce wrong results
too high ==> may be counterproductive (multithreading not working anymore),
and possibly deadlocks
as for performance, locks at higher level are better (fewer acq/rel).
- All this makes it difficult, if not impossible, to build a really useful thread-safe
collection, independent of the application itself.
Hope this helps.
Luc Pattyn
|
|
|
|
|
Thank you very very much buddy, Yes that really helped and clarified a lot for me
I wish I can help u back some day.
---
"Art happens when you least expect it."
|
|
|
|
|
Hello every one. i have develope a chat software in C# and online help for it but i dont know the way how to access the main page of that online help from the help menu button. Is there any one who can help me in this regard??????
|
|
|
|
|
Process.Start will launch the doc with the default viewer. IE can be embedded in your app, if you prefer.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|